Skip to content

Commit

Permalink
Populate all default template variables
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jul 13, 2021
1 parent 7c688ef commit 173ab68
Showing 1 changed file with 35 additions and 32 deletions.
67 changes: 35 additions & 32 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,20 +608,6 @@ func (c *Config) defaultSourceDir(fileSystem vfs.Stater, bds *xdg.BaseDirectoryS
}

func (c *Config) defaultTemplateData() map[string]interface{} {
data := map[string]interface{}{
"arch": runtime.GOARCH,
"homeDir": c.homeDir,
"homedir": c.homeDir, // TODO Remove in version 2.1.
"os": runtime.GOOS,
"sourceDir": string(c.SourceDirAbsPath),
"version": map[string]interface{}{
"builtBy": c.versionInfo.BuiltBy,
"commit": c.versionInfo.Commit,
"date": c.versionInfo.Date,
"version": c.versionInfo.Version,
},
}

// Determine the user's username and group, if possible.
//
// user.Current and user.LookupGroupId in Go's standard library are
Expand All @@ -647,11 +633,12 @@ func (c *Config) defaultTemplateData() map[string]interface{} {
// determined. Unset variables will trigger template errors if used,
// alerting the user to the problem and allowing them to find alternative
// solutions.
var username, group string
if currentUser, err := user.Current(); err == nil {
data["username"] = currentUser.Username
username = currentUser.Username
if runtime.GOOS != "windows" {
if group, err := user.LookupGroupId(currentUser.Gid); err == nil {
data["group"] = group.Name
if rawGroup, err := user.LookupGroupId(currentUser.Gid); err == nil {
group = rawGroup.Name
} else {
log.Debug().
Str("gid", currentUser.Gid).
Expand All @@ -663,47 +650,63 @@ func (c *Config) defaultTemplateData() map[string]interface{} {
log.Debug().
Err(err).
Msg("user.Current")
user, ok := os.LookupEnv("USER")
if ok {
data["username"] = user
} else {
var ok bool
username, ok = os.LookupEnv("USER")
if !ok {
log.Debug().
Str("key", "USER").
Bool("ok", ok).
Msg("os.LookupEnv")
}
}

if fqdnHostname := chezmoi.FQDNHostname(c.fileSystem); fqdnHostname != "" {
data["fqdnHostname"] = fqdnHostname
}
fqdnHostname := chezmoi.FQDNHostname(c.fileSystem)

if hostname, err := os.Hostname(); err == nil {
data["hostname"] = strings.SplitN(hostname, ".", 2)[0]
var hostname string
if rawHostname, err := os.Hostname(); err == nil {
hostname = strings.SplitN(rawHostname, ".", 2)[0]
} else {
log.Debug().
Err(err).
Msg("os.Hostname")
}

if kernel, err := chezmoi.Kernel(c.fileSystem); err == nil {
data["kernel"] = kernel
} else {
kernel, err := chezmoi.Kernel(c.fileSystem)
if err != nil {
log.Debug().
Err(err).
Msg("chezmoi.Kernel")
}

if osRelease, err := chezmoi.OSRelease(c.fileSystem); err == nil {
data["osRelease"] = upperSnakeCaseToCamelCaseMap(osRelease)
var osRelease map[string]interface{}
if rawOSRelease, err := chezmoi.OSRelease(c.fileSystem); err == nil {
osRelease = upperSnakeCaseToCamelCaseMap(rawOSRelease)
} else {
log.Debug().
Err(err).
Msg("chezmoi.OSRelease")
}

return map[string]interface{}{
"chezmoi": data,
"chezmoi": map[string]interface{}{
"arch": runtime.GOARCH,
"fqdnHostname": fqdnHostname,
"group": group,
"homeDir": c.homeDir,
"homedir": c.homeDir, // TODO Remove in version 2.1.
"hostname": hostname,
"kernel": kernel,
"os": runtime.GOOS,
"osRelease": osRelease,
"sourceDir": string(c.SourceDirAbsPath),
"username": username,
"version": map[string]interface{}{
"builtBy": c.versionInfo.BuiltBy,
"commit": c.versionInfo.Commit,
"date": c.versionInfo.Date,
"version": c.versionInfo.Version,
},
},
}
}

Expand Down

0 comments on commit 173ab68

Please sign in to comment.