Skip to content

Commit

Permalink
Add writeToStdout init template function
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed May 18, 2021
1 parent 8a86a3d commit 7f8716a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/executetemplatecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (c *Config) runExecuteTemplateCmd(cmd *cobra.Command, args []string, source
"stdinIsATTY": func() bool {
return c.executeTemplate.stdinIsATTY
},
"writeToStdout": c.writeToStdout,
})
}

Expand Down
18 changes: 14 additions & 4 deletions cmd/initcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,11 @@ func (c *Config) createConfigFile(filename chezmoi.RelPath, data []byte) ([]byte
funcMap := make(template.FuncMap)
chezmoi.RecursiveMerge(funcMap, c.templateFuncs)
chezmoi.RecursiveMerge(funcMap, map[string]interface{}{
"promptBool": c.promptBool,
"promptInt": c.promptInt,
"promptString": c.promptString,
"stdinIsATTY": c.stdinIsATTY,
"promptBool": c.promptBool,
"promptInt": c.promptInt,
"promptString": c.promptString,
"stdinIsATTY": c.stdinIsATTY,
"writeToStdout": c.writeToStdout,
})

t, err := template.New(string(filename)).Funcs(funcMap).Parse(string(data))
Expand Down Expand Up @@ -308,6 +309,15 @@ func (c *Config) stdinIsATTY() bool {
return term.IsTerminal(int(file.Fd()))
}

func (c *Config) writeToStdout(args ...string) string {
for _, arg := range args {
if _, err := c.stdout.Write([]byte(arg)); err != nil {
panic(err)
}
}
return ""
}

// guessDotfilesRepoURL guesses the user's dotfile repo from arg.
func guessDotfilesRepoURL(arg string) string {
for _, dotfileRepoGuess := range dotfilesRepoGuesses {
Expand Down
10 changes: 10 additions & 0 deletions docs/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Manage your dotfiles securely across multiple machines.
* [`stat` *name*](#stat-name)
* [`stdinIsATTY`](#stdinisatty)
* [`vault` *key*](#vault-key)
* [`writeToStdout` *string*...](#writetostdout-string)

## Concepts

Expand Down Expand Up @@ -1694,3 +1695,12 @@ times with the same *key* will only invoke `vault` once.
#### `vault` examples

{{ (vault "<key>").data.data.password }}

### `writeToStdout` *string*...

`writeToStdout` writes each *string* to stdout. It is only available when
generating the initial config file.

#### `writeToStdout` examples

{{- writeToStdout "Hello, world\n" -}}
4 changes: 4 additions & 0 deletions testdata/scripts/templatefuncs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ stdout arg
# test that the output template function fails if the command fails
! chezmoi execute-template '{{ output "false" }}'

# test writeToStdout
chezmoi execute-template --init '{{ writeToStdout "string" }}'
stdout string

-- bin/chezmoi-output-test --
#!/bin/sh

Expand Down

0 comments on commit 7f8716a

Please sign in to comment.