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 {
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)

View File

@ -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(

View File

@ -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,