Skip to content

Commit ce2a441

Browse files
committed
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
1 parent 5720dbc commit ce2a441

3 files changed

Lines changed: 40 additions & 31 deletions

File tree

src/cmd/init.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/spf13/cobra"
88
)
99

10+
var initYes bool
11+
1012
var initCmd = &cobra.Command{
1113
Use: "init",
1214
Short: "Initialize dtvem (setup directories and PATH)",
@@ -38,7 +40,7 @@ Example:
3840
// Setup PATH - AddToPath handles checking position and moving if needed
3941
shimsDir := path.ShimsDir()
4042

41-
if err := path.AddToPath(shimsDir); err != nil {
43+
if err := path.AddToPath(shimsDir, initYes); err != nil {
4244
ui.Error("Failed to configure PATH: %v", err)
4345
ui.Info("You can manually add %s to your PATH", shimsDir)
4446
return
@@ -53,5 +55,6 @@ Example:
5355
}
5456

5557
func init() {
58+
initCmd.Flags().BoolVarP(&initYes, "yes", "y", false, "Skip confirmation prompts")
5659
rootCmd.AddCommand(initCmd)
5760
}

src/internal/path/path_unix.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ func GetShellConfigFile(shell string) string {
5252
}
5353
}
5454

55-
// AddToPath adds the shims directory to the user's PATH by modifying their shell config
56-
func AddToPath(shimsDir string) error {
55+
// AddToPath adds the shims directory to the user's PATH by modifying their shell config.
56+
// If skipConfirmation is true, the function will proceed without prompting the user.
57+
func AddToPath(shimsDir string, skipConfirmation bool) error {
5758
shell := DetectShell()
5859
if shell == "unknown" {
5960
return fmt.Errorf("could not detect shell - please add %s to your PATH manually", shimsDir)
@@ -85,22 +86,24 @@ func AddToPath(shimsDir string) error {
8586
exportLine = fmt.Sprintf("\n# Added by dtvem\nexport PATH=\"%s:$PATH\"\n", shimsDir)
8687
}
8788

88-
// Prompt user for confirmation
89-
ui.Header("PATH Setup Required")
90-
ui.Info("dtvem needs to add the shims directory to your PATH")
91-
ui.Info("Shell: %s", ui.Highlight(shell))
92-
ui.Info("Config file: %s", ui.Highlight(configFile))
93-
ui.Info("Will append: %s", ui.Highlight(strings.TrimSpace(exportLine)))
94-
fmt.Printf("\nProceed? [Y/n]: ")
95-
96-
var response string
97-
_, _ = fmt.Scanln(&response)
98-
response = strings.ToLower(strings.TrimSpace(response))
99-
100-
if response != "" && response != constants.ResponseY && response != constants.ResponseYes {
101-
ui.Warning("PATH not modified. Please add this manually to your %s:", configFile)
102-
ui.Info("%s", strings.TrimSpace(exportLine))
103-
return nil
89+
// Prompt user for confirmation (unless skipConfirmation is true)
90+
if !skipConfirmation {
91+
ui.Header("PATH Setup Required")
92+
ui.Info("dtvem needs to add the shims directory to your PATH")
93+
ui.Info("Shell: %s", ui.Highlight(shell))
94+
ui.Info("Config file: %s", ui.Highlight(configFile))
95+
ui.Info("Will append: %s", ui.Highlight(strings.TrimSpace(exportLine)))
96+
fmt.Printf("\nProceed? [Y/n]: ")
97+
98+
var response string
99+
_, _ = fmt.Scanln(&response)
100+
response = strings.ToLower(strings.TrimSpace(response))
101+
102+
if response != "" && response != constants.ResponseY && response != constants.ResponseYes {
103+
ui.Warning("PATH not modified. Please add this manually to your %s:", configFile)
104+
ui.Info("%s", strings.TrimSpace(exportLine))
105+
return nil
106+
}
104107
}
105108

106109
// Ensure the directory exists for fish config

src/internal/path/path_windows.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ const (
3333

3434
// AddToPath adds the shims directory to the System PATH on Windows.
3535
// This requires administrator privileges. If not elevated, it will prompt
36-
// the user to re-run with elevation.
37-
func AddToPath(shimsDir string) error {
36+
// the user to re-run with elevation (unless skipConfirmation is true).
37+
func AddToPath(shimsDir string, skipConfirmation bool) error {
3838
// Check current System PATH status
3939
needsUpdate, action, err := checkSystemPath(shimsDir)
4040
if err != nil {
@@ -48,7 +48,7 @@ func AddToPath(shimsDir string) error {
4848

4949
// Check if we have admin privileges
5050
if !isAdmin() {
51-
return promptForElevation(shimsDir, action)
51+
return promptForElevation(shimsDir, action, skipConfirmation)
5252
}
5353

5454
// We have admin privileges - proceed with modification
@@ -97,8 +97,9 @@ func isAdmin() bool {
9797
return true
9898
}
9999

100-
// promptForElevation prompts the user to re-run dtvem init with admin privileges
101-
func promptForElevation(shimsDir, action string) error {
100+
// promptForElevation prompts the user to re-run dtvem init with admin privileges.
101+
// If skipConfirmation is true, it will automatically re-launch with elevation.
102+
func promptForElevation(shimsDir, action string, skipConfirmation bool) error {
102103
if action == pathActionMove {
103104
ui.Header("PATH Fix Required (Administrator)")
104105
ui.Warning("%s is in your System PATH but not at the beginning", shimsDir)
@@ -113,15 +114,17 @@ func promptForElevation(shimsDir, action string) error {
113114
ui.Info("On Windows, System PATH takes priority over User PATH.")
114115
ui.Info("Modifying System PATH requires administrator privileges.")
115116

116-
fmt.Printf("\nRe-run with administrator privileges? [Y/n]: ")
117+
if !skipConfirmation {
118+
fmt.Printf("\nRe-run with administrator privileges? [Y/n]: ")
117119

118-
var response string
119-
_, _ = fmt.Scanln(&response)
120-
response = strings.ToLower(strings.TrimSpace(response))
120+
var response string
121+
_, _ = fmt.Scanln(&response)
122+
response = strings.ToLower(strings.TrimSpace(response))
121123

122-
if response != "" && response != constants.ResponseY && response != constants.ResponseYes {
123-
ui.Warning("PATH not modified. You can run 'dtvem init' again later.")
124-
return nil
124+
if response != "" && response != constants.ResponseY && response != constants.ResponseYes {
125+
ui.Warning("PATH not modified. You can run 'dtvem init' again later.")
126+
return nil
127+
}
125128
}
126129

127130
// Re-launch with elevation

0 commit comments

Comments
 (0)