Do not allow to overwrite campaign file on init

This commit is contained in:
Miguel de la Cruz 2020-10-07 19:31:17 +02:00
parent 37b2300907
commit 27965460d4
2 changed files with 13 additions and 2 deletions

View file

@ -50,6 +50,15 @@ func InitCmd() *cobra.Command {
} }
func initCmdF(cmd *cobra.Command, _ []string) { func initCmdF(cmd *cobra.Command, _ []string) {
campaignPath, _ := cmd.Flags().GetString("campaign")
_, err := os.Stat(campaignPath)
if err == nil {
ErrorAndExit(cmd, fmt.Errorf("cannot use %s as campaign file: file already exists", campaignPath))
} else if !os.IsNotExist(err) {
ErrorAndExit(cmd, fmt.Errorf("cannot use %s as campaign file: %w", campaignPath, err))
}
getStringFlagOrAskIfEmpty := func(name string, question string) string { getStringFlagOrAskIfEmpty := func(name string, question string) string {
val, _ := cmd.Flags().GetString(name) val, _ := cmd.Flags().GetString(name)
if val == "" { if val == "" {
@ -96,7 +105,7 @@ func initCmdF(cmd *cobra.Command, _ []string) {
IssueTemplate: issueTemplate, IssueTemplate: issueTemplate,
FooterTemplate: footerTemplate, FooterTemplate: footerTemplate,
} }
if err := app.SaveCampaign(campaign, "./campaign.json"); err != nil { if err := app.SaveCampaign(campaign, campaignPath); err != nil {
ErrorAndExit(cmd, err) ErrorAndExit(cmd, err)
} }
} }

View file

@ -25,7 +25,9 @@ func withApp(f func(*app.App, *cobra.Command, []string)) func(*cobra.Command, []
func withAppE(f func(*app.App, *cobra.Command, []string) error) func(*cobra.Command, []string) error { func withAppE(f func(*app.App, *cobra.Command, []string) error) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error {
a, err := app.NewApp("./campaign.json") campaignPath, _ := cmd.Flags().GetString("campaign")
a, err := app.NewApp(campaignPath)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "ERROR: "+err.Error()) fmt.Fprintln(os.Stderr, "ERROR: "+err.Error())
os.Exit(1) os.Exit(1)