Skip to content

Commit

Permalink
Merge pull request #9 from osspkg/develop
Browse files Browse the repository at this point in the history
add license cmd
  • Loading branch information
markus621 authored Jun 18, 2023
2 parents 8464352 + 71e174c commit ce9a032
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .lict
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
author: "Mikhail Knyzhev <markus621@yandex.ru>"
lic_short: BSD 3-Clause
lic_file: LICENSE
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/osspkg/devtool

go 1.17

require github.com/osspkg/go-sdk v1.2.5
require (
github.com/osspkg/go-sdk v1.2.6
gopkg.in/yaml.v3 v3.0.1
)
8 changes: 5 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFF
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/osspkg/go-algorithms v1.2.2/go.mod h1:Zdclm/CKhDrUD34kIm9PL4VDbiey/jXAHY36nfc0r5Q=
github.com/osspkg/go-sdk v1.2.5 h1:zeef6s8dg/sRyOVRyARksaREX+TY2PZtBcdD2m/SQss=
github.com/osspkg/go-sdk v1.2.5/go.mod h1:7gGXipcTc2sB+EzmBUSz5poGs6wM/xXnwCZBMKrIzD8=
github.com/osspkg/go-algorithms v1.2.3/go.mod h1:Zdclm/CKhDrUD34kIm9PL4VDbiey/jXAHY36nfc0r5Q=
github.com/osspkg/go-sdk v1.2.6 h1:wdIGUB6M7J7C9l7Ln/q8638mSZjd8qB+pv2RKS+MtPE=
github.com/osspkg/go-sdk v1.2.6/go.mod h1:nf6mgNnNMT0tEqrBc30rSRFvAJzP4Y4TKRaqq9iZu/Y=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
Expand Down Expand Up @@ -68,6 +68,8 @@ google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCID
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5 changes: 5 additions & 0 deletions internal/build/build.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright (c) 2022-2023 Mikhail Knyzhev <markus621@yandex.ru>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package build

import (
Expand Down
5 changes: 5 additions & 0 deletions internal/global/global.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright (c) 2022-2023 Mikhail Knyzhev <markus621@yandex.ru>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package global

import (
Expand Down
104 changes: 104 additions & 0 deletions internal/lic/lic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2022-2023 Mikhail Knyzhev <markus621@yandex.ru>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package lic

import (
"context"
"fmt"
"os"
"regexp"
"strings"
"time"

"github.com/osspkg/devtool/internal/global"
"github.com/osspkg/devtool/pkg/exec"
"github.com/osspkg/devtool/pkg/files"
"github.com/osspkg/go-sdk/console"
)

const (
gitFirstCommitDate = `git log --reverse --date="format:%Y" --format="format:%ad" | head -n 1`
licFilename = ".lict"
)

func Cmd() console.CommandGetter {
return console.NewCommand(func(setter console.CommandSetter) {
setter.Setup("license", "")
setter.ExecFunc(func(_ []string) {
global.SetupEnv()
console.Infof("--- LICENSE ---")

model := &Lic{}
if !files.Exist(licFilename) {
model.Default()
console.FatalIfErr(files.YamlWrite(licFilename, model), "Create `%s`", licFilename)
console.Errorf("Update `%s`", licFilename)
os.Exit(1)
} else {
console.FatalIfErr(files.YamlRead(licFilename, model), "Read `%s`", licFilename)
}

out, err := exec.SingleCmd(context.TODO(), "bash", gitFirstCommitDate)
console.FatalIfErr(err, "Get fist commit")

start, err := time.Parse("2006", strings.TrimSpace(string(out)))
console.FatalIfErr(err, "Get fist commit date parse")

startY := start.Year()
currY := time.Now().Year()
model.Years = fmt.Sprintf("%d-%d", startY, currY)
if startY >= currY {
model.Years = fmt.Sprintf("%d", currY)
}

goFiles, err := files.DetectByExt(".go")
console.FatalIfErr(err, "Get go files")

for _, file := range goFiles {
err = files.Rewrite(file, func(s string) string {
tmpl := buildTemplate(model)
return replaceLic(s, tmpl)
})
console.FatalIfErr(err, "Update go file `%s`", file)
}

exec.CommandPack("bash",
"go fmt ./...",
)
})
})
}

type Lic struct {
Author string `yaml:"author"`
LicShort string `yaml:"lic_short"`
LicFile string `yaml:"lic_file"`
Years string `yaml:"-"`
}

func (l *Lic) Default() {
l.Author = "User <user@email>"
l.LicShort = "MIT"
l.LicFile = "LICENSE"
}

const template = "/*\n" +
" * Copyright (c) %s %s. All rights reserved.\n" +
" * Use of this source code is governed by a %s license that can be found in the %s file.\n" +
" */\n\n"

func buildTemplate(m *Lic) string {
return fmt.Sprintf(template, m.Years, m.Author, m.LicShort, m.LicFile)
}

var rexLic = regexp.MustCompile(`(?miUs)^\/\*(.*Copyright.*)\*\/`)

func replaceLic(b string, n string) string {
if rexLic.MatchString(b) {
return rexLic.ReplaceAllString(b, n)
}
return n + b
}
5 changes: 5 additions & 0 deletions internal/lint/lint.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright (c) 2022-2023 Mikhail Knyzhev <markus621@yandex.ru>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package lint

import (
Expand Down
9 changes: 9 additions & 0 deletions internal/setup/setup.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright (c) 2022-2023 Mikhail Knyzhev <markus621@yandex.ru>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package setup

import (
Expand Down Expand Up @@ -549,6 +554,10 @@ setup:
lint:
devtool lint
.PHONY: license
license:
devtool license
.PHONY: build
build:
devtool build --arch=amd64
Expand Down
5 changes: 5 additions & 0 deletions internal/tests/tests.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright (c) 2022-2023 Mikhail Knyzhev <markus621@yandex.ru>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package tests

import (
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/*
* Copyright (c) 2022-2023 Mikhail Knyzhev <markus621@yandex.ru>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package main

import (
"os"

"github.com/osspkg/devtool/internal/build"
"github.com/osspkg/devtool/internal/global"
"github.com/osspkg/devtool/internal/lic"
"github.com/osspkg/devtool/internal/lint"
"github.com/osspkg/devtool/internal/setup"
"github.com/osspkg/devtool/internal/tests"
Expand Down Expand Up @@ -35,6 +41,7 @@ func main() {
lint.Cmd(),
tests.Cmd(),
build.Cmd(),
lic.Cmd(),
)

app.Exec()
Expand Down
14 changes: 14 additions & 0 deletions pkg/exec/exec.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright (c) 2022-2023 Mikhail Knyzhev <markus621@yandex.ru>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package exec

import (
Expand Down Expand Up @@ -35,6 +40,15 @@ func Command(shell string, command string) (err error) {
return
}

func SingleCmd(ctx context.Context, shell string, command string) ([]byte, error) {
console.Infof(command)
cmd := exec.CommandContext(ctx, shell, "-c", command)
cmd.Env = os.Environ()
cmd.Dir = files.CurrentDir()

return cmd.CombinedOutput()
}

func runCmd(ctx context.Context, shell string, command string) error {
console.Infof(command)
cmd := exec.CommandContext(ctx, shell, "-c", command)
Expand Down
38 changes: 38 additions & 0 deletions pkg/files/files.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright (c) 2022-2023 Mikhail Knyzhev <markus621@yandex.ru>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package files

import (
Expand All @@ -7,6 +12,7 @@ import (
"strings"

"github.com/osspkg/go-sdk/console"
"gopkg.in/yaml.v3"
)

func CurrentDir() string {
Expand All @@ -31,6 +37,22 @@ func Detect(filename string) ([]string, error) {
return files, err
}

func DetectByExt(ext string) ([]string, error) {
curDir := CurrentDir()
files := make([]string, 0)
err := filepath.Walk(curDir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() || filepath.Ext(info.Name()) != ext {
return nil
}
files = append(files, path)
return nil
})
return files, err
}

func Rewrite(filename string, cb func(string) string) error {
if !Exist(filename) {
if err := os.WriteFile(filename, []byte(""), 0755); err != nil {
Expand All @@ -57,3 +79,19 @@ func Folder(filename string) string {
tree := strings.Split(dir, string(os.PathSeparator))
return tree[len(tree)-1]
}

func YamlRead(filename string, v interface{}) error {
b, err := os.ReadFile(filename)
if err != nil {
return err
}
return yaml.Unmarshal(b, v)
}

func YamlWrite(filename string, v interface{}) error {
b, err := yaml.Marshal(v)
if err != nil {
return err
}
return os.WriteFile(filename, b, 0755)
}

0 comments on commit ce9a032

Please sign in to comment.