Fill in migrations for cli.Exit and BoolFlag with Value

This commit is contained in:
Dan Buch 2016-05-30 15:15:49 -04:00
parent 1d13fa8140
commit 659e1c1e0b
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC
2 changed files with 47 additions and 2 deletions

View File

@ -20,7 +20,7 @@ _SLICE_TYPE_RE = re.compile(
flags=re.DOTALL
)
_FLAG_LITERAL_RE = re.compile(
'(?P<prefix>[^&]?)cli\\.(?P<type>\\w+)Flag{',
'(?P<prefix>\\s+)cli\\.(?P<type>\\w+)Flag{',
)
_COMMAND_SLICE_RE = re.compile(
'(?P<prefix>\\[\\])cli\\.Command{',
@ -28,6 +28,11 @@ _COMMAND_SLICE_RE = re.compile(
_COMMAND_LITERAL_RE = re.compile(
'(?P<prefix>\\s+)cli\\.Command{',
)
_EXIT_ERROR_RE = re.compile('cli\\.NewExitError')
_BOOL_T_FLAG_RE = re.compile(
'cli\\.BoolTFlag{(?P<args>[^}]*)}',
flags=re.DOTALL
)
_MIGRATORS = {}
@ -162,6 +167,21 @@ def _migrate_v1_imports(source):
return _V1_IMPORT_RE.sub(_V2_IMPORT, source)
@_migrator
def _migrate_new_exit_error(source):
return _EXIT_ERROR_RE.sub('cli.Exit', source)
@_migrator
def _migrate_bool_t_flag(source):
return _BOOL_T_FLAG_RE.sub(_bool_t_flag_repl, source)
def _bool_t_flag_repl(match):
return 'cli.BoolFlag{{Value: true, {}}}'.format(
match.groupdict()['args']
)
def test_migrators():
import difflib
@ -235,6 +255,30 @@ _MIGRATOR_TESTS = (
\t"gopkg.in/urfave/cli.v2"
\t"gopkg.in/urfave/cli.v2"
\t"gopkg.in/urfave/cli.v2"
"""),
("""
\t\t\t\treturn cli.NewExitError("foo whatebber", 9)
""", """
\t\t\t\treturn cli.Exit("foo whatebber", 9)
"""),
("""
\t\t\tapp.Flags = []cli.Flag{
\t\t\t\tcli.StringFlag{
\t\t\t\t\tName: "aha",
\t\t\t\t},
\t\t\t\tcli.BoolTFlag{
\t\t\t\t\tName: "blurp",
\t\t\t\t},
\t\t\t}
""", """
\t\t\tapp.Flags = []cli.Flag{
\t\t\t\t&cli.StringFlag{
\t\t\t\t\tName: "aha",
\t\t\t\t},
\t\t\t\t&cli.BoolFlag{Value: true,
\t\t\t\t\tName: "blurp",
\t\t\t\t},
\t\t\t}
""")
)

View File

@ -88,7 +88,8 @@ def _migrations():
_run(['python', migration_script, '-w', gofile])
_run('go build -o tmp.out {}'.format(gofile).split())
finally:
shutil.rmtree(tmpdir, ignore_errors=True)
if os.environ.get('NOCLEAN', '') == '':
shutil.rmtree(tmpdir, ignore_errors=True)
def _run(command):