()
| 143 | } |
| 144 | |
| 145 | func (z zsh) registerRunCommand() error { |
| 146 | zshrcPath := filepath.Join(z.homeDir, ".zshrc") |
| 147 | if !fileio.PathExists(zshrcPath) { |
| 148 | return fmt.Errorf("no .zshrc file found in home dir") |
| 149 | } |
| 150 | |
| 151 | // Check if already contains the fpath. |
| 152 | content, err := os.ReadFile(zshrcPath) |
| 153 | if err != nil { |
| 154 | return fmt.Errorf("failed to read ~/.zshrc -> %w", err) |
| 155 | } |
| 156 | if strings.Contains(string(content), z.registerFpath) { |
| 157 | return nil |
| 158 | } |
| 159 | |
| 160 | // Add fpath to the bottom of `export PATH=$HOME/.local/bin:$PATH` |
| 161 | file, err := os.Open(zshrcPath) |
| 162 | if err != nil { |
| 163 | return err |
| 164 | } |
| 165 | defer file.Close() |
| 166 | |
| 167 | var buffer bytes.Buffer |
| 168 | scanner := bufio.NewScanner(file) |
| 169 | for scanner.Scan() { |
| 170 | line := scanner.Text() |
| 171 | if line == z.registerBinary { |
| 172 | buffer.WriteString(line + "\n" + z.registerFpath + "\n\n") |
| 173 | } else { |
| 174 | buffer.WriteString(line + "\n") |
| 175 | } |
| 176 | } |
| 177 | if err := scanner.Err(); err != nil { |
| 178 | return err |
| 179 | } |
| 180 | |
| 181 | // Write back to .zshrc |
| 182 | if err := os.WriteFile(zshrcPath, buffer.Bytes(), os.ModePerm); err != nil { |
| 183 | return err |
| 184 | } |
| 185 | |
| 186 | return nil |
| 187 | } |
| 188 | |
| 189 | func (z zsh) unregisterRunCommand() error { |
| 190 | // Check if .zshrc exists |
no test coverage detected