()
| 19 | ) |
| 20 | |
| 21 | func (r *RootCmd) dotfiles() *serpent.Command { |
| 22 | var symlinkDir string |
| 23 | var gitbranch string |
| 24 | var dotfilesRepoDir string |
| 25 | |
| 26 | cmd := &serpent.Command{ |
| 27 | Use: "dotfiles <git_repo_url>", |
| 28 | Middleware: serpent.RequireNArgs(1), |
| 29 | Short: "Personalize your workspace by applying a canonical dotfiles repository", |
| 30 | Long: FormatExamples( |
| 31 | Example{ |
| 32 | Description: "Check out and install a dotfiles repository without prompts", |
| 33 | Command: "coder dotfiles --yes git@github.com:example/dotfiles.git", |
| 34 | }, |
| 35 | ), |
| 36 | Handler: func(inv *serpent.Invocation) error { |
| 37 | var ( |
| 38 | gitRepo = inv.Args[0] |
| 39 | cfg = r.createConfig() |
| 40 | cfgDir = string(cfg) |
| 41 | dotfilesDir = filepath.Join(cfgDir, dotfilesRepoDir) |
| 42 | // This follows the same pattern outlined by others in the market: |
| 43 | // https://github.com/coder/coder/pull/1696#issue-1245742312 |
| 44 | installScriptSet = installScriptFiles() |
| 45 | ) |
| 46 | |
| 47 | if cfg == "" { |
| 48 | return xerrors.Errorf("no config directory") |
| 49 | } |
| 50 | |
| 51 | _, _ = fmt.Fprint(inv.Stdout, "Checking if dotfiles repository already exists...\n") |
| 52 | dotfilesExists, err := dirExists(dotfilesDir) |
| 53 | if err != nil { |
| 54 | return xerrors.Errorf("checking dir %s: %w", dotfilesDir, err) |
| 55 | } |
| 56 | |
| 57 | moved := false |
| 58 | if dotfilesExists { |
| 59 | du, err := cfg.DotfilesURL().Read() |
| 60 | if err != nil && !errors.Is(err, os.ErrNotExist) { |
| 61 | return xerrors.Errorf("reading dotfiles url config: %w", err) |
| 62 | } |
| 63 | // if the git url has changed we create a backup and clone fresh |
| 64 | if gitRepo != du { |
| 65 | backupDir := fmt.Sprintf("%s_backup_%s", dotfilesDir, time.Now().Format(time.RFC3339)) |
| 66 | _, err = cliui.Prompt(inv, cliui.PromptOptions{ |
| 67 | Text: fmt.Sprintf("The dotfiles URL has changed from %q to %q.\n Coder will backup the existing repo to %s.\n\n Continue?", du, gitRepo, backupDir), |
| 68 | IsConfirm: true, |
| 69 | }) |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | |
| 74 | err = os.Rename(dotfilesDir, backupDir) |
| 75 | if err != nil { |
| 76 | return xerrors.Errorf("renaming dir %s: %w", dotfilesDir, err) |
| 77 | } |
| 78 | _, _ = fmt.Fprint(inv.Stdout, "Done backup up dotfiles.\n") |
no test coverage detected