Skip to content

Commit

Permalink
Ask for confirmation for fix redis command (#4435)
Browse files Browse the repository at this point in the history
`cozy-stack fix redis` command is sensible so requiring user
confirmation before starting to rebuild redis on all instances seems the
way to go
  • Loading branch information
nono committed Jul 1, 2024
2 parents 94d55d8 + 274cbb7 commit bb19ef1
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cmd/fix.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmd

import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/mail"
Expand All @@ -22,6 +24,7 @@ import (

var (
dryRunFlag bool
forceFlag bool
withMetadataFlag bool
)

Expand Down Expand Up @@ -99,11 +102,35 @@ var redisFixer = &cobra.Command{
Use: "redis",
Short: "Rebuild scheduling data strucutures in redis",
RunE: func(cmd *cobra.Command, args []string) error {
if !forceFlag {
if err := askForConfirmation("Rebuild redis on all instances?"); err != nil {
return err
}
}
ac := newAdminClient()
return ac.RebuildRedis()
},
}

func askForConfirmation(prompt string) error {
reader := bufio.NewReader(os.Stdin)
fmt.Fprintf(os.Stdout, "%s [y/N]: ", prompt)

response, err := reader.ReadString('\n')
if err != nil {
return err
}

response = strings.ToLower(strings.TrimSpace(response))

if response != "y" && response != "yes" {
return errors.New("Aborted")
}

fmt.Println()
return nil
}

var thumbnailsFixer = &cobra.Command{
Use: "thumbnails <domain>",
Short: "Rebuild thumbnails image for images files",
Expand Down Expand Up @@ -375,6 +402,7 @@ this instance are correctly set.
func init() {
thumbnailsFixer.Flags().BoolVar(&dryRunFlag, "dry-run", false, "Dry run")
thumbnailsFixer.Flags().BoolVar(&withMetadataFlag, "with-metadata", false, "Recalculate images metadata")
redisFixer.Flags().BoolVar(&forceFlag, "force", false, "Do not ask for confirmation before fixing redis on all instances")

fixerCmdGroup.AddCommand(jobsFixer)
fixerCmdGroup.AddCommand(mimeFixerCmd)
Expand Down

0 comments on commit bb19ef1

Please sign in to comment.