fix: populate temp cache before assignment

resolves #1673
This commit is contained in:
Jan De Dobbeleer 2022-02-03 17:45:35 +01:00 committed by Jan De Dobbeleer
parent 156040735e
commit e1a7f4e954

View file

@ -599,13 +599,13 @@ func (env *ShellEnvironment) TemplateCache() *TemplateCache {
if env.tmplCache != nil { if env.tmplCache != nil {
return env.tmplCache return env.tmplCache
} }
env.tmplCache = &TemplateCache{ tmplCache := &TemplateCache{
Root: env.Root(), Root: env.Root(),
Shell: env.Shell(), Shell: env.Shell(),
Code: env.ErrorCode(), Code: env.ErrorCode(),
WSL: env.IsWsl(), WSL: env.IsWsl(),
} }
env.tmplCache.Env = make(map[string]string) tmplCache.Env = make(map[string]string)
const separator = "=" const separator = "="
values := os.Environ() values := os.Environ()
for value := range values { for value := range values {
@ -615,22 +615,23 @@ func (env *ShellEnvironment) TemplateCache() *TemplateCache {
} }
key := splitted[0] key := splitted[0]
val := splitted[1:] val := splitted[1:]
env.tmplCache.Env[key] = strings.Join(val, separator) tmplCache.Env[key] = strings.Join(val, separator)
} }
pwd := env.Pwd() pwd := env.Pwd()
pwd = strings.Replace(pwd, env.Home(), "~", 1) pwd = strings.Replace(pwd, env.Home(), "~", 1)
env.tmplCache.PWD = pwd tmplCache.PWD = pwd
env.tmplCache.Folder = Base(env, pwd) tmplCache.Folder = Base(env, pwd)
env.tmplCache.UserName = env.User() tmplCache.UserName = env.User()
if host, err := env.Host(); err == nil { if host, err := env.Host(); err == nil {
env.tmplCache.HostName = host tmplCache.HostName = host
} }
goos := env.GOOS() goos := env.GOOS()
env.tmplCache.OS = goos tmplCache.OS = goos
if goos == LinuxPlatform { if goos == LinuxPlatform {
env.tmplCache.OS = env.Platform() tmplCache.OS = env.Platform()
} }
return env.tmplCache env.tmplCache = tmplCache
return tmplCache
} }
func DirMatchesOneOf(env Environment, dir string, regexes []string) bool { func DirMatchesOneOf(env Environment, dir string, regexes []string) bool {