chore: improve migrate confirmation (#1488)

master
Kevin Wan 3 years ago committed by GitHub
parent 0e0abc3a95
commit 6f4b97864a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -27,7 +27,16 @@ import (
const zeromicroVersion = "1.3.0" const zeromicroVersion = "1.3.0"
var fset = token.NewFileSet() const (
confirmUnknown = iota
confirmAll
confirmIgnore
)
var (
fset = token.NewFileSet()
builderxConfirm = confirmUnknown
)
func Migrate(c *cli.Context) error { func Migrate(c *cli.Context) error {
verbose := c.Bool("verbose") verbose := c.Bool("verbose")
@ -133,7 +142,7 @@ func rewriteFile(pkgs map[string]*ast.Package, verbose bool) error {
} }
if verbose { if verbose {
console.Debug("[...] migrate %q ... ", filepath.Base(filename)) console.Debug("[...] migrating %q ... ", filepath.Base(filename))
} }
if strings.Contains(imp.Path.Value, deprecatedBuilderx) { if strings.Contains(imp.Path.Value, deprecatedBuilderx) {
@ -179,7 +188,7 @@ func writeFile(pkgs []*ast.Package, verbose bool) error {
return fmt.Errorf("[rewriteImport] write file %s error: %+v", filename, err) return fmt.Errorf("[rewriteImport] write file %s error: %+v", filename, err)
} }
if verbose { if verbose {
console.Success("[OK] migrate %q success ", filepath.Base(filename)) console.Success("[OK] migrated %q successfully", filepath.Base(filename))
} }
} }
} }
@ -239,11 +248,21 @@ func replacePkg(file *ast.File) {
} }
func refactorBuilderx(deprecated, replacement string, fn func(allow bool)) { func refactorBuilderx(deprecated, replacement string, fn func(allow bool)) {
switch builderxConfirm {
case confirmAll:
fn(true)
return
case confirmIgnore:
fn(false)
return
}
msg := fmt.Sprintf(`Detects a deprecated package in the source code, msg := fmt.Sprintf(`Detects a deprecated package in the source code,
Deprecated package: %q Deprecated package: %q
Replacement package: %q Replacement package: %q
It's recommended to use the replacement package, do you want to replace? It's recommended to use the replacement package, do you want to replace?
[input 'Y' for yes, 'N' for no]: `, deprecated, replacement) ['Y' for yes, 'N' for no, 'A' for all, 'I' for ignore]: `,
deprecated, replacement)
if runtime.GOOS != vars.OsWindows { if runtime.GOOS != vars.OsWindows {
msg = aurora.Yellow(msg).String() msg = aurora.Yellow(msg).String()
@ -253,21 +272,23 @@ It's recommended to use the replacement package, do you want to replace?
for { for {
var in string var in string
fmt.Scanln(&in) fmt.Scanln(&in)
if len(in) == 0 { switch {
console.Warning("nothing input, please try again [input 'Y' for yes, 'N' for no]:") case strings.EqualFold(in, "Y"):
continue
}
if strings.EqualFold(in, "Y") {
fn(true) fn(true)
return return
} case strings.EqualFold(in, "N"):
fn(false)
if strings.EqualFold(in, "N") { return
case strings.EqualFold(in, "A"):
fn(true)
builderxConfirm = confirmAll
return
case strings.EqualFold(in, "I"):
fn(false) fn(false)
builderxConfirm = confirmIgnore
return return
default:
console.Warning("['Y' for yes, 'N' for no, 'A' for all, 'I' for ignore]: ")
} }
console.Warning("invalid input, please try again [input 'Y' for yes, 'N' for no]:")
} }
} }

Loading…
Cancel
Save