Skip to content

Commit

Permalink
fix: better UX around --backup
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Sep 4, 2023
1 parent 25acb33 commit fd69944
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
30 changes: 24 additions & 6 deletions cmd/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,24 @@ var defaultCmd = &cobra.Command{

fmt.Println(command.PorgASCIIArt)

backup, _ := cmd.Flags().GetBool("backup")
yes, _ := cmd.Flags().GetBool("yes")
yesbackup, _ := cmd.Flags().GetBool("backup")
noBackup, _ := cmd.Flags().GetBool("no-backup")
yesDefault, _ := cmd.Flags().GetBool("yes")

backup := false
if yesbackup {
backup = true
} else if noBackup {
backup = false
} else {
prompt := &survey.Confirm{
Message: "Backup your current Launchpad/Dock settings?",
}
if err := survey.AskOne(prompt, &backup); err == terminal.InterruptErr {
log.Warn("Exiting...")
return nil
}
}

conf := &command.Config{
Cmd: cmd.Use,
Expand All @@ -61,22 +77,22 @@ var defaultCmd = &cobra.Command{
return err
}

if backup {
if conf.Backup {
log.Debug("Backing up current launchpad settings")
if err := command.SaveConfig(conf); err != nil {
return err
}
}

if !yes {
if !yesDefault {
prompt := &survey.Confirm{
Message: "Organize launchpad with default config?",
}
if err := survey.AskOne(prompt, &yes); err == terminal.InterruptErr {
if err := survey.AskOne(prompt, &yesDefault); err == terminal.InterruptErr {
log.Warn("Exiting...")
return nil
}
if !yes {
if !yesDefault {
return nil
}
}
Expand All @@ -91,6 +107,8 @@ func init() {

defaultCmd.Flags().BoolP("yes", "y", false, "Do not prompt user for confirmation")
defaultCmd.Flags().BoolP("backup", "b", false, "Backup current launchpad settings")
defaultCmd.Flags().BoolP("no-backup", "n", false, "Do NOT backup current launchpad settings")
defaultCmd.MarkFlagsMutuallyExclusive("backup", "no-backup")
defaultCmd.SetHelpFunc(func(c *cobra.Command, s []string) {
rootCmd.PersistentFlags().MarkHidden("config")
rootCmd.PersistentFlags().MarkHidden("icloud")
Expand Down
30 changes: 25 additions & 5 deletions cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,26 @@ var loadCmd = &cobra.Command{
log.SetLevel(log.DebugLevel)
}

backup, _ := cmd.Flags().GetBool("backup")
yes, _ := cmd.Flags().GetBool("yes")
fmt.Println(command.PorgASCIIArt)

yesbackup, _ := cmd.Flags().GetBool("backup")
noBackup, _ := cmd.Flags().GetBool("no-backup")
yesLoad, _ := cmd.Flags().GetBool("yes")

backup := false
if yesbackup {
backup = true
} else if noBackup {
backup = false
} else {
prompt := &survey.Confirm{
Message: "Backup your current Launchpad/Dock settings?",
}
if err := survey.AskOne(prompt, &backup); err == terminal.InterruptErr {
log.Warn("Exiting...")
return nil
}
}

conf := &command.Config{
Cmd: cmd.Use,
Expand All @@ -66,15 +84,15 @@ var loadCmd = &cobra.Command{
}
}

if !yes {
if !yesLoad {
prompt := &survey.Confirm{
Message: fmt.Sprintf("Load launchpad config '%s'?", conf.File),
}
if err := survey.AskOne(prompt, &yes); err == terminal.InterruptErr {
if err := survey.AskOne(prompt, &yesLoad); err == terminal.InterruptErr {
log.Warn("Exiting...")
return nil
}
if !yes {
if !yesLoad {
return nil
}
}
Expand All @@ -88,5 +106,7 @@ func init() {
rootCmd.AddCommand(loadCmd)

loadCmd.Flags().BoolP("backup", "b", false, "Backup current launchpad settings")
loadCmd.Flags().BoolP("no-backup", "n", false, "Do NOT backup current launchpad settings")
loadCmd.Flags().BoolP("yes", "y", false, "Do not prompt user for confirmation")
loadCmd.MarkFlagsMutuallyExclusive("backup", "no-backup")
}
4 changes: 2 additions & 2 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,9 @@ func SaveConfig(c *Config) (err error) {
}

if c.Backup {
log.Infof(bold, "successfully backed up current settings!")
log.Infof(bold, "successfully backed up current settings to: "+c.File)
} else {
log.Infof(bold, "successfully wrote: "+c.File)
log.Infof(bold, "successfully wrote settings to: "+c.File)
}

return nil
Expand Down

0 comments on commit fd69944

Please sign in to comment.