Skip to content

Commit 5b1c3b8

Browse files
authored
Merge pull request #35 from twpayne/hide-internal-config
Hide internal config
2 parents 098e792 + ffd84fe commit 5b1c3b8

File tree

6 files changed

+48
-48
lines changed

6 files changed

+48
-48
lines changed

cmd/add.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ var addCommand = &cobra.Command{
1616
RunE: makeRunE(config.runAddCommandE),
1717
}
1818

19+
// An AddCommandConfig is a configuration for the add command.
20+
type addCommandConfig struct {
21+
empty bool
22+
recursive bool
23+
template bool
24+
}
25+
1926
func init() {
2027
rootCommand.AddCommand(addCommand)
2128

2229
persistentFlags := addCommand.PersistentFlags()
23-
persistentFlags.BoolVarP(&config.Add.Empty, "empty", "e", false, "add empty files")
24-
persistentFlags.BoolVarP(&config.Add.Recursive, "recursive", "r", false, "recurse in to subdirectories")
25-
persistentFlags.BoolVarP(&config.Add.Template, "template", "T", false, "add files as templates")
30+
persistentFlags.BoolVarP(&config.add.empty, "empty", "e", false, "add empty files")
31+
persistentFlags.BoolVarP(&config.add.recursive, "recursive", "r", false, "recurse in to subdirectories")
32+
persistentFlags.BoolVarP(&config.add.template, "template", "T", false, "add files as templates")
2633
}
2734

2835
func (c *Config) runAddCommandE(fs vfs.FS, command *cobra.Command, args []string) error {
@@ -53,17 +60,17 @@ func (c *Config) runAddCommandE(fs vfs.FS, command *cobra.Command, args []string
5360
if err != nil {
5461
return err
5562
}
56-
if c.Add.Recursive {
63+
if c.add.recursive {
5764
if err := vfs.Walk(fs, path, func(path string, info os.FileInfo, err error) error {
5865
if err != nil {
5966
return err
6067
}
61-
return targetState.Add(fs, path, info, c.Add.Empty, c.Add.Template, actuator)
68+
return targetState.Add(fs, path, info, c.add.empty, c.add.template, actuator)
6269
}); err != nil {
6370
return err
6471
}
6572
} else {
66-
if err := targetState.Add(fs, path, nil, c.Add.Empty, c.Add.Template, actuator); err != nil {
73+
if err := targetState.Add(fs, path, nil, c.add.empty, c.add.template, actuator); err != nil {
6774
return err
6875
}
6976
}

cmd/add_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88

99
func TestAddCommand(t *testing.T) {
1010
for _, tc := range []struct {
11-
name string
12-
args []string
13-
addCommandConfig AddCommandConfig
14-
root interface{}
15-
tests interface{}
11+
name string
12+
args []string
13+
add addCommandConfig
14+
root interface{}
15+
tests interface{}
1616
}{
1717
{
1818
name: "add_first_file",
@@ -28,8 +28,8 @@ func TestAddCommand(t *testing.T) {
2828
{
2929
name: "add_template",
3030
args: []string{"/home/jenkins/.gitconfig"},
31-
addCommandConfig: AddCommandConfig{
32-
Template: true,
31+
add: addCommandConfig{
32+
template: true,
3333
},
3434
root: map[string]interface{}{
3535
"/home/jenkins": &vfst.Dir{Perm: 0755},
@@ -43,8 +43,8 @@ func TestAddCommand(t *testing.T) {
4343
{
4444
name: "add_recursive",
4545
args: []string{"/home/jenkins/.config"},
46-
addCommandConfig: AddCommandConfig{
47-
Recursive: true,
46+
add: addCommandConfig{
47+
recursive: true,
4848
},
4949
root: map[string]interface{}{
5050
"/home/jenkins": &vfst.Dir{Perm: 0755},
@@ -70,8 +70,8 @@ func TestAddCommand(t *testing.T) {
7070
{
7171
name: "add_empty_file",
7272
args: []string{"/home/jenkins/empty"},
73-
addCommandConfig: AddCommandConfig{
74-
Empty: true,
73+
add: addCommandConfig{
74+
empty: true,
7575
},
7676
root: map[string]interface{}{
7777
"/home/jenkins": &vfst.Dir{Perm: 0755},
@@ -97,8 +97,8 @@ func TestAddCommand(t *testing.T) {
9797
{
9898
name: "add_symlink_in_dir_recursive",
9999
args: []string{"/home/jenkins/foo"},
100-
addCommandConfig: AddCommandConfig{
101-
Recursive: true,
100+
add: addCommandConfig{
101+
recursive: true,
102102
},
103103
root: map[string]interface{}{
104104
"/home/jenkins": &vfst.Dir{Perm: 0755},
@@ -137,7 +137,7 @@ func TestAddCommand(t *testing.T) {
137137
"name": "John Smith",
138138
"email": "john.smith@company.com",
139139
},
140-
Add: tc.addCommandConfig,
140+
add: tc.add,
141141
}
142142
fs, cleanup, err := vfst.NewTestFS(tc.root)
143143
defer cleanup()

cmd/cmd.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,6 @@ type Version struct {
2424
Date string
2525
}
2626

27-
// An AddCommandConfig is a configuration for the add command.
28-
type AddCommandConfig struct {
29-
Empty bool
30-
Recursive bool
31-
Template bool
32-
}
33-
34-
// A KeyringCommandConfig is a configuration for the keyring command.
35-
type KeyringCommandConfig struct {
36-
Service string
37-
User string
38-
Password string
39-
}
40-
4127
// A Config represents a configuration.
4228
type Config struct {
4329
version Version
@@ -48,19 +34,19 @@ type Config struct {
4834
Verbose bool
4935
SourceVCSCommand string
5036
Data map[string]interface{}
51-
Funcs template.FuncMap
52-
Add AddCommandConfig
53-
Keyring KeyringCommandConfig
37+
funcs template.FuncMap
38+
add addCommandConfig
39+
keyring keyringCommandConfig
5440
}
5541

5642
func (c *Config) addFunc(key string, value interface{}) {
57-
if c.Funcs == nil {
58-
c.Funcs = make(template.FuncMap)
43+
if c.funcs == nil {
44+
c.funcs = make(template.FuncMap)
5945
}
60-
if _, ok := c.Funcs[key]; ok {
46+
if _, ok := c.funcs[key]; ok {
6147
panic(fmt.Sprintf("Config.addFunc: %s already defined", key))
6248
}
63-
c.Funcs[key] = value
49+
c.funcs[key] = value
6450
}
6551

6652
func (c *Config) exec(argv []string) error {
@@ -158,7 +144,7 @@ func (c *Config) getTargetState(fs vfs.FS) (*chezmoi.TargetState, error) {
158144
for key, value := range c.Data {
159145
data[key] = value
160146
}
161-
targetState := chezmoi.NewTargetState(c.TargetDir, os.FileMode(c.Umask), c.SourceDir, data, c.Funcs)
147+
targetState := chezmoi.NewTargetState(c.TargetDir, os.FileMode(c.Umask), c.SourceDir, data, c.funcs)
162148
if err := targetState.Populate(fs); err != nil {
163149
return nil, err
164150
}

cmd/keyring.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@ var keyringCommand = &cobra.Command{
1111
Short: "Interact with keyring",
1212
}
1313

14+
// A keyringCommandConfig is a configuration for the keyring command.
15+
type keyringCommandConfig struct {
16+
service string
17+
user string
18+
password string
19+
}
20+
1421
func init() {
1522
rootCommand.AddCommand(keyringCommand)
1623

1724
persistentFlags := keyringCommand.PersistentFlags()
1825

19-
persistentFlags.StringVar(&config.Keyring.Service, "service", "", "service")
26+
persistentFlags.StringVar(&config.keyring.service, "service", "", "service")
2027
keyringCommand.MarkPersistentFlagRequired("service")
2128

22-
persistentFlags.StringVar(&config.Keyring.User, "user", "", "user")
29+
persistentFlags.StringVar(&config.keyring.user, "user", "", "user")
2330
keyringCommand.MarkPersistentFlagRequired("user")
2431

2532
config.addFunc("keyring", func(service, user string) string {

cmd/keyring_get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func init() {
2020
}
2121

2222
func (c *Config) runKeyringGetCommand(fs vfs.FS, cmd *cobra.Command, args []string) error {
23-
password, err := keyring.Get(c.Keyring.Service, c.Keyring.User)
23+
password, err := keyring.Get(c.keyring.service, c.keyring.user)
2424
if err != nil {
2525
return err
2626
}

cmd/keyring_set.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ func init() {
2121
keyringCommand.AddCommand(keyringSetCommand)
2222

2323
persistentFlags := keyringSetCommand.PersistentFlags()
24-
persistentFlags.StringVar(&config.Keyring.Password, "password", "", "password")
24+
persistentFlags.StringVar(&config.keyring.password, "password", "", "password")
2525
}
2626

2727
func (c *Config) runKeyringSetCommand(fs vfs.FS, cmd *cobra.Command, args []string) error {
28-
passwordString := c.Keyring.Password
28+
passwordString := c.keyring.password
2929
if passwordString == "" {
3030
fmt.Print("Password: ")
3131
password, err := terminal.ReadPassword(syscall.Stdin)
@@ -34,5 +34,5 @@ func (c *Config) runKeyringSetCommand(fs vfs.FS, cmd *cobra.Command, args []stri
3434
}
3535
passwordString = string(password)
3636
}
37-
return keyring.Set(c.Keyring.Service, c.Keyring.User, passwordString)
37+
return keyring.Set(c.keyring.service, c.keyring.user, passwordString)
3838
}

0 commit comments

Comments
 (0)