Skip to content

Commit

Permalink
Mark dotfiles which have expanded envs
Browse files Browse the repository at this point in the history
  • Loading branch information
evanpurkhiser committed Jul 11, 2019
1 parent 4a04639 commit 1e4326c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
4 changes: 0 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ type SourceConfig struct {
// environment variables. These generally look like: ${HOME}. This
// configuration is useful for config files that do not support environment
// variable expansion.
//
// TODO: Decide if this is actually something we want to implement. It
// makes diffing a bit harder since we can't straight compare the
// file size for single files anymore.
ExpandEnvironment []string `yaml:"expand_environment"`
}

Expand Down
16 changes: 15 additions & 1 deletion resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Dotfile struct {
Path string
Removed bool
Added bool
ExpandEnv bool
Sources []*SourceFile
InstallFiles []string
}
Expand Down Expand Up @@ -185,7 +186,7 @@ func resolveOverrides(dotfiles dotfileMap, overrideSuffix string) {
}

// resolveInstalls looks for dotfiles ending in the installSuffix and will map
// them to the dorfile theyare named after, or any dotfile's that exist within
// them to the dorfile they are named after, or any dotfile's that exist within
// the directory they are named after.
func resolveInstalls(dotfiles dotfileMap, installSuffix string) {
for path, dotfile := range dotfiles {
Expand Down Expand Up @@ -233,6 +234,16 @@ func resolveRemoved(dotfiles dotfileMap, oldDotfiles []string) {
}
}

func resolveExpandEnv(dotfiles dotfileMap, expandPaths []string) {
for _, expandTarget := range expandPaths {
for path, dotfile := range dotfiles {
if expandTarget == path {
dotfile.ExpandEnv = true
}
}
}
}

// sourceLoader provides a list of files given a source path.
var sourceLoader = func(sourcePath string) []string {
sources := []string{}
Expand Down Expand Up @@ -271,5 +282,8 @@ func ResolveDotfiles(conf config.SourceConfig, lockfile config.SourceLockfile) D
resolveInstalls(dotfiles, "."+conf.InstallSuffix)
resolveRemoved(dotfiles, lockfile.InstalledFiles)

// Mark dotfiles which will have environment expansion
resolveExpandEnv(dotfiles, conf.ExpandEnvironment)

return dotfiles.asList()
}
18 changes: 11 additions & 7 deletions resolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestResolveDotfiles(t *testing.T) {
SourceFiles []string
ExistingFiles []string
Groups []string
ExpandEnv []string
OverrideSuffix string
InstallSuffix string
Expected Dotfiles
Expand Down Expand Up @@ -54,6 +55,7 @@ func TestResolveDotfiles(t *testing.T) {
},
ExistingFiles: []string{"bash/conf", "environment"},
Groups: []string{"base", "machines/desktop", "machines/server"},
ExpandEnv: []string{"generic-config"},
OverrideSuffix: "ovrd",
InstallSuffix: "inst",
Expected: Dotfiles{
Expand Down Expand Up @@ -105,8 +107,9 @@ func TestResolveDotfiles(t *testing.T) {
},
},
{
Path: "generic-config",
Added: true,
Path: "generic-config",
ExpandEnv: true,
Added: true,
Sources: []*SourceFile{
{
Group: "base",
Expand Down Expand Up @@ -260,8 +263,8 @@ func TestResolveDotfiles(t *testing.T) {
},
}

ogSourceLoader := sourceLoader
defer func() { sourceLoader = ogSourceLoader }()
origSourceLoader := sourceLoader
defer func() { sourceLoader = origSourceLoader }()

for _, test := range tests {
if test.OverrideSuffix == "" {
Expand All @@ -273,9 +276,10 @@ func TestResolveDotfiles(t *testing.T) {
}

conf := config.SourceConfig{
BaseGroups: []string{},
OverrideSuffix: test.OverrideSuffix,
InstallSuffix: test.InstallSuffix,
BaseGroups: []string{},
OverrideSuffix: test.OverrideSuffix,
InstallSuffix: test.InstallSuffix,
ExpandEnvironment: test.ExpandEnv,
}

lockfile := config.SourceLockfile{
Expand Down

0 comments on commit 1e4326c

Please sign in to comment.