Skip to content

Commit

Permalink
change overwrite protection to be proactive
Browse files Browse the repository at this point in the history
Fixes #168
  • Loading branch information
codepope committed Jun 25, 2020
1 parent 988a905 commit 691eb66
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
11 changes: 11 additions & 0 deletions cmd/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/superfly/flyctl/cmd/presenters"
"github.com/superfly/flyctl/docstrings"
"github.com/superfly/flyctl/flyctl"
"github.com/superfly/flyctl/helpers"
)

//TODO: Move all output to status styled begin/done updates
Expand Down Expand Up @@ -203,6 +204,16 @@ func runAppsCreate(commandContext *cmdctx.CmdContext) error {
}
}

configfilename, err := flyctl.ResolveConfigFileFromPath(commandContext.WorkingDir)

if helpers.FileExists(configfilename) {
commandContext.Status("create", cmdctx.SERROR, "An existing configuration file has been found.")
confirmation := confirm(fmt.Sprintf("Overwrite file '%s'", configfilename))
if !confirmation {
return nil
}
}

newAppConfig := flyctl.NewAppConfig()

if builder, _ := commandContext.Config.GetString("builder"); builder != "" {
Expand Down
16 changes: 12 additions & 4 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cmd
import (
"errors"
"fmt"
"github.com/superfly/flyctl/cmdctx"
"os"

"github.com/superfly/flyctl/cmdctx"

"github.com/superfly/flyctl/docstrings"

"github.com/logrusorgru/aurora"
Expand Down Expand Up @@ -53,6 +54,16 @@ func runDisplayConfig(ctx *cmdctx.CmdContext) error {
}

func runSaveConfig(ctx *cmdctx.CmdContext) error {
configfilename, err := flyctl.ResolveConfigFileFromPath(ctx.WorkingDir)

if helpers.FileExists(configfilename) {
ctx.Status("create", cmdctx.SERROR, "An existing configuration file has been found.")
confirmation := confirm(fmt.Sprintf("Overwrite file '%s'", configfilename))
if !confirmation {
return nil
}
}

if ctx.AppConfig == nil {
ctx.AppConfig = flyctl.NewAppConfig()
}
Expand Down Expand Up @@ -99,9 +110,6 @@ func printAppConfigErrors(cfg api.AppConfig) {
}

func writeAppConfig(path string, appConfig *flyctl.AppConfig) error {
if !confirmFileOverwrite(path) {
return nil
}

if err := appConfig.WriteToFile(path); err != nil {
return err
Expand Down

0 comments on commit 691eb66

Please sign in to comment.