Skip to content

Commit

Permalink
Add execute-template command
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Mar 17, 2020
1 parent 79d7ddf commit b047430
Show file tree
Hide file tree
Showing 7 changed files with 994 additions and 832 deletions.
1,677 changes: 846 additions & 831 deletions cmd/docs.gen.go

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions cmd/executetemplate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"io/ioutil"
"strconv"

"github.com/spf13/cobra"
)

var executeTemplateCmd = &cobra.Command{
Use: "execute-template [templates...]",
Short: "Write the result of executing the given template(s) to stdout",
Long: mustGetLongHelp("execute-template"),
Example: getExample("execute-template"),
PreRunE: config.ensureNoError,
RunE: config.runExecuteTemplateCmd,
}

func init() {
rootCmd.AddCommand(executeTemplateCmd)
}

func (c *Config) runExecuteTemplateCmd(cmd *cobra.Command, args []string) error {
ts, err := c.getTargetState(nil)
if err != nil {
return err
}
switch len(args) {
case 0:
data, err := ioutil.ReadAll(c.Stdin)
if err != nil {
return err
}
result, err := ts.ExecuteTemplateData("stdin", data)
if err != nil {
return err
}
_, err = c.Stdout.Write(result)
return err
default:
for i, arg := range args {
result, err := ts.ExecuteTemplateData("arg"+strconv.Itoa(i+1), []byte(arg))
if err != nil {
return err
}
if _, err := c.Stdout.Write(result); err != nil {
return err
}
}
return nil
}
}
15 changes: 15 additions & 0 deletions cmd/helps.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions completions/chezmoi-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,44 @@ _chezmoi_edit-config()
noun_aliases=()
}

_chezmoi_execute-template()
{
last_command="chezmoi_execute-template"

command_aliases=()

commands=()

flags=()
two_word_flags=()
local_nonpersistent_flags=()
flags_with_completion=()
flags_completion=()

flags+=("--color=")
two_word_flags+=("--color")
flags+=("--config=")
two_word_flags+=("--config")
two_word_flags+=("-c")
flags+=("--debug")
flags+=("--destination=")
two_word_flags+=("--destination")
two_word_flags+=("-D")
flags+=("--dry-run")
flags+=("-n")
flags+=("--follow")
flags+=("--remove")
flags+=("--source=")
two_word_flags+=("--source")
two_word_flags+=("-S")
flags+=("--verbose")
flags+=("-v")

must_have_one_flag=()
must_have_one_noun=()
noun_aliases=()
}

_chezmoi_forget()
{
last_command="chezmoi_forget"
Expand Down Expand Up @@ -1847,6 +1885,7 @@ _chezmoi_root_command()
commands+=("dump")
commands+=("edit")
commands+=("edit-config")
commands+=("execute-template")
commands+=("forget")
if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then
command_aliases+=("unmanage")
Expand Down
12 changes: 11 additions & 1 deletion completions/chezmoi.fish
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

function __fish_chezmoi_no_subcommand --description 'Test if chezmoi has yet to be given the subcommand'
for i in (commandline -opc)
if contains -- $i add apply archive cat cd chattr completion data diff docs doctor dump edit edit-config forget git hg import init merge remove secret source source-path unmanaged update upgrade verify
if contains -- $i add apply archive cat cd chattr completion data diff docs doctor dump edit edit-config execute-template forget git hg import init merge remove secret source source-path unmanaged update upgrade verify
return 1
end
end
Expand Down Expand Up @@ -49,6 +49,7 @@ complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a doctor -d 'Check you
complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a dump -d 'Write a dump of the target state to stdout'
complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a edit -d 'Edit the source state of a target'
complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a edit-config -d 'Edit the configuration file'
complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a execute-template -d 'Write the result of executing the given template(s) to stdout'
complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a forget -d 'Remove a target from the source state'
complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a git -d 'Run git in the source directory'
complete -c chezmoi -f -n '__fish_chezmoi_no_subcommand' -a hg -d 'Run mercurial in the source directory'
Expand Down Expand Up @@ -216,6 +217,15 @@ complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path edit-config' -l
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path edit-config' -l remove -d 'remove targets'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path edit-config' -r -s S -l source -d 'source directory'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path edit-config' -s v -l verbose -d 'verbose'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path execute-template' -r -l color -d 'colorize diffs'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path execute-template' -r -s c -l config -d 'config file'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path execute-template' -l debug -d 'write debug logs'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path execute-template' -r -s D -l destination -d 'destination directory'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path execute-template' -s n -l dry-run -d 'dry run'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path execute-template' -l follow -d 'follow symlinks'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path execute-template' -l remove -d 'remove targets'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path execute-template' -r -s S -l source -d 'source directory'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path execute-template' -s v -l verbose -d 'verbose'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path forget' -r -l color -d 'colorize diffs'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path forget' -r -s c -l config -d 'config file'
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path forget' -l debug -d 'write debug logs'
Expand Down
17 changes: 17 additions & 0 deletions completions/chezmoi.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function _chezmoi {
"dump:Write a dump of the target state to stdout"
"edit:Edit the source state of a target"
"edit-config:Edit the configuration file"
"execute-template:Write the result of executing the given template(s) to stdout"
"forget:Remove a target from the source state"
"git:Run git in the source directory"
"help:Print help about a command"
Expand Down Expand Up @@ -97,6 +98,9 @@ function _chezmoi {
edit-config)
_chezmoi_edit-config
;;
execute-template)
_chezmoi_execute-template
;;
forget)
_chezmoi_forget
;;
Expand Down Expand Up @@ -400,6 +404,19 @@ function _chezmoi_edit-config {
'(-v --verbose)'{-v,--verbose}'[verbose]'
}

function _chezmoi_execute-template {
_arguments \
'--color[colorize diffs]:' \
'(-c --config)'{-c,--config}'[config file]:' \
'--debug[write debug logs]' \
'(-D --destination)'{-D,--destination}'[destination directory]:' \
'(-n --dry-run)'{-n,--dry-run}'[dry run]' \
'--follow[follow symlinks]' \
'--remove[remove targets]' \
'(-S --source)'{-S,--source}'[source directory]:' \
'(-v --verbose)'{-v,--verbose}'[verbose]'
}

function _chezmoi_forget {
_arguments \
'--color[colorize diffs]:' \
Expand Down
14 changes: 14 additions & 0 deletions docs/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Manage your dotfiles securely across multiple machines.
* [`dump` [*targets*]](#dump-targets)
* [`edit` [*targets*]](#edit-targets)
* [`edit-config`](#edit-config)
* [`execute-template` [*templates*]](#execute-template-templates)
* [`forget` *targets*](#forget-targets)
* [`git` [*arguments*]](#git-arguments)
* [`help` *command*](#help-command)
Expand Down Expand Up @@ -543,6 +544,19 @@ Edit the configuration file.

chezmoi edit-config

### `execute-template` [*templates*]

Write the result of evaluating *templates* to stdout. This is useful for testing
templates or for calling chezmoi from other scripts. *templates* are interpeted
as literal template data, with no whitespace added to the output between
arguments. If no templates are specified, the template data are read from stdin.

#### `execute-template` examples

chezmoi execute-template '{{ .chezmoi.sourceDir }}'
chezmoi execute-template '{{ .chezmoi.os }}' / '{{ .chezmoi.arch }}'
echo '{{ .chezmoi | toJson }}' | chezmoi execute-template

### `forget` *targets*

Remove *targets* from the source state, i.e. stop managing them.
Expand Down

0 comments on commit b047430

Please sign in to comment.