feat: split depsURL and despFile
parent
782526b08a
commit
30d6f15f87
|
@ -9,6 +9,7 @@ import (
|
|||
type action struct {
|
||||
flags struct {
|
||||
depsFile string
|
||||
depsURL string
|
||||
verbose bool
|
||||
dryRun bool
|
||||
}
|
||||
|
@ -21,6 +22,7 @@ func (a *action) RunHelp(c *cli.Context) error {
|
|||
func (a *action) getFlags(c *cli.Context) {
|
||||
a.flags.verbose = c.Bool(flagVerbose)
|
||||
a.flags.depsFile = c.String(flagDepsFile)
|
||||
a.flags.depsURL = c.String(flagDepsURL)
|
||||
a.flags.dryRun = c.Bool(flagDryRun)
|
||||
|
||||
a.log("flags %+v", a.flags)
|
||||
|
|
|
@ -48,18 +48,18 @@ func (a *action) Run(c *cli.Context) error {
|
|||
existVendor = true
|
||||
}
|
||||
|
||||
depsFileStr, err := a.runReadDepsFile(c)
|
||||
depsStr, err := a.runReadDepsFile(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if depsFileStr == "" {
|
||||
if depsStr == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Read deps file line by line to upgrade
|
||||
successUpgradedModules := make([]Module, 0, 100)
|
||||
modulePaths := strings.Split(depsFileStr, "\n")
|
||||
modulePaths := strings.Split(depsStr, "\n")
|
||||
for _, modulePath := range modulePaths {
|
||||
successUpgradedModules, err = a.runUpgradeModule(c, mapImportedModules, successUpgradedModules, modulePath)
|
||||
if err != nil {
|
||||
|
@ -108,13 +108,16 @@ func (a *action) runGetImportedModules(c *cli.Context) (map[string]struct{}, err
|
|||
}
|
||||
|
||||
func (a *action) runReadDepsFile(c *cli.Context) (string, error) {
|
||||
// Try to parse url first
|
||||
var depsFileStr string
|
||||
depsFileURL, err := url.Parse(a.flags.depsFile)
|
||||
if err == nil {
|
||||
httpRsp, err := http.Get(depsFileURL.String())
|
||||
// Try to read from url first
|
||||
if a.flags.depsURL != "" {
|
||||
depsURL, err := url.Parse(a.flags.depsURL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to http get %s: %w", depsFileURL.String(), err)
|
||||
return "", fmt.Errorf("failed to parse deps file url %s: %w", a.flags.depsURL, err)
|
||||
}
|
||||
|
||||
httpRsp, err := http.Get(depsURL.String())
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to http get %s: %w", depsURL.String(), err)
|
||||
}
|
||||
defer httpRsp.Body.Close()
|
||||
|
||||
|
@ -122,17 +125,16 @@ func (a *action) runReadDepsFile(c *cli.Context) (string, error) {
|
|||
return "", fmt.Errorf("http status code not ok %d: %w", httpRsp.StatusCode, ErrFailedStatusCode)
|
||||
}
|
||||
|
||||
depsFileBytes, err := io.ReadAll(httpRsp.Body)
|
||||
depsBytes, err := io.ReadAll(httpRsp.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read http response body: %w", err)
|
||||
}
|
||||
|
||||
depsFileStr = string(depsFileBytes)
|
||||
} else {
|
||||
a.log("url parse error: %s", err)
|
||||
return strings.TrimSpace(string(depsBytes)), nil
|
||||
}
|
||||
|
||||
// If not url, try to read local file
|
||||
depsFileBytes, err := os.ReadFile(a.flags.depsFile)
|
||||
// If empty url, try to read from file
|
||||
depsBytes, err := os.ReadFile(a.flags.depsFile)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
color.PrintAppWarning(name, fmt.Sprintf("deps file [%s] not found", a.flags.depsFile))
|
||||
|
@ -142,10 +144,7 @@ func (a *action) runReadDepsFile(c *cli.Context) (string, error) {
|
|||
return "", fmt.Errorf("failed to read file %s: %w", a.flags.depsFile, err)
|
||||
}
|
||||
|
||||
depsFileStr = strings.TrimSpace(string(depsFileBytes))
|
||||
}
|
||||
|
||||
return depsFileStr, nil
|
||||
return strings.TrimSpace(string(depsBytes)), nil
|
||||
}
|
||||
|
||||
func (a *action) runUpgradeModule(
|
||||
|
|
|
@ -13,14 +13,18 @@ const (
|
|||
|
||||
flagVerbose = "verbose"
|
||||
flagDepsFile = "deps-file"
|
||||
flagDepsURL = "deps-url"
|
||||
flagDryRun = "dry-run"
|
||||
|
||||
commandRun = "run"
|
||||
|
||||
usageCommandRun = "run the program"
|
||||
usageFlagVerbose = "show what is going on"
|
||||
usageDepsFile = "show what deps need to upgrade"
|
||||
usageDepsFile = "file which show what deps need to upgrade"
|
||||
usageDepsURL = "url which show what deps need to upgrade"
|
||||
usageDryRun = "demo what would be done"
|
||||
|
||||
defaultDepsFile = ".deps"
|
||||
)
|
||||
|
||||
var aliasFlagVerbose = []string{"v"}
|
||||
|
@ -48,7 +52,11 @@ func NewApp() *App {
|
|||
&cli.StringFlag{
|
||||
Name: flagDepsFile,
|
||||
Usage: usageDepsFile,
|
||||
Required: true,
|
||||
Value: defaultDepsFile,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: flagDepsURL,
|
||||
Usage: usageDepsURL,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: flagDryRun,
|
||||
|
|
Loading…
Reference in New Issue