From 057c418be4bc883789cbf2517a4f32780b896289 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 11 Mar 2020 01:19:01 -0400 Subject: [PATCH] feat: implement :clone and :rename --- plugins/apps/functions.go | 10 +++ plugins/apps/src/subcommands/subcommands.go | 7 +- plugins/apps/subcommands.go | 94 +++++++++++++++++++-- 3 files changed, 104 insertions(+), 7 deletions(-) diff --git a/plugins/apps/functions.go b/plugins/apps/functions.go index f171b6eb9..adc7e960d 100644 --- a/plugins/apps/functions.go +++ b/plugins/apps/functions.go @@ -96,6 +96,16 @@ func getAppName(args []string) (appName string, err error) { return } +func getNewAppName(args []string) (appName string, err error) { + if len(args) >= 2 { + appName = args[1] + } else { + err = errors.New("Please specify an new app name") + } + + return +} + func listImagesByAppLabel(appName string) ([]string, error) { command := []string{ common.DockerBin(), diff --git a/plugins/apps/src/subcommands/subcommands.go b/plugins/apps/src/subcommands/subcommands.go index 8a628ee10..17cd59c60 100644 --- a/plugins/apps/src/subcommands/subcommands.go +++ b/plugins/apps/src/subcommands/subcommands.go @@ -19,8 +19,10 @@ func main() { switch subcommand { case "clone": args := flag.NewFlagSet("apps:clone", flag.ExitOnError) + skipDeploy := args.Bool("skip-deploy", false, "--skip-deploy: skip deploy of the new app") + ignoreExisting := args.Bool("ignore-existing", false, "--ignore-existing: exit 0 if new app already exists") args.Parse(os.Args[2:]) - err = apps.CommandClone(args.Args()) + err = apps.CommandClone(args.Args(), *skipDeploy, *ignoreExisting) case "create": args := flag.NewFlagSet("apps:create", flag.ExitOnError) args.Parse(os.Args[2:]) @@ -47,8 +49,9 @@ func main() { err = apps.CommandLocked(args.Args()) case "rename": args := flag.NewFlagSet("apps:rename", flag.ExitOnError) + skipDeploy := args.Bool("skip-deploy", false, "--skip-deploy: skip deploy of the new app") args.Parse(os.Args[2:]) - err = apps.CommandRename(args.Args()) + err = apps.CommandRename(args.Args(), *skipDeploy) case "report": args := flag.NewFlagSet("apps:report", flag.ExitOnError) args.Parse(os.Args[2:]) diff --git a/plugins/apps/subcommands.go b/plugins/apps/subcommands.go index ff58babd8..e3d8d9ded 100644 --- a/plugins/apps/subcommands.go +++ b/plugins/apps/subcommands.go @@ -10,13 +10,55 @@ import ( ) // CommandClone clones an app -func CommandClone(args []string) error { - appName, err := getAppName(args) +func CommandClone(args []string, skipDeploy bool, ignoreExisting bool) error { + oldAppName, err := getAppName(args) if err != nil { return err } - fmt.Println(appName) + newAppName, err := getNewAppName(args) + if err != nil { + return err + } + + if err = common.IsValidAppName(oldAppName); err != nil { + return err + } + + if err = common.IsValidAppName(newAppName); err != nil { + return err + } + + if err = appExists(oldAppName); err != nil { + return err + } + + if err = appExists(newAppName); err == nil { + if ignoreExisting { + common.LogWarn("Name is already taken") + return nil + } + + return errors.New("Name is already taken") + } + + common.LogInfo1Quiet(fmt.Sprintf("Cloning %s to %s", oldAppName, newAppName)) + if err = createApp(newAppName); err != nil { + return err + } + + if err = PlugnTrigger("post-app-clone-setup", []string{oldAppName, newAppName}...); err != nil { + return err + } + + if skipDeploy { + os.Setenv("SKIP_REBUILD", "true") + } + + if err = PlugnTrigger("post-app-clone", []string{oldAppName, newAppName}...); err != nil { + return err + } + return nil } @@ -116,13 +158,55 @@ func CommandLocked(args []string) error { } // CommandRename renames an app -func CommandRename(args []string) error { +func CommandRename(args []string, skipDeploy bool) error { appName, err := getAppName(args) if err != nil { return err } - fmt.Println(appName) + newAppName, err := getNewAppName(args) + if err != nil { + return err + } + + if err = common.IsValidAppName(oldAppName); err != nil { + return err + } + + if err = common.IsValidAppName(newAppName); err != nil { + return err + } + + if err = appExists(oldAppName); err != nil { + return err + } + + if err = appExists(newAppName); err == nil { + return errors.New("Name is already taken") + } + + common.LogInfo1Quiet(fmt.Sprintf("Renaming %s to %s", oldAppName, newAppName)) + if err = createApp(newAppName); err != nil { + return err + } + + if err = PlugnTrigger("post-app-rename-setup", []string{oldAppName, newAppName}...); err != nil { + return err + } + + os.Setenv("DOKKU_APPS_FORCE_DELETE", "1") + if err = destroyApp(appName); err != nil { + return err + } + + if skipDeploy { + os.Setenv("SKIP_REBUILD", "true") + } + + if err = PlugnTrigger("post-app-rename", []string{oldAppName, newAppName}...); err != nil { + return err + } + return nil }