chore: use pointer for Module struct
parent
933b58da1d
commit
21a239aff4
|
@ -32,16 +32,6 @@ var (
|
||||||
ErrFailedStatusCode = errors.New("failed status code")
|
ErrFailedStatusCode = errors.New("failed status code")
|
||||||
)
|
)
|
||||||
|
|
||||||
// See https://pkg.go.dev/cmd/go#hdr-List_packages_or_modules
|
|
||||||
type Module struct {
|
|
||||||
Update *Module
|
|
||||||
Replace *Module
|
|
||||||
Path string
|
|
||||||
Version string
|
|
||||||
Main bool
|
|
||||||
Indirect bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *action) Run(c *cli.Context) error {
|
func (a *action) Run(c *cli.Context) error {
|
||||||
a.getFlags(c)
|
a.getFlags(c)
|
||||||
|
|
||||||
|
@ -66,7 +56,7 @@ func (a *action) Run(c *cli.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read deps file line by line to upgrade
|
// Read deps file line by line to upgrade
|
||||||
successUpgradedModules := make([]Module, 0, defaultCountModule)
|
successUpgradedModules := make([]*Module, 0, defaultCountModule)
|
||||||
modulePaths := strings.Split(depsStr, "\n")
|
modulePaths := strings.Split(depsStr, "\n")
|
||||||
for _, modulePath := range modulePaths {
|
for _, modulePath := range modulePaths {
|
||||||
successUpgradedModules, err = a.runUpgradeModule(
|
successUpgradedModules, err = a.runUpgradeModule(
|
||||||
|
@ -92,7 +82,7 @@ func (a *action) Run(c *cli.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all imported modules
|
// Get all imported modules
|
||||||
func (a *action) runGetImportedModules(c *cli.Context) (map[string]Module, error) {
|
func (a *action) runGetImportedModules(c *cli.Context) (map[string]*Module, error) {
|
||||||
goListAllArgs := []string{"list", "-m", "-json", "-mod=readonly", "all"}
|
goListAllArgs := []string{"list", "-m", "-json", "-mod=readonly", "all"}
|
||||||
goOutput, err := exec.CommandContext(c.Context, "go", goListAllArgs...).CombinedOutput()
|
goOutput, err := exec.CommandContext(c.Context, "go", goListAllArgs...).CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -105,13 +95,13 @@ func (a *action) runGetImportedModules(c *cli.Context) (map[string]Module, error
|
||||||
goOutputStr = strings.ReplaceAll(goOutputStr, "}{", "},{")
|
goOutputStr = strings.ReplaceAll(goOutputStr, "}{", "},{")
|
||||||
goOutputStr = "[" + goOutputStr + "]"
|
goOutputStr = "[" + goOutputStr + "]"
|
||||||
|
|
||||||
importedModules := make([]Module, 0, defaultCountModule)
|
importedModules := make([]*Module, 0, defaultCountModule)
|
||||||
if err := json.Unmarshal([]byte(goOutputStr), &importedModules); err != nil {
|
if err := json.Unmarshal([]byte(goOutputStr), &importedModules); err != nil {
|
||||||
return nil, fmt.Errorf("failed to json unmarshal: %w", err)
|
return nil, fmt.Errorf("failed to json unmarshal: %w", err)
|
||||||
}
|
}
|
||||||
a.log("Go output: %s\n", string(goOutput))
|
a.log("Go output: %s\n", string(goOutput))
|
||||||
|
|
||||||
mapImportedModules := make(map[string]Module)
|
mapImportedModules := make(map[string]*Module)
|
||||||
for _, importedModule := range importedModules {
|
for _, importedModule := range importedModules {
|
||||||
// Ignore main module
|
// Ignore main module
|
||||||
if importedModule.Main {
|
if importedModule.Main {
|
||||||
|
@ -179,10 +169,10 @@ func (a *action) runReadDepsFile() (depsStr string, useDepFile bool, err error)
|
||||||
|
|
||||||
func (a *action) runUpgradeModule(
|
func (a *action) runUpgradeModule(
|
||||||
c *cli.Context,
|
c *cli.Context,
|
||||||
mapImportedModules map[string]Module,
|
mapImportedModules map[string]*Module,
|
||||||
successUpgradedModules []Module,
|
successUpgradedModules []*Module,
|
||||||
modulePath string,
|
modulePath string,
|
||||||
) ([]Module, error) {
|
) ([]*Module, error) {
|
||||||
modulePath = strings.TrimSpace(modulePath)
|
modulePath = strings.TrimSpace(modulePath)
|
||||||
|
|
||||||
// Ignore empty
|
// Ignore empty
|
||||||
|
@ -211,8 +201,8 @@ func (a *action) runUpgradeModule(
|
||||||
}
|
}
|
||||||
a.log("Go output: %s\n", string(goOutput))
|
a.log("Go output: %s\n", string(goOutput))
|
||||||
|
|
||||||
module := Module{}
|
module := &Module{}
|
||||||
if err := json.Unmarshal(goOutput, &module); err != nil {
|
if err := json.Unmarshal(goOutput, module); err != nil {
|
||||||
return successUpgradedModules, fmt.Errorf("failed to json unmarshal: %w", err)
|
return successUpgradedModules, fmt.Errorf("failed to json unmarshal: %w", err)
|
||||||
}
|
}
|
||||||
a.log("Module: %+v\n", module)
|
a.log("Module: %+v\n", module)
|
||||||
|
@ -270,7 +260,7 @@ func (a *action) runGoMod(c *cli.Context, existVendor bool) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *action) runGitCommit(c *cli.Context, successUpgradedModules []Module, existVendor, useDepFile bool) error {
|
func (a *action) runGitCommit(c *cli.Context, successUpgradedModules []*Module, existVendor, useDepFile bool) error {
|
||||||
if a.flags.dryRun {
|
if a.flags.dryRun {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package cli
|
||||||
|
|
||||||
|
// See https://pkg.go.dev/cmd/go#hdr-List_packages_or_modules
|
||||||
|
type Module struct {
|
||||||
|
Update *Module
|
||||||
|
Replace *Module
|
||||||
|
Path string
|
||||||
|
Version string
|
||||||
|
Main bool
|
||||||
|
Indirect bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://pkg.go.dev/cmd/go#hdr-Edit_go_mod_from_tools_or_scripts
|
||||||
|
type GoMod struct {
|
||||||
|
Module ModPath
|
||||||
|
Go string
|
||||||
|
Toolchain string
|
||||||
|
}
|
||||||
|
|
||||||
|
type ModPath struct {
|
||||||
|
Path string
|
||||||
|
Deprecated string
|
||||||
|
}
|
Loading…
Reference in New Issue