feat: split depsURL and despFile

main
sudo pacman -Syu 2022-07-22 22:50:51 +07:00
parent 782526b08a
commit 30d6f15f87
No known key found for this signature in database
GPG Key ID: D6CB5C6C567C47B0
3 changed files with 40 additions and 31 deletions

View File

@ -9,6 +9,7 @@ import (
type action struct { type action struct {
flags struct { flags struct {
depsFile string depsFile string
depsURL string
verbose bool verbose bool
dryRun bool dryRun bool
} }
@ -21,6 +22,7 @@ func (a *action) RunHelp(c *cli.Context) error {
func (a *action) getFlags(c *cli.Context) { func (a *action) getFlags(c *cli.Context) {
a.flags.verbose = c.Bool(flagVerbose) a.flags.verbose = c.Bool(flagVerbose)
a.flags.depsFile = c.String(flagDepsFile) a.flags.depsFile = c.String(flagDepsFile)
a.flags.depsURL = c.String(flagDepsURL)
a.flags.dryRun = c.Bool(flagDryRun) a.flags.dryRun = c.Bool(flagDryRun)
a.log("flags %+v", a.flags) a.log("flags %+v", a.flags)

View File

@ -48,18 +48,18 @@ func (a *action) Run(c *cli.Context) error {
existVendor = true existVendor = true
} }
depsFileStr, err := a.runReadDepsFile(c) depsStr, err := a.runReadDepsFile(c)
if err != nil { if err != nil {
return err return err
} }
if depsFileStr == "" { if depsStr == "" {
return nil return nil
} }
// Read deps file line by line to upgrade // Read deps file line by line to upgrade
successUpgradedModules := make([]Module, 0, 100) successUpgradedModules := make([]Module, 0, 100)
modulePaths := strings.Split(depsFileStr, "\n") modulePaths := strings.Split(depsStr, "\n")
for _, modulePath := range modulePaths { for _, modulePath := range modulePaths {
successUpgradedModules, err = a.runUpgradeModule(c, mapImportedModules, successUpgradedModules, modulePath) successUpgradedModules, err = a.runUpgradeModule(c, mapImportedModules, successUpgradedModules, modulePath)
if err != nil { 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) { func (a *action) runReadDepsFile(c *cli.Context) (string, error) {
// Try to parse url first // Try to read from url first
var depsFileStr string if a.flags.depsURL != "" {
depsFileURL, err := url.Parse(a.flags.depsFile) depsURL, err := url.Parse(a.flags.depsURL)
if err == nil {
httpRsp, err := http.Get(depsFileURL.String())
if err != nil { 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() 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) 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 { if err != nil {
return "", fmt.Errorf("failed to read http response body: %w", err) return "", fmt.Errorf("failed to read http response body: %w", err)
} }
depsFileStr = string(depsFileBytes) return strings.TrimSpace(string(depsBytes)), nil
} 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 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( func (a *action) runUpgradeModule(

View File

@ -13,14 +13,18 @@ const (
flagVerbose = "verbose" flagVerbose = "verbose"
flagDepsFile = "deps-file" flagDepsFile = "deps-file"
flagDepsURL = "deps-url"
flagDryRun = "dry-run" flagDryRun = "dry-run"
commandRun = "run" commandRun = "run"
usageCommandRun = "run the program" usageCommandRun = "run the program"
usageFlagVerbose = "show what is going on" 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" usageDryRun = "demo what would be done"
defaultDepsFile = ".deps"
) )
var aliasFlagVerbose = []string{"v"} var aliasFlagVerbose = []string{"v"}
@ -46,9 +50,13 @@ func NewApp() *App {
Usage: usageFlagVerbose, Usage: usageFlagVerbose,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: flagDepsFile, Name: flagDepsFile,
Usage: usageDepsFile, Usage: usageDepsFile,
Required: true, Value: defaultDepsFile,
},
&cli.StringFlag{
Name: flagDepsURL,
Usage: usageDepsURL,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: flagDryRun, Name: flagDryRun,