From 30d6f15f8774a6f1d92b423c3b1b6a8d81456847 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Fri, 22 Jul 2022 22:50:51 +0700 Subject: [PATCH] feat: split depsURL and despFile --- internal/cli/action.go | 2 ++ internal/cli/action_run.go | 53 +++++++++++++++++++------------------- internal/cli/app.go | 16 +++++++++--- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/internal/cli/action.go b/internal/cli/action.go index a424f2a..02daf89 100644 --- a/internal/cli/action.go +++ b/internal/cli/action.go @@ -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) diff --git a/internal/cli/action_run.go b/internal/cli/action_run.go index 041534c..6188f46 100644 --- a/internal/cli/action_run.go +++ b/internal/cli/action_run.go @@ -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,30 +125,26 @@ 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) - - // If not url, try to read local file - depsFileBytes, 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)) - return "", nil - } - - return "", fmt.Errorf("failed to read file %s: %w", a.flags.depsFile, err) - } - - depsFileStr = strings.TrimSpace(string(depsFileBytes)) + return strings.TrimSpace(string(depsBytes)), nil } - return depsFileStr, nil + // 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)) + return "", nil + } + + return "", fmt.Errorf("failed to read file %s: %w", a.flags.depsFile, err) + } + + return strings.TrimSpace(string(depsBytes)), nil } func (a *action) runUpgradeModule( diff --git a/internal/cli/app.go b/internal/cli/app.go index ff54dc2..94f57e8 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -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"} @@ -46,9 +50,13 @@ func NewApp() *App { Usage: usageFlagVerbose, }, &cli.StringFlag{ - Name: flagDepsFile, - Usage: usageDepsFile, - Required: true, + Name: flagDepsFile, + Usage: usageDepsFile, + Value: defaultDepsFile, + }, + &cli.StringFlag{ + Name: flagDepsURL, + Usage: usageDepsURL, }, &cli.BoolFlag{ Name: flagDryRun,