Skip to content

Commit

Permalink
Merge pull request #728 from twpayne/execute-template-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed May 4, 2020
2 parents d6b059c + e3b3da1 commit 0bc0257
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 38 deletions.
58 changes: 44 additions & 14 deletions cmd/docs.gen.go

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

18 changes: 14 additions & 4 deletions cmd/executetemplate.go
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"io/ioutil"
"strconv"

Expand All @@ -9,6 +10,7 @@ import (

type executeTemplateCmdConfig struct {
init bool
output string
promptString map[string]string
}

Expand All @@ -26,6 +28,7 @@ func init() {

persistentFlags := executeTemplateCmd.PersistentFlags()
persistentFlags.BoolVarP(&config.executeTemplate.init, "init", "i", false, "simulate chezmoi init")
persistentFlags.StringVarP(&config.executeTemplate.output, "output", "o", "", "output filename")
persistentFlags.StringToStringVarP(&config.executeTemplate.promptString, "promptString", "p", nil, "simulate promptString")
}

Expand All @@ -43,6 +46,7 @@ func (c *Config) runExecuteTemplateCmd(cmd *cobra.Command, args []string) error
if err != nil {
return err
}
output := &bytes.Buffer{}
switch len(args) {
case 0:
data, err := ioutil.ReadAll(c.Stdin)
Expand All @@ -53,18 +57,24 @@ func (c *Config) runExecuteTemplateCmd(cmd *cobra.Command, args []string) error
if err != nil {
return err
}
_, err = c.Stdout.Write(result)
return err
if _, err = output.Write(result); err != nil {
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 {
if _, err := output.Write(result); err != nil {
return err
}
}
return nil
}

if c.executeTemplate.output == "" {
_, err = c.Stdout.Write(output.Bytes())
return err
}
return c.fs.WriteFile(c.executeTemplate.output, output.Bytes(), 0o666)
}
13 changes: 8 additions & 5 deletions cmd/helps.gen.go

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

3 changes: 3 additions & 0 deletions completions/chezmoi-completion.bash
Expand Up @@ -916,6 +916,9 @@ _chezmoi_execute-template()

flags+=("--init")
flags+=("-i")
flags+=("--output=")
two_word_flags+=("--output")
two_word_flags+=("-o")
flags+=("--promptString=")
two_word_flags+=("--promptString")
two_word_flags+=("-p")
Expand Down
1 change: 1 addition & 0 deletions completions/chezmoi.zsh
Expand Up @@ -418,6 +418,7 @@ function _chezmoi_edit-config {
function _chezmoi_execute-template {
_arguments \
'(-i --init)'{-i,--init}'[simulate chezmoi init]' \
'(-o --output)'{-o,--output}'[output filename]:' \
'(-p --promptString)'{-p,--promptString}'[simulate promptString]:' \
'--color[colorize diffs]:' \
'(-c --config)'{-c,--config}'[config file]:' \
Expand Down
48 changes: 37 additions & 11 deletions docs/FAQ.md
Expand Up @@ -16,7 +16,7 @@
* [gpg encryption fails. What could be wrong?](#gpg-encryption-fails-what-could-be-wrong)
* [I'm getting errors trying to build chezmoi from source](#im-getting-errors-trying-to-build-chezmoi-from-source)
* [What inspired chezmoi?](#what-inspired-chezmoi)
* [Can I use chezmoi outside my home directory?](#can-i-use-chezmoi-outside-my-home-directory)
* [Can I use chezmoi to manage files outside my home directory?](#can-i-use-chezmoi-to-manage-files-outside-my-home-directory)
* [Where does the name "chezmoi" come from?](#where-does-the-name-chezmoi-come-from)
* [What other questions have been asked about chezmoi?](#what-other-questions-have-been-asked-about-chezmoi)
* [Where do I ask a question that isn't answered here?](#where-do-i-ask-a-question-that-isnt-answered-here)
Expand Down Expand Up @@ -213,16 +213,42 @@ Puppet is a slow overkill for managing your personal configuration files. The
focus of chezmoi will always be personal home directory management. If your
needs grow beyond that, switch to a whole system configuration management tool.
## Can I use chezmoi outside my home directory?
chezmoi, by default, operates on your home directory, but this can be overridden
with the `--destination` command line flag or by specifying `destDir` in your config
file. In theory, you could use chezmoi to manage any aspect of your filesystem.
That said, although you can do this, you probably shouldn't. Existing
configuration management tools like [Puppet](https://puppet.com/),
[Chef](https://www.chef.io/chef/), [Ansible](https://www.ansible.com/), and
[Salt](https://www.saltstack.com/) are much better suited to whole system
configuration management.
## Can I use chezmoi to manage files outside my home directory?
In practice, yes, you can, but this is strongly discouraged beyond using your
system's package manager to install the packages you need.
chezmoi is designed to operate on your home directory, and is explicitly not a
full system configuration management tool. That said, there are some ways to
have chezmoi manage a few files outside your home directory.
chezmoi's scripts can execute arbitrary commands, so you can use a `run_` script
that is run every time you run `chezmoi apply`, to, for example:
* Make the target file outside your home directory a symlink to a file managed
by chezmoi in your home directory.
* Copy a file managed by chezmoi inside your home directory to the target file.
* Execute a template with `chezmoi execute-template --output=filename template`
where `filename` is outside the target directory.
chezmoi executes all scripts as the user executing chezmoi, so you may need to
add extra privilege elevation commands like `sudo` or `PowerShell start -verb
runas -wait` to your script.
chezmoi, by default, operates on your home directory but this can be overridden
with the `--destination` command line flag or by specifying `destDir` in your
config file, and could even be the root directory (`/` or `C:\`). This allows
you, in theory, to use chezmoi to manage any file in your filesystem, but this
usage is extremely strongly discouraged.
If your needs extend beyond modifying a handful of files outside your target
system, then existing configuration management tools like
[Puppet](https://puppet.com/), [Chef](https://www.chef.io/chef/),
[Ansible](https://www.ansible.com/), and [Salt](https://www.saltstack.com/) are
much better suited - and of couse can be called from a chezmoi `run_` script.
Put your Puppet Manifests, Chef Recipes, Ansible Modules, and Salt Modules in a
directory ignored by `.chezmoiignore` so they do not pollute your home
directory.
## Where does the name "chezmoi" come from?
Expand Down
12 changes: 8 additions & 4 deletions docs/REFERENCE.md
Expand Up @@ -582,15 +582,19 @@ Edit the configuration file.

### `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 *templates*. This is useful for testing templates or for calling chezmoi
from other scripts. *templates* are interpeted as literal templates, with no
whitespace added to the output between arguments. If no templates are specified,
the template is read from stdin.

#### `--init`, `-i`

Include simulated functions only available during `chezmoi init`.

#### '--output', '-o' *filename*

Write the output to *filename* instead of stdout.

#### `--promptString`, `-p` *pairs*

Simulate the `promptString` function with a function that returns values from
Expand Down

0 comments on commit 0bc0257

Please sign in to comment.