Skip to content

Commit 6b2aee4

Browse files
authored
feat(cli): make the dotfiles repository directory configurable (#10377)
1 parent d8592bf commit 6b2aee4

File tree

4 files changed

+88
-5
lines changed

4 files changed

+88
-5
lines changed

cli/dotfiles.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
func (r *RootCmd) dotfiles() *clibase.Cmd {
2323
var symlinkDir string
2424
var gitbranch string
25+
var dotfilesRepoDir string
2526

2627
cmd := &clibase.Cmd{
2728
Use: "dotfiles <git_repo_url>",
@@ -35,11 +36,10 @@ func (r *RootCmd) dotfiles() *clibase.Cmd {
3536
),
3637
Handler: func(inv *clibase.Invocation) error {
3738
var (
38-
dotfilesRepoDir = "dotfiles"
39-
gitRepo = inv.Args[0]
40-
cfg = r.createConfig()
41-
cfgDir = string(cfg)
42-
dotfilesDir = filepath.Join(cfgDir, dotfilesRepoDir)
39+
gitRepo = inv.Args[0]
40+
cfg = r.createConfig()
41+
cfgDir = string(cfg)
42+
dotfilesDir = filepath.Join(cfgDir, dotfilesRepoDir)
4343
// This follows the same pattern outlined by others in the market:
4444
// https://github.com/coder/coder/pull/1696#issue-1245742312
4545
installScriptSet = []string{
@@ -290,6 +290,13 @@ func (r *RootCmd) dotfiles() *clibase.Cmd {
290290
"If empty, will default to cloning the default branch or using the existing branch in the cloned repo on disk.",
291291
Value: clibase.StringOf(&gitbranch),
292292
},
293+
{
294+
Flag: "repo-dir",
295+
Default: "dotfiles",
296+
Env: "CODER_DOTFILES_REPO_DIR",
297+
Description: "Specifies the directory for the dotfiles repository, relative to global config directory.",
298+
Value: clibase.StringOf(&dotfilesRepoDir),
299+
},
293300
cliui.SkipPromptOption(),
294301
}
295302
return cmd

cli/dotfiles_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,68 @@ func TestDotfiles(t *testing.T) {
5050
require.NoError(t, err)
5151
require.Equal(t, string(b), "wow")
5252
})
53+
t.Run("SwitchRepoDir", func(t *testing.T) {
54+
t.Parallel()
55+
_, root := clitest.New(t)
56+
testRepo := testGitRepo(t, root)
57+
58+
// nolint:gosec
59+
err := os.WriteFile(filepath.Join(testRepo, ".bashrc"), []byte("wow"), 0o750)
60+
require.NoError(t, err)
61+
62+
c := exec.Command("git", "add", ".bashrc")
63+
c.Dir = testRepo
64+
err = c.Run()
65+
require.NoError(t, err)
66+
67+
c = exec.Command("git", "commit", "-m", `"add .bashrc"`)
68+
c.Dir = testRepo
69+
out, err := c.CombinedOutput()
70+
require.NoError(t, err, string(out))
71+
72+
inv, _ := clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "--repo-dir", "testrepo", "-y", testRepo)
73+
err = inv.Run()
74+
require.NoError(t, err)
75+
76+
b, err := os.ReadFile(filepath.Join(string(root), ".bashrc"))
77+
require.NoError(t, err)
78+
require.Equal(t, string(b), "wow")
79+
80+
stat, staterr := os.Stat(filepath.Join(string(root), "testrepo"))
81+
require.NoError(t, staterr)
82+
require.True(t, stat.IsDir())
83+
})
84+
t.Run("SwitchRepoDirRelative", func(t *testing.T) {
85+
t.Parallel()
86+
_, root := clitest.New(t)
87+
testRepo := testGitRepo(t, root)
88+
89+
// nolint:gosec
90+
err := os.WriteFile(filepath.Join(testRepo, ".bashrc"), []byte("wow"), 0o750)
91+
require.NoError(t, err)
92+
93+
c := exec.Command("git", "add", ".bashrc")
94+
c.Dir = testRepo
95+
err = c.Run()
96+
require.NoError(t, err)
97+
98+
c = exec.Command("git", "commit", "-m", `"add .bashrc"`)
99+
c.Dir = testRepo
100+
out, err := c.CombinedOutput()
101+
require.NoError(t, err, string(out))
102+
103+
inv, _ := clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "--repo-dir", "./relrepo", "-y", testRepo)
104+
err = inv.Run()
105+
require.NoError(t, err)
106+
107+
b, err := os.ReadFile(filepath.Join(string(root), ".bashrc"))
108+
require.NoError(t, err)
109+
require.Equal(t, string(b), "wow")
110+
111+
stat, staterr := os.Stat(filepath.Join(string(root), "relrepo"))
112+
require.NoError(t, staterr)
113+
require.True(t, stat.IsDir())
114+
})
53115
t.Run("InstallScript", func(t *testing.T) {
54116
t.Parallel()
55117
if runtime.GOOS == "windows" {

cli/testdata/coder_dotfiles_--help.golden

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ OPTIONS:
1515
default branch or using the existing branch in the cloned repo on
1616
disk.
1717

18+
--repo-dir string, $CODER_DOTFILES_REPO_DIR (default: dotfiles)
19+
Specifies the directory for the dotfiles repository, relative to
20+
global config directory.
21+
1822
--symlink-dir string, $CODER_SYMLINK_DIR
1923
Specifies the directory for the dotfiles symlink destinations. If
2024
empty, will use $HOME.

docs/cli/dotfiles.md

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)