Skip to content

Commit

Permalink
Fix common.AskConfirmation handling of no input
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles-Antoine Mathieu committed Sep 28, 2020
1 parent 1da9bc6 commit c53f989
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
10 changes: 8 additions & 2 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,21 @@ func LoadConfig() (config *CliConfig, err error) {
fmt.Printf("We have been redirected to : %s\n", finalURL)
fmt.Printf("Replace current url (%s) with the new one ? [Y/n] ", config.URL)

ok, _ := common.AskConfirmation(true)
ok, err := common.AskConfirmation(true)
if err != nil {
return nil, fmt.Errorf("Unable to ask for confirmation : %s", err)
}
if ok {
config.URL = strings.TrimSuffix(finalURL, "/")
}
}

// Enable client updates ?
fmt.Println("Do you want to enable client auto update ? [Y/n] ")
ok, _ := common.AskConfirmation(true)
ok, err := common.AskConfirmation(true)
if err != nil {
return nil, fmt.Errorf("Unable to ask for confirmation : %s", err)
}
if ok {
config.AutoUpdate = true
}
Expand Down
17 changes: 13 additions & 4 deletions client/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,14 @@ func update(client *plik.Client, updateFlag bool) (err error) {
} else {
fmt.Printf("Update Plik client to match server version ? [Y/n] ")
}
if ok, _ := common.AskConfirmation(true); !ok {
if ok, err := common.AskConfirmation(true); err != nil || !ok {
if err != nil {
return fmt.Errorf("Unable to ask for confirmation : %s", err)
}
if updateFlag {
os.Exit(0)
}
return
return nil
}

// Display release notes
Expand Down Expand Up @@ -147,7 +150,10 @@ func update(client *plik.Client, updateFlag bool) (err error) {

// Ask to display the release notes
fmt.Printf("Do you want to browse the release notes of version %s ? [Y/n] ", release.Name)
if ok, _ := common.AskConfirmation(true); !ok {
if ok, err := common.AskConfirmation(true); err != nil || !ok {
if err != nil {
return fmt.Errorf("Unable to ask for confirmation : %s", err)
}
continue
}

Expand All @@ -159,7 +165,10 @@ func update(client *plik.Client, updateFlag bool) (err error) {
// Let user review the last release notes and ask to confirm update
if release.Name == newVersion {
fmt.Printf("\nUpdate Plik client from %s to %s ? [Y/n] ", currentVersion, newVersion)
if ok, _ := common.AskConfirmation(true); !ok {
if ok, err := common.AskConfirmation(true); err != nil || !ok {
if err != nil {
return fmt.Errorf("Unable to ask for confirmation : %s", err)
}
if updateFlag {
os.Exit(0)
}
Expand Down
22 changes: 17 additions & 5 deletions server/cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ func deleteFiles(cmd *cobra.Command, args []string) {

// Ask confirmation
fmt.Printf("Do you really want to remove this file %s %s ? [y/N]\n", file.ID, file.Name)
ok, _ := common.AskConfirmation(false)
ok, err := common.AskConfirmation(false)
if err != nil {
fmt.Printf("Unable to ask for confirmation : %s", err)
os.Exit(1)
}
if !ok {
os.Exit(0)
}
Expand All @@ -128,12 +132,16 @@ func deleteFiles(cmd *cobra.Command, args []string) {

// Ask confirmation
fmt.Printf("Do you really want to remove this upload %s ? [y/N]\n", fileParams.uploadID)
ok, _ := common.AskConfirmation(false)
ok, err := common.AskConfirmation(false)
if err != nil {
fmt.Printf("Unable to ask for confirmation : %s", err)
os.Exit(1)
}
if !ok {
os.Exit(0)
}

err := metadataBackend.DeleteUpload(fileParams.uploadID)
err = metadataBackend.DeleteUpload(fileParams.uploadID)
if err != nil {
fmt.Printf("Unable to get upload files : %s\n", err)
os.Exit(1)
Expand All @@ -142,15 +150,19 @@ func deleteFiles(cmd *cobra.Command, args []string) {

// Ask confirmation
fmt.Printf("Do you really want to remove ALL uploads ? [y/N]\n")
ok, _ := common.AskConfirmation(false)
ok, err := common.AskConfirmation(false)
if err != nil {
fmt.Printf("Unable to ask for confirmation : %s", err)
os.Exit(1)
}
if !ok {
os.Exit(0)
}

deleteUpload := func(upload *common.Upload) error {
return metadataBackend.DeleteUpload(upload.ID)
}
err := metadataBackend.ForEachUpload(deleteUpload)
err = metadataBackend.ForEachUpload(deleteUpload)
if err != nil {
fmt.Printf("Unable to delete uploads : %s\n", err)
os.Exit(1)
Expand Down
6 changes: 5 additions & 1 deletion server/cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,11 @@ func deleteUser(cmd *cobra.Command, args []string) {

// Ask confirmation
fmt.Printf("Do you really want to delete this user %s and all its uploads ? [y/N]\n", userID)
ok, _ := common.AskConfirmation(false)
ok, err := common.AskConfirmation(false)
if err != nil {
fmt.Printf("Unable to ask for confirmation : %s", err)
os.Exit(1)
}
if !ok {
os.Exit(0)
}
Expand Down
3 changes: 3 additions & 0 deletions server/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func AskConfirmation(defaultValue bool) (bool, error) {
var input string
_, err := fmt.Scanln(&input)
if err != nil {
if err.Error() == "unexpected newline" {
return defaultValue, nil
}
return false, err
}

Expand Down

0 comments on commit c53f989

Please sign in to comment.