Skip to content

Commit 1c8d357

Browse files
committed
Reorganize functions
1 parent 32ce15c commit 1c8d357

File tree

2 files changed

+73
-72
lines changed

2 files changed

+73
-72
lines changed

cmd/cmd.go renamed to cmd/config.go

Lines changed: 73 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import (
1414

1515
homedir "github.com/mitchellh/go-homedir"
1616
"github.com/spf13/cobra"
17+
"github.com/spf13/viper"
1718
"github.com/twpayne/chezmoi/lib/chezmoi"
1819
vfs "github.com/twpayne/go-vfs"
20+
xdg "github.com/twpayne/go-xdg"
1921
)
2022

2123
// A Config represents a configuration.
@@ -98,39 +100,6 @@ func (c *Config) getDefaultMutator(fs vfs.FS) chezmoi.Mutator {
98100
return mutator
99101
}
100102

101-
func getDefaultData() (map[string]interface{}, error) {
102-
data := map[string]interface{}{
103-
"arch": runtime.GOARCH,
104-
"os": runtime.GOOS,
105-
}
106-
107-
currentUser, err := user.Current()
108-
if err != nil {
109-
return nil, err
110-
}
111-
data["username"] = currentUser.Username
112-
113-
group, err := user.LookupGroupId(currentUser.Gid)
114-
if err != nil {
115-
return nil, err
116-
}
117-
data["group"] = group.Name
118-
119-
homedir, err := homedir.Dir()
120-
if err != nil {
121-
return nil, err
122-
}
123-
data["homedir"] = homedir
124-
125-
hostname, err := os.Hostname()
126-
if err != nil {
127-
return nil, err
128-
}
129-
data["hostname"] = hostname
130-
131-
return data, nil
132-
}
133-
134103
func (c *Config) getEditor() string {
135104
if editor := os.Getenv("VISUAL"); editor != "" {
136105
return editor
@@ -194,6 +163,77 @@ func (c *Config) runEditor(argv ...string) error {
194163
return cmd.Run()
195164
}
196165

166+
func getDefaultConfigFile(x *xdg.XDG, homeDir string) string {
167+
// Search XDG config directories first.
168+
for _, configDir := range x.ConfigDirs {
169+
for _, extension := range viper.SupportedExts {
170+
configFilePath := filepath.Join(configDir, "chezmoi", "chezmoi."+extension)
171+
if _, err := os.Stat(configFilePath); err == nil {
172+
return configFilePath
173+
}
174+
}
175+
}
176+
// Search for ~/.chezmoi.* for backwards compatibility.
177+
for _, extension := range viper.SupportedExts {
178+
configFilePath := filepath.Join(homeDir, ".chezmoi."+extension)
179+
if _, err := os.Stat(configFilePath); err == nil {
180+
return configFilePath
181+
}
182+
}
183+
// Fallback to XDG default.
184+
return filepath.Join(x.ConfigHome, "chezmoi", "chezmoi.yaml")
185+
}
186+
187+
func getDefaultData() (map[string]interface{}, error) {
188+
data := map[string]interface{}{
189+
"arch": runtime.GOARCH,
190+
"os": runtime.GOOS,
191+
}
192+
193+
currentUser, err := user.Current()
194+
if err != nil {
195+
return nil, err
196+
}
197+
data["username"] = currentUser.Username
198+
199+
group, err := user.LookupGroupId(currentUser.Gid)
200+
if err != nil {
201+
return nil, err
202+
}
203+
data["group"] = group.Name
204+
205+
homedir, err := homedir.Dir()
206+
if err != nil {
207+
return nil, err
208+
}
209+
data["homedir"] = homedir
210+
211+
hostname, err := os.Hostname()
212+
if err != nil {
213+
return nil, err
214+
}
215+
data["hostname"] = hostname
216+
217+
return data, nil
218+
}
219+
220+
func getDefaultSourceDir(x *xdg.XDG, homeDir string) string {
221+
// Check for XDG data directories first.
222+
for _, dataDir := range x.DataDirs {
223+
sourceDir := filepath.Join(dataDir, "chezmoi")
224+
if _, err := os.Stat(sourceDir); err == nil {
225+
return sourceDir
226+
}
227+
}
228+
// Check for ~/.chezmoi for backwards compatibility.
229+
sourceDir := filepath.Join(homeDir, ".chezmoi")
230+
if _, err := os.Stat(sourceDir); err == nil {
231+
return sourceDir
232+
}
233+
// Fallback to XDG default.
234+
return filepath.Join(x.DataHome, "chezmoi")
235+
}
236+
197237
func makeRunE(runCommand func(vfs.FS, []string) error) func(*cobra.Command, []string) error {
198238
return func(cmd *cobra.Command, args []string) error {
199239
return runCommand(vfs.OSFS, args)

cmd/root.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"fmt"
55
"os"
6-
"path/filepath"
76

87
homedir "github.com/mitchellh/go-homedir"
98
"github.com/spf13/cobra"
@@ -96,41 +95,3 @@ func (c *Config) persistentPreRunRootE(fs vfs.FS, args []string) error {
9695
}
9796
return nil
9897
}
99-
100-
func getDefaultConfigFile(x *xdg.XDG, homeDir string) string {
101-
// Search XDG config directories first.
102-
for _, configDir := range x.ConfigDirs {
103-
for _, extension := range viper.SupportedExts {
104-
configFilePath := filepath.Join(configDir, "chezmoi", "chezmoi."+extension)
105-
if _, err := os.Stat(configFilePath); err == nil {
106-
return configFilePath
107-
}
108-
}
109-
}
110-
// Search for ~/.chezmoi.* for backwards compatibility.
111-
for _, extension := range viper.SupportedExts {
112-
configFilePath := filepath.Join(homeDir, ".chezmoi."+extension)
113-
if _, err := os.Stat(configFilePath); err == nil {
114-
return configFilePath
115-
}
116-
}
117-
// Fallback to XDG default.
118-
return filepath.Join(x.ConfigHome, "chezmoi", "chezmoi.yaml")
119-
}
120-
121-
func getDefaultSourceDir(x *xdg.XDG, homeDir string) string {
122-
// Check for XDG data directories first.
123-
for _, dataDir := range x.DataDirs {
124-
sourceDir := filepath.Join(dataDir, "chezmoi")
125-
if _, err := os.Stat(sourceDir); err == nil {
126-
return sourceDir
127-
}
128-
}
129-
// Check for ~/.chezmoi for backwards compatibility.
130-
sourceDir := filepath.Join(homeDir, ".chezmoi")
131-
if _, err := os.Stat(sourceDir); err == nil {
132-
return sourceDir
133-
}
134-
// Fallback to XDG default.
135-
return filepath.Join(x.DataHome, "chezmoi")
136-
}

0 commit comments

Comments
 (0)