Skip to content
This repository was archived by the owner on Dec 29, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,6 @@ linters:
- errorlint
- bodyclose
- exportloopref
- gci
# - gci
- gosec
fast: false
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
go.osspkg.com/goppy v0.14.0
golang.org/x/mod v0.13.0
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
go.osspkg.com/goppy v0.14.0 h1:7YoNaSA+XIAy+lbIMEdmMIXH9Em+keNRMVvqP4+4jiE=
go.osspkg.com/goppy v0.14.0/go.mod h1:NAWYk3WylEMTTcEgFiFEQsL69T/ox614gpuzlWyxlzg=
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
26 changes: 22 additions & 4 deletions internal/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
package lint

import (
"path/filepath"

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

Expand All @@ -18,10 +21,25 @@ func Cmd() console.CommandGetter {
global.SetupEnv()
console.Infof("--- LINT ---")

exec.CommandPack("bash",
"golangci-lint --version",
"golangci-lint -v run ./...",
)
cmds := make([]string, 0, 50)
cmds = append(cmds, "golangci-lint --version")
if files.Exist(files.CurrentDir() + "/go.work") {
cmds = append(cmds, "go work use -r .", "go work sync")
mods, err := files.Detect("go.mod")
console.FatalIfErr(err, "detects go.mod in workspace")
for _, mod := range mods {
dir := filepath.Dir(mod)
cmds = append(cmds,
"cd "+dir+" && golangci-lint -v run ./...",
)
}
} else {
cmds = append(cmds,
"golangci-lint -v run ./...",
)
}

exec.CommandPack("bash", cmds...)
})
})
}
27 changes: 22 additions & 5 deletions internal/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,28 @@ func CmdLib() console.CommandGetter {
console.FatalIfErr(os.WriteFile(files.CurrentDir()+"/"+name, []byte(config), 0755), "create config [%s]", name)
}

exec.CommandPack("bash",
"go mod tidy -compat=1.17",
"go mod download",
"go generate ./...",
)
cmds := make([]string, 0, 50)
if files.Exist(files.CurrentDir() + "/go.work") {
cmds = append(cmds, "go work use -r .", "go work sync")
mods, err := files.Detect("go.mod")
console.FatalIfErr(err, "detects go.mod in workspace")
for _, mod := range mods {
dir := filepath.Dir(mod)
cmds = append(cmds,
"cd "+dir+" && go mod tidy",
"cd "+dir+" && go mod download",
"cd "+dir+" && go generate ./...",
)
}
} else {
cmds = append(cmds,
"go mod tidy -compat=1.17",
"go mod download",
"go generate ./...",
)
}

exec.CommandPack("bash", cmds...)
})
})
}
Expand Down
135 changes: 135 additions & 0 deletions internal/tag/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package tag

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/osspkg/devtool/internal/global"
"github.com/osspkg/devtool/pkg/exec"
"github.com/osspkg/devtool/pkg/files"
"github.com/osspkg/devtool/pkg/modules"
"github.com/osspkg/devtool/pkg/repo"
"github.com/osspkg/devtool/pkg/ver"
"go.osspkg.com/goppy/sdk/console"
"golang.org/x/mod/modfile"
)

func Cmd() console.CommandGetter {
return console.NewCommand(func(setter console.CommandSetter) {
setter.Setup("tag", "")
setter.Flag(func(fs console.FlagsSetter) {
fs.Bool("minor", "update minor version (default - patch)")
})
setter.ExecFunc(func(_ []string, minor bool) {
global.SetupEnv()
console.Infof("--- READ CONFIG ---")

var (
allMods map[string]*modules.Mod
currmod *modules.Mod
err error
b []byte
f *modfile.File
fi os.FileInfo
HEAD string
)

console.Infof("--- GET ALL MODULES ---")

allMods, err = modules.Detect(files.CurrentDir())
console.FatalIfErr(err, "Detect go.mod files")

var root *modules.Mod
for _, m := range allMods {
if root == nil {
root = m
continue
}
if len(root.Name) > len(m.Name) {
root = m
}
}
for _, m := range allMods {
m.Prefix = strings.Trim(strings.TrimPrefix(m.Name, root.Name), "/")
if len(m.Prefix) > 0 {
m.Prefix += "/"
}
b, err = exec.SingleCmd(context.TODO(), "bash", "git tag -l "+m.Prefix+"v*")
console.FatalIfErr(err, "Get tags for: %s", m.Name)
m.Version = ver.Max(strings.Split(string(b), "\n")...)
}

console.Infof("--- DETECT CHANGES ---")

HEAD, err = repo.HEAD("")
console.FatalIfErr(err, "Get git HEAD")
b, err = exec.SingleCmd(context.TODO(), "bash", "git diff --name-only --ignore-submodules --diff-filter=ACMRTUXB origin/"+HEAD+" -- \"*.go\" \"*.mod\" \"*.sum\"")
console.FatalIfErr(err, "Detect changed files")
changedFiles := strings.Split(string(b), "\n")
for _, file := range changedFiles {
dir := filepath.Dir(file)
currmod, err = modules.Read(dir + "/go.mod")
if err != nil {
continue
}
for _, m := range allMods {
if m.Name == currmod.Name && !m.Changed {
m.Changed = true
if minor {
m.Version.Minor++
} else {
m.Version.Patch++
}
}
}
}

console.Infof("--- UPDATE MODULES ---")

for _, m := range allMods {
fmt.Println(">", m.Name)
fi, err = os.Stat(m.File)
console.FatalIfErr(err, "Get info go.mod file: %s", m.File)
b, err = os.ReadFile(m.File)
console.FatalIfErr(err, "Read go.mod file: %s", m.File)
f, err = modfile.Parse(m.File, b, func(path, version string) (string, error) {
if mm, ok := allMods[path]; ok && mm.Version.String() != version {
if !m.Changed {
if minor {
m.Version.Minor++
} else {
m.Version.Patch++
}
m.Changed = true
}
return mm.Version.String(), nil
}
return version, nil
})
console.FatalIfErr(err, "Parce go.mod file: %s", m.File)
b, err = f.Format()
console.FatalIfErr(err, "Format go.mod file: %s", m.File)
err = os.WriteFile(m.File, b, fi.Mode())
console.FatalIfErr(err, "Update go.mod file: %s", m.File)
}

console.Infof("--- GIT COMMITTED ---")

cmds := make([]string, 0, 50)
cmds = append(cmds,
"git add .",
"git commit -m \"update vendors\"",
)
for _, m := range allMods {
if !m.Changed {
continue
}
cmds = append(cmds, "git tag "+m.Prefix+m.Version.String())
}
exec.CommandPack("bash", cmds...)
})
})
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package main
import (
"os"

"github.com/osspkg/devtool/internal/tag"

"github.com/osspkg/devtool/internal/build"
"github.com/osspkg/devtool/internal/global"
"github.com/osspkg/devtool/internal/gosite"
Expand Down Expand Up @@ -44,6 +46,7 @@ func main() {
build.Cmd(),
lic.Cmd(),
gosite.Cmd(),
tag.Cmd(),
)

app.Exec()
Expand Down
70 changes: 70 additions & 0 deletions pkg/modules/mods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package modules

import (
"os"
"regexp"
"strings"

"github.com/osspkg/devtool/pkg/files"
"github.com/osspkg/devtool/pkg/ver"
"go.osspkg.com/goppy/sdk/errors"
)

var rex = regexp.MustCompile(`(?mU)module (.*)\n`)
var SkipErr = errors.New("skip read module")

type Mod struct {
Name string
File string
Prefix string
Version *ver.Ver
Changed bool
}

func Detect(dir string) (map[string]*Mod, error) {
list := make(map[string]*Mod, 20)
mods, err := files.DetectInDir(dir, "go.mod")
if err != nil {
return nil, err
}
var b []byte
for _, mod := range mods {
if b, err = os.ReadFile(mod); err != nil {
return nil, err
}

temp := rex.FindStringSubmatch(string(b))
if len(temp) != 2 {
continue
}
module := temp[1]
if !strings.Contains(module, "/") {
continue
}
list[module] = &Mod{
Name: module,
File: mod,
}
}

return list, nil
}

func Read(filepath string) (*Mod, error) {
b, err := os.ReadFile(filepath)
if err != nil {
return nil, err
}
temp := rex.FindStringSubmatch(string(b))
if len(temp) != 2 {
return nil, SkipErr
}
module := temp[1]
if !strings.Contains(module, "/") {
return nil, SkipErr
}
return &Mod{
Name: module,
File: filepath,
}, nil
}
31 changes: 31 additions & 0 deletions pkg/repo/head.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package repo

import (
"context"
"fmt"
"regexp"
"strings"

"github.com/osspkg/devtool/pkg/exec"
)

var rexHEAD = regexp.MustCompile(`(?mU)ref\: refs/heads/(\w+)\s+HEAD`)

func HEAD(url string) (string, error) {
if len(url) == 0 {
b, err := exec.SingleCmd(context.TODO(), "bash", "git remote get-url origin")
if err != nil {
return "", err
}
url = strings.Trim(string(b), "\n")
}
b, err := exec.SingleCmd(context.TODO(), "bash", "git ls-remote --symref "+url+" HEAD")
if err != nil {
return "", err
}
_strs := rexHEAD.FindStringSubmatch(string(b))
if len(_strs) != 2 {
return "", fmt.Errorf("HEAD not found")
}
return _strs[1], nil
}
Loading