|
| 1 | +package setup |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "runtime" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + pathBlockBegin = "# >>> codag PATH >>>" |
| 15 | + pathBlockEnd = "# <<< codag PATH <<<" |
| 16 | +) |
| 17 | + |
| 18 | +type shellPathTarget struct { |
| 19 | + Path string |
| 20 | + Syntax string |
| 21 | +} |
| 22 | + |
| 23 | +func ensureExecutableOnPath(opts Options) { |
| 24 | + exe, err := os.Executable() |
| 25 | + if err != nil { |
| 26 | + return |
| 27 | + } |
| 28 | + dir := filepath.Dir(exe) |
| 29 | + if dir == "." || dir == string(filepath.Separator) { |
| 30 | + return |
| 31 | + } |
| 32 | + if pathResolvesTo(exe) { |
| 33 | + return |
| 34 | + } |
| 35 | + target, ok := shellPathTargetForEnv() |
| 36 | + if !ok { |
| 37 | + fmt.Printf(" %s PATH: add %s to your shell PATH.\n", yellow("!"), dir) |
| 38 | + return |
| 39 | + } |
| 40 | + if opts.PrintOnly { |
| 41 | + fmt.Printf(" [dry-run] would add %s to PATH in %s\n", dir, displayHome(target.Path)) |
| 42 | + return |
| 43 | + } |
| 44 | + if err := writeShellPathBlock(target, dir); err != nil { |
| 45 | + fmt.Printf(" %s PATH: could not update %s: %v\n", yellow("!"), displayHome(target.Path), err) |
| 46 | + return |
| 47 | + } |
| 48 | + fmt.Printf(" %s added %s to PATH in %s\n", green("✓"), dir, displayHome(target.Path)) |
| 49 | + fmt.Println(dim(" Restart your shell after setup if `codag` is not available immediately.")) |
| 50 | +} |
| 51 | + |
| 52 | +func pathResolvesTo(exe string) bool { |
| 53 | + path, err := exec.LookPath("codag") |
| 54 | + if err != nil { |
| 55 | + return false |
| 56 | + } |
| 57 | + return samePath(path, exe) |
| 58 | +} |
| 59 | + |
| 60 | +func samePath(a, b string) bool { |
| 61 | + aa, errA := filepath.EvalSymlinks(a) |
| 62 | + if errA != nil { |
| 63 | + aa = a |
| 64 | + } |
| 65 | + bb, errB := filepath.EvalSymlinks(b) |
| 66 | + if errB != nil { |
| 67 | + bb = b |
| 68 | + } |
| 69 | + return filepath.Clean(aa) == filepath.Clean(bb) |
| 70 | +} |
| 71 | + |
| 72 | +func shellPathTargetForEnv() (shellPathTarget, bool) { |
| 73 | + home, err := os.UserHomeDir() |
| 74 | + if err != nil || home == "" { |
| 75 | + return shellPathTarget{}, false |
| 76 | + } |
| 77 | + shellName := filepath.Base(os.Getenv("SHELL")) |
| 78 | + switch shellName { |
| 79 | + case "zsh": |
| 80 | + return shellPathTarget{Path: filepath.Join(home, ".zshrc"), Syntax: "sh"}, true |
| 81 | + case "bash": |
| 82 | + profile := filepath.Join(home, ".bash_profile") |
| 83 | + if _, err := os.Stat(profile); err == nil { |
| 84 | + return shellPathTarget{Path: profile, Syntax: "sh"}, true |
| 85 | + } |
| 86 | + return shellPathTarget{Path: filepath.Join(home, ".bashrc"), Syntax: "sh"}, true |
| 87 | + case "fish": |
| 88 | + return shellPathTarget{Path: filepath.Join(home, ".config", "fish", "config.fish"), Syntax: "fish"}, true |
| 89 | + default: |
| 90 | + if runtime.GOOS == "windows" { |
| 91 | + return shellPathTarget{}, false |
| 92 | + } |
| 93 | + return shellPathTarget{Path: filepath.Join(home, ".profile"), Syntax: "sh"}, true |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func writeShellPathBlock(target shellPathTarget, dir string) error { |
| 98 | + if err := os.MkdirAll(filepath.Dir(target.Path), 0o700); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + var existing string |
| 102 | + if data, err := os.ReadFile(target.Path); err == nil { // #nosec G304 -- setup only edits the user's shell startup file. |
| 103 | + existing = string(data) |
| 104 | + } else if !os.IsNotExist(err) { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + base, err := removeShellPathBlock(existing) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + base = strings.TrimRight(base, "\n") |
| 113 | + if base != "" { |
| 114 | + base += "\n\n" |
| 115 | + } |
| 116 | + base += renderShellPathBlock(target.Syntax, dir) |
| 117 | + return os.WriteFile(target.Path, []byte(base), 0o600) |
| 118 | +} |
| 119 | + |
| 120 | +func removeShellPathBlock(existing string) (string, error) { |
| 121 | + var out []string |
| 122 | + skip := false |
| 123 | + scanner := bufio.NewScanner(strings.NewReader(existing)) |
| 124 | + for scanner.Scan() { |
| 125 | + line := scanner.Text() |
| 126 | + switch line { |
| 127 | + case pathBlockBegin: |
| 128 | + skip = true |
| 129 | + continue |
| 130 | + case pathBlockEnd: |
| 131 | + skip = false |
| 132 | + continue |
| 133 | + } |
| 134 | + if !skip { |
| 135 | + out = append(out, line) |
| 136 | + } |
| 137 | + } |
| 138 | + if err := scanner.Err(); err != nil { |
| 139 | + return "", err |
| 140 | + } |
| 141 | + return strings.Join(out, "\n"), nil |
| 142 | +} |
| 143 | + |
| 144 | +func renderShellPathBlock(syntax, dir string) string { |
| 145 | + escaped := escapeDoubleQuoted(dir) |
| 146 | + if syntax == "fish" { |
| 147 | + return fmt.Sprintf("%s\nfish_add_path -g \"%s\"\n%s\n", pathBlockBegin, escaped, pathBlockEnd) |
| 148 | + } |
| 149 | + return fmt.Sprintf("%s\nexport PATH=\"%s:$PATH\"\n%s\n", pathBlockBegin, escaped, pathBlockEnd) |
| 150 | +} |
| 151 | + |
| 152 | +func escapeDoubleQuoted(s string) string { |
| 153 | + replacer := strings.NewReplacer( |
| 154 | + `\`, `\\`, |
| 155 | + `"`, `\"`, |
| 156 | + `$`, `\$`, |
| 157 | + "`", "\\`", |
| 158 | + ) |
| 159 | + return replacer.Replace(s) |
| 160 | +} |
| 161 | + |
| 162 | +func displayHome(path string) string { |
| 163 | + home, err := os.UserHomeDir() |
| 164 | + if err != nil || home == "" { |
| 165 | + return path |
| 166 | + } |
| 167 | + if path == home { |
| 168 | + return "~" |
| 169 | + } |
| 170 | + prefix := home + string(filepath.Separator) |
| 171 | + if strings.HasPrefix(path, prefix) { |
| 172 | + return "~/" + strings.TrimPrefix(path, prefix) |
| 173 | + } |
| 174 | + return path |
| 175 | +} |
0 commit comments