| 49 | } |
| 50 | |
| 51 | func (b bash) installBinary() error { |
| 52 | executable, err := os.Executable() |
| 53 | if err != nil { |
| 54 | return fmt.Errorf("failed to get celer's path -> %w", err) |
| 55 | } |
| 56 | |
| 57 | // Mkdir bin dir. |
| 58 | binDir := filepath.Join(b.homeDir, ".local", "bin") |
| 59 | if err := os.MkdirAll(binDir, os.ModePerm); err != nil { |
| 60 | return err |
| 61 | } |
| 62 | |
| 63 | // Copy into `~/.local/bin` |
| 64 | if err := fileio.CopyFile(executable, filepath.Join(binDir, "celer")); err != nil { |
| 65 | return fmt.Errorf("failed to copy celer to ~/.local/bin -> %w", err) |
| 66 | } |
| 67 | fmt.Println("[integrate] celer -> ~/.local/bin") |
| 68 | |
| 69 | // Check if already contains the line. |
| 70 | bashrcPath := filepath.Join(b.homeDir, ".bashrc") |
| 71 | content, err := os.ReadFile(bashrcPath) |
| 72 | if err != nil { |
| 73 | return fmt.Errorf("failed to read ~/.bashrc -> %w", err) |
| 74 | } |
| 75 | if strings.Contains(string(content), b.registerBinary) { |
| 76 | return nil |
| 77 | } |
| 78 | |
| 79 | // Append to `export PATH=~/.local/bin:$PATH` to end of .bashrc |
| 80 | file, err := os.OpenFile(bashrcPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModeAppend) |
| 81 | if err != nil { |
| 82 | return fmt.Errorf("failed to open ~/.bashrc -> %w", err) |
| 83 | } |
| 84 | defer file.Close() |
| 85 | |
| 86 | if _, err := file.WriteString("\n" + b.registerBinary); err != nil { |
| 87 | return fmt.Errorf("failed to write to ~/.bashrc -> %w", err) |
| 88 | } |
| 89 | |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | func (b bash) installCompletion() error { |
| 94 | if err := dirs.CleanTmpFilesDir(); err != nil { |