Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down Expand Up @@ -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
Expand All @@ -53,5 +55,6 @@ Example:
}

func init() {
initCmd.Flags().BoolVarP(&initYes, "yes", "y", false, "Skip confirmation prompts")
rootCmd.AddCommand(initCmd)
}
39 changes: 21 additions & 18 deletions src/internal/path/path_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
27 changes: 15 additions & 12 deletions src/internal/path/path_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down