From ce2a441298d21c2587868f8ec6906f02159effaf Mon Sep 17 00:00:00 2001 From: "Calvin A. Allen" Date: Sat, 13 Dec 2025 18:43:51 -0500 Subject: [PATCH] feat(init): add --yes flag to skip confirmation prompts Add -y/--yes flag to the init command that skips interactive confirmation prompts when configuring PATH. This allows non-interactive usage in CI environments where integration tests call 'dtvem init --yes'. - Add initYes flag variable and register with cobra - Update AddToPath signature to accept skipConfirmation parameter - Skip prompts on Unix when flag is set (shell config modification) - Skip prompts on Windows when flag is set (elevation request) Fixes #132 --- src/cmd/init.go | 5 +++- src/internal/path/path_unix.go | 39 +++++++++++++++++-------------- src/internal/path/path_windows.go | 27 +++++++++++---------- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/cmd/init.go b/src/cmd/init.go index 0ee3d8b..573d934 100644 --- a/src/cmd/init.go +++ b/src/cmd/init.go @@ -7,6 +7,8 @@ import ( "github.com/spf13/cobra" ) +var initYes bool + var initCmd = &cobra.Command{ Use: "init", Short: "Initialize dtvem (setup directories and PATH)", @@ -38,7 +40,7 @@ Example: // Setup PATH - AddToPath handles checking position and moving if needed shimsDir := path.ShimsDir() - if err := path.AddToPath(shimsDir); err != nil { + if err := path.AddToPath(shimsDir, initYes); err != nil { ui.Error("Failed to configure PATH: %v", err) ui.Info("You can manually add %s to your PATH", shimsDir) return @@ -53,5 +55,6 @@ Example: } func init() { + initCmd.Flags().BoolVarP(&initYes, "yes", "y", false, "Skip confirmation prompts") rootCmd.AddCommand(initCmd) } diff --git a/src/internal/path/path_unix.go b/src/internal/path/path_unix.go index 9fdb224..94b76f7 100644 --- a/src/internal/path/path_unix.go +++ b/src/internal/path/path_unix.go @@ -52,8 +52,9 @@ func GetShellConfigFile(shell string) string { } } -// AddToPath adds the shims directory to the user's PATH by modifying their shell config -func AddToPath(shimsDir string) error { +// AddToPath adds the shims directory to the user's PATH by modifying their shell config. +// If skipConfirmation is true, the function will proceed without prompting the user. +func AddToPath(shimsDir string, skipConfirmation bool) error { shell := DetectShell() if shell == "unknown" { return fmt.Errorf("could not detect shell - please add %s to your PATH manually", shimsDir) @@ -85,22 +86,24 @@ func AddToPath(shimsDir string) error { exportLine = fmt.Sprintf("\n# Added by dtvem\nexport PATH=\"%s:$PATH\"\n", shimsDir) } - // Prompt user for confirmation - ui.Header("PATH Setup Required") - ui.Info("dtvem needs to add the shims directory to your PATH") - ui.Info("Shell: %s", ui.Highlight(shell)) - ui.Info("Config file: %s", ui.Highlight(configFile)) - ui.Info("Will append: %s", ui.Highlight(strings.TrimSpace(exportLine))) - fmt.Printf("\nProceed? [Y/n]: ") - - var response string - _, _ = fmt.Scanln(&response) - response = strings.ToLower(strings.TrimSpace(response)) - - if response != "" && response != constants.ResponseY && response != constants.ResponseYes { - ui.Warning("PATH not modified. Please add this manually to your %s:", configFile) - ui.Info("%s", strings.TrimSpace(exportLine)) - return nil + // Prompt user for confirmation (unless skipConfirmation is true) + if !skipConfirmation { + ui.Header("PATH Setup Required") + ui.Info("dtvem needs to add the shims directory to your PATH") + ui.Info("Shell: %s", ui.Highlight(shell)) + ui.Info("Config file: %s", ui.Highlight(configFile)) + ui.Info("Will append: %s", ui.Highlight(strings.TrimSpace(exportLine))) + fmt.Printf("\nProceed? [Y/n]: ") + + var response string + _, _ = fmt.Scanln(&response) + response = strings.ToLower(strings.TrimSpace(response)) + + if response != "" && response != constants.ResponseY && response != constants.ResponseYes { + ui.Warning("PATH not modified. Please add this manually to your %s:", configFile) + ui.Info("%s", strings.TrimSpace(exportLine)) + return nil + } } // Ensure the directory exists for fish config diff --git a/src/internal/path/path_windows.go b/src/internal/path/path_windows.go index 63244f7..895fe2c 100644 --- a/src/internal/path/path_windows.go +++ b/src/internal/path/path_windows.go @@ -33,8 +33,8 @@ const ( // AddToPath adds the shims directory to the System PATH on Windows. // This requires administrator privileges. If not elevated, it will prompt -// the user to re-run with elevation. -func AddToPath(shimsDir string) error { +// the user to re-run with elevation (unless skipConfirmation is true). +func AddToPath(shimsDir string, skipConfirmation bool) error { // Check current System PATH status needsUpdate, action, err := checkSystemPath(shimsDir) if err != nil { @@ -48,7 +48,7 @@ func AddToPath(shimsDir string) error { // Check if we have admin privileges if !isAdmin() { - return promptForElevation(shimsDir, action) + return promptForElevation(shimsDir, action, skipConfirmation) } // We have admin privileges - proceed with modification @@ -97,8 +97,9 @@ func isAdmin() bool { return true } -// promptForElevation prompts the user to re-run dtvem init with admin privileges -func promptForElevation(shimsDir, action string) error { +// promptForElevation prompts the user to re-run dtvem init with admin privileges. +// If skipConfirmation is true, it will automatically re-launch with elevation. +func promptForElevation(shimsDir, action string, skipConfirmation bool) error { if action == pathActionMove { ui.Header("PATH Fix Required (Administrator)") ui.Warning("%s is in your System PATH but not at the beginning", shimsDir) @@ -113,15 +114,17 @@ func promptForElevation(shimsDir, action string) error { ui.Info("On Windows, System PATH takes priority over User PATH.") ui.Info("Modifying System PATH requires administrator privileges.") - fmt.Printf("\nRe-run with administrator privileges? [Y/n]: ") + if !skipConfirmation { + fmt.Printf("\nRe-run with administrator privileges? [Y/n]: ") - var response string - _, _ = fmt.Scanln(&response) - response = strings.ToLower(strings.TrimSpace(response)) + var response string + _, _ = fmt.Scanln(&response) + response = strings.ToLower(strings.TrimSpace(response)) - if response != "" && response != constants.ResponseY && response != constants.ResponseYes { - ui.Warning("PATH not modified. You can run 'dtvem init' again later.") - return nil + if response != "" && response != constants.ResponseY && response != constants.ResponseYes { + ui.Warning("PATH not modified. You can run 'dtvem init' again later.") + return nil + } } // Re-launch with elevation