Skip to content

Commit fc56533

Browse files
arran4twpayne
authored andcommittedOct 1, 2023
feat: Add Path Separator and Path List Separator to provided template values.
This change exports a provided variable from Go which practically makes it free in terms of implementation costs. This functionality would be implemented by hand by most people without variance when it is provided to Chezmoi by Go.
1 parent f0ec849 commit fc56533

File tree

2 files changed

+95
-87
lines changed

2 files changed

+95
-87
lines changed
 

‎assets/chezmoi.io/docs/reference/templates/variables.md

+29-27
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,35 @@
22

33
chezmoi provides the following automatically-populated variables:
44

5-
| Variable | Type | Value |
6-
| -------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------ |
7-
| `.chezmoi.arch` | string | Architecture, e.g. `amd64`, `arm`, etc. as returned by [runtime.GOARCH](https://pkg.go.dev/runtime?tab=doc#pkg-constants) |
8-
| `.chezmoi.args` | []string | The arguments passed to the `chezmoi` command, starting with the program command |
9-
| `.chezmoi.cacheDir` | string | The cache directory |
10-
| `.chezmoi.config` | object | The configuration, as read from the config file |
11-
| `.chezmoi.configFile` | string | The path to the configuration file used by chezmoi |
12-
| `.chezmoi.executable` | string | The path to the `chezmoi` executable, if available |
13-
| `.chezmoi.fqdnHostname` | string | The fully-qualified domain name hostname of the machine chezmoi is running on |
14-
| `.chezmoi.gid` | string | The primary group ID |
15-
| `.chezmoi.group` | string | The group of the user running chezmoi |
16-
| `.chezmoi.homeDir` | string | The home directory of the user running chezmoi |
17-
| `.chezmoi.hostname` | string | The hostname of the machine chezmoi is running on, up to the first `.` |
18-
| `.chezmoi.kernel` | string | Contains information from `/proc/sys/kernel`. Linux only, useful for detecting specific kernels (e.g. Microsoft's WSL kernel) |
19-
| `.chezmoi.os` | string | Operating system, e.g. `darwin`, `linux`, etc. as returned by [runtime.GOOS](https://pkg.go.dev/runtime?tab=doc#pkg-constants) |
20-
| `.chezmoi.osRelease` | string | The information from `/etc/os-release`, Linux only, run `chezmoi data` to see its output |
21-
| `.chezmoi.sourceDir` | string | The source directory |
22-
| `.chezmoi.sourceFile` | string | The path of the template relative to the source directory |
23-
| `.chezmoi.targetFile` | string | The absolute path of the target file for the template |
24-
| `.chezmoi.uid` | string | The user ID |
25-
| `.chezmoi.username` | string | The username of the user running chezmoi |
26-
| `.chezmoi.version.builtBy` | string | The program that built the `chezmoi` executable, if set |
27-
| `.chezmoi.version.commit` | string | The git commit at which the `chezmoi` executable was built, if set |
28-
| `.chezmoi.version.date` | string | The timestamp at which the `chezmoi` executable was built, if set |
29-
| `.chezmoi.version.version` | string | The version of chezmoi |
30-
| `.chezmoi.windowsVersion` | object | Windows version information, if running on Windows |
31-
| `.chezmoi.workingTree` | string | The working tree of the source directory |
5+
| Variable | Type | Value |
6+
|-------------------------------| -------- |-------------------------------------------------------------------------------------------------------------------------------------------------------|
7+
| `.chezmoi.arch` | string | Architecture, e.g. `amd64`, `arm`, etc. as returned by [runtime.GOARCH](https://pkg.go.dev/runtime?tab=doc#pkg-constants) |
8+
| `.chezmoi.args` | []string | The arguments passed to the `chezmoi` command, starting with the program command |
9+
| `.chezmoi.cacheDir` | string | The cache directory |
10+
| `.chezmoi.config` | object | The configuration, as read from the config file |
11+
| `.chezmoi.configFile` | string | The path to the configuration file used by chezmoi |
12+
| `.chezmoi.executable` | string | The path to the `chezmoi` executable, if available |
13+
| `.chezmoi.fqdnHostname` | string | The fully-qualified domain name hostname of the machine chezmoi is running on |
14+
| `.chezmoi.gid` | string | The primary group ID |
15+
| `.chezmoi.group` | string | The group of the user running chezmoi |
16+
| `.chezmoi.homeDir` | string | The home directory of the user running chezmoi |
17+
| `.chezmoi.hostname` | string | The hostname of the machine chezmoi is running on, up to the first `.` |
18+
| `.chezmoi.kernel` | string | Contains information from `/proc/sys/kernel`. Linux only, useful for detecting specific kernels (e.g. Microsoft's WSL kernel) |
19+
| `.chezmoi.os` | string | Operating system, e.g. `darwin`, `linux`, etc. as returned by [runtime.GOOS](https://pkg.go.dev/runtime?tab=doc#pkg-constants) |
20+
| `.chezmoi.osRelease` | string | The information from `/etc/os-release`, Linux only, run `chezmoi data` to see its output |
21+
| `.chezmoi.pathListSeparator` | string | The path list separator, typically `;` on Windows and `:` on other systems. Used to separate paths in environment varialbes. ie `/bin:/sbin:/usr/bin` |
22+
| `.chezmoi.pathSeparator` | string | The path separator, typically `\` on windows and `/` on unix. Used to separate files and directories in a path. ie `c:\see\dos\run` |
23+
| `.chezmoi.sourceDir` | string | The source directory |
24+
| `.chezmoi.sourceFile` | string | The path of the template relative to the source directory |
25+
| `.chezmoi.targetFile` | string | The absolute path of the target file for the template |
26+
| `.chezmoi.uid` | string | The user ID |
27+
| `.chezmoi.username` | string | The username of the user running chezmoi |
28+
| `.chezmoi.version.builtBy` | string | The program that built the `chezmoi` executable, if set |
29+
| `.chezmoi.version.commit` | string | The git commit at which the `chezmoi` executable was built, if set |
30+
| `.chezmoi.version.date` | string | The timestamp at which the `chezmoi` executable was built, if set |
31+
| `.chezmoi.version.version` | string | The version of chezmoi |
32+
| `.chezmoi.windowsVersion` | object | Windows version information, if running on Windows |
33+
| `.chezmoi.workingTree` | string | The working tree of the source directory |
3234

3335
`.chezmoi.windowsVersion` contains the following keys populated from the
3436
registry key `Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows

‎internal/cmd/config.go

+66-60
Original file line numberDiff line numberDiff line change
@@ -249,27 +249,29 @@ type Config struct {
249249
}
250250

251251
type templateData struct {
252-
arch string
253-
args []string
254-
cacheDir chezmoi.AbsPath
255-
command string
256-
config map[string]any
257-
configFile chezmoi.AbsPath
258-
executable chezmoi.AbsPath
259-
fqdnHostname string
260-
gid string
261-
group string
262-
homeDir chezmoi.AbsPath
263-
hostname string
264-
kernel map[string]any
265-
os string
266-
osRelease map[string]any
267-
sourceDir chezmoi.AbsPath
268-
uid string
269-
username string
270-
version map[string]any
271-
windowsVersion map[string]any
272-
workingTree chezmoi.AbsPath
252+
arch string
253+
args []string
254+
cacheDir chezmoi.AbsPath
255+
command string
256+
config map[string]any
257+
configFile chezmoi.AbsPath
258+
executable chezmoi.AbsPath
259+
fqdnHostname string
260+
gid string
261+
group string
262+
homeDir chezmoi.AbsPath
263+
hostname string
264+
kernel map[string]any
265+
os string
266+
osRelease map[string]any
267+
pathListSeparator string
268+
pathSeparator string
269+
sourceDir chezmoi.AbsPath
270+
uid string
271+
username string
272+
version map[string]any
273+
windowsVersion map[string]any
274+
workingTree chezmoi.AbsPath
273275
}
274276

275277
// A configOption sets and option on a Config.
@@ -1395,27 +1397,29 @@ func (c *Config) getTemplateDataMap(cmd *cobra.Command) map[string]any {
13951397

13961398
return map[string]any{
13971399
"chezmoi": map[string]any{
1398-
"arch": templateData.arch,
1399-
"args": templateData.args,
1400-
"cacheDir": templateData.cacheDir.String(),
1401-
"command": templateData.command,
1402-
"config": templateData.config,
1403-
"configFile": templateData.configFile.String(),
1404-
"executable": templateData.executable.String(),
1405-
"fqdnHostname": templateData.fqdnHostname,
1406-
"gid": templateData.gid,
1407-
"group": templateData.group,
1408-
"homeDir": templateData.homeDir.String(),
1409-
"hostname": templateData.hostname,
1410-
"kernel": templateData.kernel,
1411-
"os": templateData.os,
1412-
"osRelease": templateData.osRelease,
1413-
"sourceDir": templateData.sourceDir.String(),
1414-
"uid": templateData.uid,
1415-
"username": templateData.username,
1416-
"version": templateData.version,
1417-
"windowsVersion": templateData.windowsVersion,
1418-
"workingTree": templateData.workingTree.String(),
1400+
"arch": templateData.arch,
1401+
"args": templateData.args,
1402+
"cacheDir": templateData.cacheDir.String(),
1403+
"command": templateData.command,
1404+
"config": templateData.config,
1405+
"configFile": templateData.configFile.String(),
1406+
"executable": templateData.executable.String(),
1407+
"fqdnHostname": templateData.fqdnHostname,
1408+
"gid": templateData.gid,
1409+
"group": templateData.group,
1410+
"homeDir": templateData.homeDir.String(),
1411+
"hostname": templateData.hostname,
1412+
"kernel": templateData.kernel,
1413+
"os": templateData.os,
1414+
"osRelease": templateData.osRelease,
1415+
"pathListSeparator": templateData.pathListSeparator,
1416+
"pathSeparator": templateData.pathSeparator,
1417+
"sourceDir": templateData.sourceDir.String(),
1418+
"uid": templateData.uid,
1419+
"username": templateData.username,
1420+
"version": templateData.version,
1421+
"windowsVersion": templateData.windowsVersion,
1422+
"workingTree": templateData.workingTree.String(),
14191423
},
14201424
}
14211425
}
@@ -2263,24 +2267,26 @@ func (c *Config) newTemplateData(cmd *cobra.Command) *templateData {
22632267
sourceDirAbsPath, _ := c.getSourceDirAbsPath(nil)
22642268

22652269
return &templateData{
2266-
arch: runtime.GOARCH,
2267-
args: os.Args,
2268-
cacheDir: c.CacheDirAbsPath,
2269-
command: cmd.Name(),
2270-
config: c.ConfigFile.toMap(),
2271-
configFile: c.configFileAbsPath,
2272-
executable: chezmoi.NewAbsPath(executable),
2273-
fqdnHostname: fqdnHostname,
2274-
gid: gid,
2275-
group: group,
2276-
homeDir: c.homeDirAbsPath,
2277-
hostname: hostname,
2278-
kernel: kernel,
2279-
os: runtime.GOOS,
2280-
osRelease: osRelease,
2281-
sourceDir: sourceDirAbsPath,
2282-
uid: uid,
2283-
username: username,
2270+
arch: runtime.GOARCH,
2271+
args: os.Args,
2272+
cacheDir: c.CacheDirAbsPath,
2273+
command: cmd.Name(),
2274+
config: c.ConfigFile.toMap(),
2275+
configFile: c.configFileAbsPath,
2276+
executable: chezmoi.NewAbsPath(executable),
2277+
fqdnHostname: fqdnHostname,
2278+
gid: gid,
2279+
group: group,
2280+
homeDir: c.homeDirAbsPath,
2281+
hostname: hostname,
2282+
kernel: kernel,
2283+
os: runtime.GOOS,
2284+
osRelease: osRelease,
2285+
pathListSeparator: string(os.PathListSeparator),
2286+
pathSeparator: string(os.PathSeparator),
2287+
sourceDir: sourceDirAbsPath,
2288+
uid: uid,
2289+
username: username,
22842290
version: map[string]any{
22852291
"builtBy": c.versionInfo.BuiltBy,
22862292
"commit": c.versionInfo.Commit,

0 commit comments

Comments
 (0)
Please sign in to comment.