| 145 | } |
| 146 | |
| 147 | func (b bash) registerRunCommand() error { |
| 148 | bashrcPath := filepath.Join(b.homeDir, ".bashrc") |
| 149 | if !fileio.PathExists(bashrcPath) { |
| 150 | return fmt.Errorf("no .bashrc file found in home dir") |
| 151 | } |
| 152 | |
| 153 | // Check if already contains the line. |
| 154 | content, err := os.ReadFile(bashrcPath) |
| 155 | if err != nil { |
| 156 | return fmt.Errorf("failed to read ~/.bashrc -> %w", err) |
| 157 | } |
| 158 | if strings.Contains(string(content), b.registerBinary) { |
| 159 | return nil |
| 160 | } |
| 161 | |
| 162 | // Append to end of .bashrc |
| 163 | file, err := os.OpenFile(bashrcPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) |
| 164 | if err != nil { |
| 165 | return fmt.Errorf("failed to open ~/.bashrc -> %w", err) |
| 166 | } |
| 167 | defer file.Close() |
| 168 | |
| 169 | if _, err := file.WriteString("\n" + b.registerBinary); err != nil { |
| 170 | return fmt.Errorf("failed to write to ~/.bashrc -> %w", err) |
| 171 | } |
| 172 | return nil |
| 173 | } |
| 174 | |
| 175 | func (b bash) unregisterRunCommand() error { |
| 176 | // Check if .bashrc exists |