-
Notifications
You must be signed in to change notification settings - Fork 240
/
move.go
77 lines (60 loc) · 1.95 KB
/
move.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package cmd
import (
"fmt"
"os"
"github.com/superfly/flyctl/cmdctx"
"github.com/AlecAivazis/survey/v2"
"github.com/logrusorgru/aurora"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/docstrings"
)
//TODO: Move all output to status styled begin/done updates
func newMoveCommand() *Command {
moveStrings := docstrings.Get("move")
moveCmd := BuildCommandKS(nil, runMove, moveStrings, os.Stdout, requireSession)
moveCmd.Args = cobra.ExactArgs(1)
// TODO: Move flag descriptions into the docStrings
moveCmd.AddBoolFlag(BoolFlagOpts{Name: "yes", Shorthand: "y", Description: "Accept all confirmations"})
moveCmd.AddStringFlag(StringFlagOpts{
Name: "org",
Description: `The organization to move the app to`,
})
return moveCmd
}
func runMove(commandContext *cmdctx.CmdContext) error {
appName := commandContext.Args[0]
app, err := commandContext.Client.API().GetApp(appName)
if err != nil {
return errors.Wrap(err, "Error fetching app")
}
commandContext.Statusf("move", cmdctx.SINFO, "App '%s' is currently in organization '%s'\n", app.Name, app.Organization.Slug)
targetOrgSlug, _ := commandContext.Config.GetString("org")
org, err := selectOrganization(commandContext.Client.API(), targetOrgSlug)
switch {
case isInterrupt(err):
return nil
case err != nil || org == nil:
return fmt.Errorf("Error setting organization: %s", err)
}
if !commandContext.Config.GetBool("yes") {
fmt.Println(aurora.Red("Are you sure you want to move this app?"))
confirm := false
prompt := &survey.Confirm{
Message: fmt.Sprintf("Move %s from %s to %s?", appName, app.Organization.Slug, org.Slug),
}
err = survey.AskOne(prompt, &confirm)
if err != nil {
return err
}
if !confirm {
return nil
}
}
_, err = commandContext.Client.API().MoveApp(appName, org.ID)
if err != nil {
return errors.WithMessage(err, "Failed to move app")
}
fmt.Printf("Successfully moved %s to %s\n", appName, org.Slug)
return nil
}