Skip to content

Commit

Permalink
feat: Add uid and gid template variables
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed May 22, 2022
1 parent 1a63dc2 commit d178db4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 2 additions & 0 deletions assets/chezmoi.io/docs/reference/templates/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ chezmoi provides the following automatically-populated variables:
| `.chezmoi.configFile` | string | The path to the configuration file used by chezmoi |
| `.chezmoi.executable` | string | The path to the `chezmoi` executable, if available |
| `.chezmoi.fqdnHostname` | string | The fully-qualified domain name hostname of the machine chezmoi is running on |
| `.chezmoi.gid` | string | The primary group ID |
| `.chezmoi.group` | string | The group of the user running chezmoi |
| `.chezmoi.homeDir` | string | The home directory of the user running chezmoi |
| `.chezmoi.hostname` | string | The hostname of the machine chezmoi is running on, up to the first `.` |
Expand All @@ -18,6 +19,7 @@ chezmoi provides the following automatically-populated variables:
| `.chezmoi.osRelease` | string | The information from `/etc/os-release`, Linux only, run `chezmoi data` to see its output |
| `.chezmoi.sourceDir` | string | The source directory |
| `.chezmoi.sourceFile` | string | The path of the template relative to the source directory |
| `.chezmoi.uid` | string | The user ID |
| `.chezmoi.username` | string | The username of the user running chezmoi |
| `.chezmoi.version.builtBy` | string | The program that built the `chezmoi` executable, if set |
| `.chezmoi.version.commit` | string | The git commit at which the `chezmoi` executable was built, if set |
Expand Down
6 changes: 5 additions & 1 deletion pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,8 +902,10 @@ 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
var gid, group, uid, username string
if currentUser, err := user.Current(); err == nil {
gid = currentUser.Gid
uid = currentUser.Uid
username = currentUser.Username
if runtime.GOOS != "windows" {
if rawGroup, err := user.LookupGroupId(currentUser.Gid); err == nil {
Expand Down Expand Up @@ -972,13 +974,15 @@ func (c *Config) defaultTemplateData() map[string]interface{} {
"configFile": c.configFileAbsPath.String(),
"executable": executable,
"fqdnHostname": fqdnHostname,
"gid": gid,
"group": group,
"homeDir": c.homeDir,
"hostname": hostname,
"kernel": kernel,
"os": runtime.GOOS,
"osRelease": osRelease,
"sourceDir": c.SourceDirAbsPath.String(),
"uid": uid,
"username": username,
"version": map[string]interface{}{
"builtBy": c.versionInfo.BuiltBy,
Expand Down

0 comments on commit d178db4

Please sign in to comment.