Skip to content

Commit db9bcbe

Browse files
emosbaughscrespod
andauthored
fix: parse flags before prompting, do not prompt in flag parsing code (#2400)
* fix: parse flags before prompting, do not prompt in flag parsing code * f --------- Co-authored-by: Steven Crespo <screswk@gmail.com>
1 parent 6d647b0 commit db9bcbe

File tree

3 files changed

+23
-30
lines changed

3 files changed

+23
-30
lines changed

cmd/installer/cli/install.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ func InstallCmd(ctx context.Context, appSlug, appTitle string) *cobra.Command {
122122
cancel() // Cancel context when command completes
123123
},
124124
RunE: func(cmd *cobra.Command, args []string) error {
125-
if err := verifyAndPrompt(ctx, appSlug, flags, prompts.New()); err != nil {
125+
if err := preRunInstall(cmd, &flags, rc, ki); err != nil {
126126
return err
127127
}
128-
if err := preRunInstall(cmd, &flags, rc, ki); err != nil {
128+
if err := verifyAndPrompt(ctx, cmd, appSlug, &flags, prompts.New()); err != nil {
129129
return err
130130
}
131131

@@ -435,13 +435,6 @@ func preRunInstallCommon(cmd *cobra.Command, flags *InstallCmdFlags, rc runtimec
435435
return err
436436
}
437437

438-
// restore command doesn't have a password flag
439-
if cmd.Flags().Lookup("admin-console-password") != nil {
440-
if err := ensureAdminConsolePassword(flags); err != nil {
441-
return err
442-
}
443-
}
444-
445438
rc.SetAdminConsolePort(flags.adminConsolePort)
446439
ki.SetAdminConsolePort(flags.adminConsolePort)
447440

@@ -812,7 +805,7 @@ func getAddonInstallOpts(flags InstallCmdFlags, rc runtimeconfig.RuntimeConfig,
812805
return opts, nil
813806
}
814807

815-
func verifyAndPrompt(ctx context.Context, appSlug string, flags InstallCmdFlags, prompt prompts.Prompt) error {
808+
func verifyAndPrompt(ctx context.Context, cmd *cobra.Command, appSlug string, flags *InstallCmdFlags, prompt prompts.Prompt) error {
816809
logrus.Debugf("checking if k0s is already installed")
817810
err := verifyNoInstallation(appSlug, "reinstall")
818811
if err != nil {
@@ -851,6 +844,13 @@ func verifyAndPrompt(ctx context.Context, appSlug string, flags InstallCmdFlags,
851844
return err
852845
}
853846

847+
// restore command doesn't have a password flag
848+
if cmd.Flags().Lookup("admin-console-password") != nil {
849+
if err := ensureAdminConsolePassword(flags); err != nil {
850+
return err
851+
}
852+
}
853+
854854
return nil
855855
}
856856

cmd/installer/cli/install_runpreflights.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ func InstallRunPreflightsCmd(ctx context.Context, appSlug string) *cobra.Command
3232
Use: "run-preflights",
3333
Short: "Run install host preflights",
3434
Hidden: true,
35-
PreRunE: func(cmd *cobra.Command, args []string) error {
35+
PostRun: func(cmd *cobra.Command, args []string) {
36+
rc.Cleanup()
37+
},
38+
RunE: func(cmd *cobra.Command, args []string) error {
3639
if err := preRunInstall(cmd, &flags, rc, ki); err != nil {
3740
return err
3841
}
42+
if err := verifyAndPrompt(ctx, cmd, appSlug, &flags, prompts.New()); err != nil {
43+
return err
44+
}
3945

4046
_ = rc.SetEnv()
4147

42-
return nil
43-
},
44-
PostRun: func(cmd *cobra.Command, args []string) {
45-
rc.Cleanup()
46-
},
47-
RunE: func(cmd *cobra.Command, args []string) error {
48-
if err := runInstallRunPreflights(cmd.Context(), appSlug, flags, rc); err != nil {
48+
if err := runInstallRunPreflights(cmd.Context(), flags, rc); err != nil {
4949
return err
5050
}
5151

@@ -62,11 +62,7 @@ func InstallRunPreflightsCmd(ctx context.Context, appSlug string) *cobra.Command
6262
return cmd
6363
}
6464

65-
func runInstallRunPreflights(ctx context.Context, appSlug string, flags InstallCmdFlags, rc runtimeconfig.RuntimeConfig) error {
66-
if err := verifyAndPrompt(ctx, appSlug, flags, prompts.New()); err != nil {
67-
return err
68-
}
69-
65+
func runInstallRunPreflights(ctx context.Context, flags InstallCmdFlags, rc runtimeconfig.RuntimeConfig) error {
7066
licenseBytes, err := os.ReadFile(flags.licenseFile)
7167
if err != nil {
7268
return fmt.Errorf("unable to read license file: %w", err)

cmd/installer/cli/restore.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,16 @@ func RestoreCmd(ctx context.Context, appSlug, appTitle string) *cobra.Command {
9898
cmd := &cobra.Command{
9999
Use: "restore",
100100
Short: fmt.Sprintf("Restore %s from a backup", appTitle),
101-
PreRunE: func(cmd *cobra.Command, args []string) error {
101+
PostRun: func(cmd *cobra.Command, args []string) {
102+
rc.Cleanup()
103+
},
104+
RunE: func(cmd *cobra.Command, args []string) error {
102105
if err := preRunInstall(cmd, &flags, rc, ki); err != nil {
103106
return err
104107
}
105108

106109
_ = rc.SetEnv()
107110

108-
return nil
109-
},
110-
PostRun: func(cmd *cobra.Command, args []string) {
111-
rc.Cleanup()
112-
},
113-
RunE: func(cmd *cobra.Command, args []string) error {
114111
if err := runRestore(cmd.Context(), appSlug, appTitle, flags, rc, s3Store, skipStoreValidation); err != nil {
115112
return err
116113
}

0 commit comments

Comments
 (0)