-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add generate command to generate install script
- Loading branch information
Showing
8 changed files
with
106 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# `generate` *file* | ||
|
||
Generates *file* for use with chezmoi. The currently supported *file*s are: | ||
|
||
| File | Description | | ||
| ------------ | ---------------------------------------------------------- | | ||
| `install.sh` | An install script, suitable for use with Github Codespaces | | ||
|
||
!!! example | ||
|
||
```console | ||
$ chezmoi generate install.sh > install.sh | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/bin/sh | ||
|
||
# -e: exit on error | ||
# -u: exit on unset variables | ||
set -eu | ||
|
||
if ! chezmoi="$(command -v chezmoi)"; then | ||
bin_dir="${HOME}/.local/bin" | ||
chezmoi="${bin_dir}/chezmoi" | ||
echo "Installing chezmoi to '${chezmoi}'" >&2 | ||
if command -v curl >/dev/null; then | ||
chezmoi_install_script="$(curl -fsSL https://chezmoi.io/get)" | ||
elif command -v wget >/dev/null; then | ||
chezmoi_install_script="$(wget -qO- https://chezmoi.io/get)" | ||
else | ||
echo "To install chezmoi, you must have curl or wget installed." >&2 | ||
exit 1 | ||
fi | ||
sh -c "${chezmoi_install_script}" -- -b "${bin_dir}" | ||
unset chezmoi_install_script bin_dir | ||
fi | ||
|
||
# POSIX way to get script's dir: https://stackoverflow.com/a/29834779/12156188 | ||
script_dir="$(cd -P -- "$(dirname -- "$(command -v -- "$0")")" && pwd -P)" | ||
|
||
set -- init --apply --source="${script_dir}" | ||
|
||
echo "Running 'chezmoi $*'" >&2 | ||
# exec: replace current process with chezmoi | ||
exec "$chezmoi" "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,6 @@ import "embed" | |
|
||
// FS contains all templates. | ||
// | ||
//go:embed *.sh | ||
//go:embed *.tmpl | ||
var FS embed.FS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/twpayne/chezmoi/v2/assets/templates" | ||
) | ||
|
||
func (c *Config) newGenerateCmd() *cobra.Command { | ||
generateCmd := &cobra.Command{ | ||
Use: "generate file", | ||
Short: "Generate a file for use with chezmoi", | ||
Long: mustLongHelp("generate"), | ||
Example: example("generate"), | ||
Args: cobra.ExactArgs(1), | ||
ValidArgs: []string{"install.sh"}, | ||
RunE: c.runGenerateCmd, | ||
Annotations: map[string]string{ | ||
doesNotRequireValidConfig: "true", | ||
}, | ||
} | ||
|
||
return generateCmd | ||
} | ||
|
||
func (c *Config) runGenerateCmd(cmd *cobra.Command, args []string) error { | ||
builder := strings.Builder{} | ||
builder.Grow(16384) | ||
switch args[0] { | ||
case "install.sh": | ||
data, err := templates.FS.ReadFile("install.sh") | ||
if err != nil { | ||
return err | ||
} | ||
if _, err := builder.Write(data); err != nil { | ||
return err | ||
} | ||
default: | ||
return fmt.Errorf("%s: unsupported file", args[0]) | ||
} | ||
return c.writeOutputString(builder.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# test that chezmoi generate install.sh generates a shell script | ||
chezmoi generate install.sh | ||
stdout '#!/bin/sh' |