mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
@ -20,6 +20,7 @@ type cFix struct {
|
||||
|
||||
type cFixInput struct {
|
||||
g.Meta `name:"fix"`
|
||||
Path string `name:"path" brief:"directory path, it uses current working directory in default"`
|
||||
}
|
||||
|
||||
type cFixOutput struct{}
|
||||
@ -32,12 +33,15 @@ type cFixItem struct {
|
||||
func (c cFix) Index(ctx context.Context, in cFixInput) (out *cFixOutput, err error) {
|
||||
mlog.Print(`start auto fixing...`)
|
||||
defer mlog.Print(`done!`)
|
||||
err = c.doFix()
|
||||
if in.Path == "" {
|
||||
in.Path = gfile.Pwd()
|
||||
}
|
||||
err = c.doFix(in)
|
||||
return
|
||||
}
|
||||
|
||||
func (c cFix) doFix() (err error) {
|
||||
version, err := c.getVersion()
|
||||
func (c cFix) doFix(in cFixInput) (err error) {
|
||||
version, err := c.getVersion(in)
|
||||
if err != nil {
|
||||
mlog.Fatal(err)
|
||||
}
|
||||
@ -83,10 +87,10 @@ func (c cFix) doFixV23(version string) error {
|
||||
return gfile.ReplaceDirFunc(replaceFunc, ".", "*.go", true)
|
||||
}
|
||||
|
||||
func (c cFix) getVersion() (string, error) {
|
||||
func (c cFix) getVersion(in cFixInput) (string, error) {
|
||||
var (
|
||||
err error
|
||||
path = "go.mod"
|
||||
path = gfile.Join(in.Path, "go.mod")
|
||||
version string
|
||||
)
|
||||
if !gfile.Exists(path) {
|
||||
@ -95,7 +99,7 @@ func (c cFix) getVersion() (string, error) {
|
||||
err = gfile.ReadLines(path, func(line string) error {
|
||||
array := gstr.SplitAndTrim(line, " ")
|
||||
if len(array) > 0 {
|
||||
if array[0] == gfPackage {
|
||||
if gstr.HasPrefix(array[0], gfPackage) {
|
||||
version = array[1]
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ type cUp struct {
|
||||
}
|
||||
|
||||
const (
|
||||
gfPackage = `github.com/gogf/gf/v2`
|
||||
gfPackage = `github.com/gogf/gf/`
|
||||
cUpEg = `
|
||||
gf up
|
||||
gf up -a
|
||||
@ -46,20 +46,19 @@ type cUpInput struct {
|
||||
type cUpOutput struct{}
|
||||
|
||||
func (c cUp) Index(ctx context.Context, in cUpInput) (out *cUpOutput, err error) {
|
||||
defer mlog.Print(`done!`)
|
||||
defer func() {
|
||||
if err == nil {
|
||||
mlog.Print(`done!`)
|
||||
}
|
||||
}()
|
||||
|
||||
if in.All {
|
||||
in.Cli = true
|
||||
in.Fix = true
|
||||
}
|
||||
if err = c.doUpgradeVersion(ctx); err != nil {
|
||||
if err = c.doUpgradeVersion(ctx, in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if in.Fix {
|
||||
if err = c.doAutoFixing(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
//if in.Cli {
|
||||
// if err = c.doUpgradeCLI(ctx); err != nil {
|
||||
// return nil, err
|
||||
@ -68,9 +67,14 @@ func (c cUp) Index(ctx context.Context, in cUpInput) (out *cUpOutput, err error)
|
||||
return
|
||||
}
|
||||
|
||||
func (c cUp) doUpgradeVersion(ctx context.Context) (err error) {
|
||||
func (c cUp) doUpgradeVersion(ctx context.Context, in cUpInput) (err error) {
|
||||
mlog.Print(`start upgrading version...`)
|
||||
|
||||
type Package struct {
|
||||
Name string
|
||||
Version string
|
||||
}
|
||||
|
||||
var (
|
||||
dir = gfile.Pwd()
|
||||
temp string
|
||||
@ -78,12 +82,15 @@ func (c cUp) doUpgradeVersion(ctx context.Context) (err error) {
|
||||
)
|
||||
for {
|
||||
if gfile.Exists(path) {
|
||||
var packages []string
|
||||
var packages []Package
|
||||
err = gfile.ReadLines(path, func(line string) error {
|
||||
line = gstr.Trim(line)
|
||||
if gstr.HasPrefix(line, gfPackage) {
|
||||
pkg := gstr.Explode(" ", line)[0]
|
||||
packages = append(packages, pkg)
|
||||
array := gstr.SplitAndTrim(line, " ")
|
||||
packages = append(packages, Package{
|
||||
Name: array[0],
|
||||
Version: array[1],
|
||||
})
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@ -91,11 +98,18 @@ func (c cUp) doUpgradeVersion(ctx context.Context) (err error) {
|
||||
return
|
||||
}
|
||||
for _, pkg := range packages {
|
||||
mlog.Printf(`upgrading %s`, pkg)
|
||||
command := fmt.Sprintf(`go get -u %s@latest`, pkg)
|
||||
mlog.Printf(`upgrading "%s" from "%s" to "latest"`, pkg.Name, pkg.Version)
|
||||
command := fmt.Sprintf(`go get -u %s@latest`, pkg.Name)
|
||||
if err = gproc.ShellRun(ctx, command); err != nil {
|
||||
return
|
||||
}
|
||||
mlog.Print()
|
||||
}
|
||||
if in.Fix {
|
||||
if err = c.doAutoFixing(ctx, dir); err != nil {
|
||||
return err
|
||||
}
|
||||
mlog.Print()
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -110,12 +124,13 @@ func (c cUp) doUpgradeVersion(ctx context.Context) (err error) {
|
||||
|
||||
func (c cUp) doUpgradeCLI(ctx context.Context) (err error) {
|
||||
mlog.Print(`start upgrading cli...`)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c cUp) doAutoFixing(ctx context.Context) (err error) {
|
||||
mlog.Print(`start auto fixing...`)
|
||||
err = cFix{}.doFix()
|
||||
func (c cUp) doAutoFixing(ctx context.Context, dirPath string) (err error) {
|
||||
mlog.Printf(`auto fixing path "%s"...`, dirPath)
|
||||
err = cFix{}.doFix(cFixInput{
|
||||
Path: dirPath,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
@ -69,7 +69,10 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
command.Run(ctx)
|
||||
err = command.RunWithError(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// zsh alias "git fetch" conflicts checks.
|
||||
|
||||
Reference in New Issue
Block a user