Skip to content

Commit

Permalink
Merge pull request #176 from superfly/codepope/issue175
Browse files Browse the repository at this point in the history
Set pause, resume, restart to use requireAppNameAsArg
  • Loading branch information
codepope committed Jun 26, 2020
2 parents 530d0d7 + eecb11e commit 37a470d
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 12 deletions.
4 changes: 2 additions & 2 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ type CheckState struct {
type Region struct {
Code string
Name string
Latitude float32 `json:"omitempty"`
Longitude float32 `json:"omitempty"`
Latitude float32
Longitude float32
}

type AutoscalingConfig struct {
Expand Down
20 changes: 15 additions & 5 deletions cmd/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,16 @@ func newAppListCommand() *Command {
})

appsPauseStrings := docstrings.Get("apps.pause")
BuildCommand(cmd, runAppsPause, appsPauseStrings.Usage, appsPauseStrings.Short, appsPauseStrings.Long, os.Stdout, requireSession, requireAppName)
appsPauseCmd := BuildCommand(cmd, runAppsPause, appsPauseStrings.Usage, appsPauseStrings.Short, appsPauseStrings.Long, os.Stdout, requireSession, requireAppNameAsArg)
appsPauseCmd.Args = cobra.RangeArgs(0, 1)

appsResumeStrings := docstrings.Get("apps.resume")
BuildCommand(cmd, runAppsResume, appsResumeStrings.Usage, appsResumeStrings.Short, appsResumeStrings.Long, os.Stdout, requireSession, requireAppName)
appsResumeCmd := BuildCommand(cmd, runAppsResume, appsResumeStrings.Usage, appsResumeStrings.Short, appsResumeStrings.Long, os.Stdout, requireSession, requireAppNameAsArg)
appsResumeCmd.Args = cobra.RangeArgs(0, 1)

appsRestartStrings := docstrings.Get("apps.restart")
BuildCommand(cmd, runAppsRestart, appsRestartStrings.Usage, appsRestartStrings.Short, appsRestartStrings.Long, os.Stdout, requireSession, requireAppName)
appsRestartCmd := BuildCommand(cmd, runAppsRestart, appsRestartStrings.Usage, appsRestartStrings.Short, appsRestartStrings.Long, os.Stdout, requireSession, requireAppNameAsArg)
appsRestartCmd.Args = cobra.RangeArgs(0, 1)

return cmd
}
Expand All @@ -103,12 +106,19 @@ func runAppsList(ctx *cmdctx.CmdContext) error {
}

func runAppsPause(ctx *cmdctx.CmdContext) error {
_, err := ctx.Client.API().PauseApp(ctx.AppName)
// appName := ctx.Args[0]
// fmt.Println(appName, len(ctx.Args))
// if appName == "" {
// appName = ctx.AppName
// }
appName := ctx.AppName

_, err := ctx.Client.API().PauseApp(appName)
if err != nil {
return err
}

appstatus, err := ctx.Client.API().GetAppStatus(ctx.AppName, false)
appstatus, err := ctx.Client.API().GetAppStatus(appName, false)

fmt.Printf("%s is now %s\n", appstatus.Name, appstatus.Status)

Expand Down
79 changes: 79 additions & 0 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,85 @@ func requireAppName(cmd *Command) Initializer {
}
}

func requireAppNameAsArg(cmd *Command) Initializer {
// TODO: Add Flags to docStrings

cmd.AddStringFlag(StringFlagOpts{
Name: "config",
Shorthand: "c",
Description: "Path to an app config file or directory containing one",
Default: defaultConfigFilePath,
EnvName: "FLY_APP_CONFIG",
})

return Initializer{
Setup: func(ctx *cmdctx.CmdContext) error {
// resolve the config file path
configPath, _ := ctx.Config.GetString("config")
if configPath == "" {
configPath = defaultConfigFilePath
}
if !filepath.IsAbs(configPath) {
absConfigPath, err := filepath.Abs(filepath.Join(ctx.WorkingDir, configPath))
if err != nil {
return err
}
configPath = absConfigPath
}
resolvedPath, err := flyctl.ResolveConfigFileFromPath(configPath)
if err != nil {
return err
}
ctx.ConfigFile = resolvedPath

// load the config file if it exists
if helpers.FileExists(ctx.ConfigFile) {
terminal.Debug("Loading app config from", ctx.ConfigFile)
appConfig, err := flyctl.LoadAppConfig(ctx.ConfigFile)
if err != nil {
return err
}
ctx.AppConfig = appConfig
} else {
ctx.AppConfig = flyctl.NewAppConfig()
}

// set the app name if provided
appName, _ := ctx.Config.GetString("app")
if appName != "" {
ctx.AppName = appName
} else if ctx.AppConfig != nil {
ctx.AppName = ctx.AppConfig.AppName
}

return nil
},
PreRun: func(ctx *cmdctx.CmdContext) error {
if len(ctx.Args) > 0 {
ctx.AppName = ctx.Args[0]
}

if ctx.AppName == "" {
return fmt.Errorf("No app specified")
}

if ctx.AppConfig == nil {
return nil
}

if ctx.AppConfig.AppName != "" && ctx.AppConfig.AppName != ctx.AppName {
terminal.Warnf("app flag '%s' does not match app name in config file '%s'\n", ctx.AppName, ctx.AppConfig.AppName)

if !confirm(fmt.Sprintf("Continue using '%s'", ctx.AppName)) {
return ErrAbort
}
}

return nil
},
}
}

func workingDirectoryFromArg(index int) func(*Command) Initializer {
return func(cmd *Command) Initializer {
return Initializer{
Expand Down
8 changes: 4 additions & 4 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func runList(ctx *cmdctx.CmdContext) error {
return nil
}

type CondensedApp struct {
type appCondensed struct {
ID string
Name string
Status string
Expand Down Expand Up @@ -78,9 +78,9 @@ func runListApps(commandContext *cmdctx.CmdContext) error {
return err
}

var filteredApps []CondensedApp
var filteredApps []appCondensed

filteredApps = make([]CondensedApp, 0)
filteredApps = make([]appCondensed, 0)

for _, app := range apps {
saved := false
Expand All @@ -100,7 +100,7 @@ func runListApps(commandContext *cmdctx.CmdContext) error {
}

if saved {
filteredApps = append(filteredApps, CondensedApp{ID: app.ID, Name: app.Name, Status: app.Status, Deployed: app.Deployed, Hostname: app.Hostname, Organization: app.Organization.Slug})
filteredApps = append(filteredApps, appCondensed{ID: app.ID, Name: app.Name, Status: app.Status, Deployed: app.Deployed, Hostname: app.Hostname, Organization: app.Organization.Slug})
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func init() {

rootCmd.PersistentFlags().Bool("gqlerrorlogging", false, "Log GraphQL errors directly to stdout")

rootCmd.Flags().MarkHidden("gqlerrorlogging")
rootCmd.PersistentFlags().MarkHidden("gqlerrorlogging")

rootCmd.AddCommand(
newAuthCommand(),
Expand Down

0 comments on commit 37a470d

Please sign in to comment.