Skip to content

Commit 39e5ad1

Browse files
committed
feat: Add docker command
1 parent 68103e2 commit 39e5ad1

File tree

7 files changed

+161
-98
lines changed

7 files changed

+161
-98
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# `docker`
2+
3+
!!! Warning
4+
5+
`docker` is an experimental command.
6+
7+
Install chezmoi, run `chezmoi init --apply`, and optionally execute your shell
8+
in Docker containers.
9+
10+
## Subcommands
11+
12+
### `exec` *container-id* *init-args*...
13+
14+
Install chezmoi, run `chezmoi init --apply *init-args*`, and execute your shell
15+
in the existing Docker container *container-id*.
16+
17+
#### Flags
18+
19+
#### `-i`, `--interactive`
20+
21+
Keep stdin open even if not attached.
22+
23+
#### `-p`, `--package`
24+
25+
Install chezmoi using the distribution's package manager, if possible.
26+
Otherwise, fall back to `curl` or `wget` installation. If neither `curl` nor
27+
`wget` are installed then install them with the distribution's package manager.
28+
29+
#### `-s`, `--shell`
30+
31+
After installing chezmoi, initializing your dotfiles, execute your shell. This
32+
is the default.
33+
34+
### `run` *image-id* *init-args*...
35+
36+
Create a new Docker container using *image-id*, and in it, install chezmoi, run
37+
`chezmoi init --apply *init-args*`, and execute your shell.
38+
39+
#### `-p`, `--package`
40+
41+
Install chezmoi using the distribution's package manager, if possible.
42+
Otherwise, fall back to `curl` or `wget` installation. If neither `curl` nor
43+
`wget` are installed then install them with the distribution's package manager.
44+
45+
## Examples
46+
47+
```sh
48+
chezmoi docker exec $CONTAINER_ID $GITHUB_USERNAME
49+
chezmoi docker run alpine:latest $GITHUB_USERNAME
50+
```

assets/chezmoi.io/docs/reference/commands/podman.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ is the default.
3333

3434
### `run` *image-id* *init-args*...
3535

36-
Create a new podman container using *image-id*, and in it, install chezmoit, run
36+
Create a new podman container using *image-id*, and in it, install chezmoi, run
3737
`chezmoi init --apply *init-args*`, and execute your shell.
3838

3939
#### `-p`, `--package`

assets/chezmoi.io/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ nav:
157157
- decrypt: reference/commands/decrypt.md
158158
- destroy: reference/commands/destroy.md
159159
- diff: reference/commands/diff.md
160+
- docker: reference/commands/docker.md
160161
- doctor: reference/commands/doctor.md
161162
- dump: reference/commands/dump.md
162163
- dump-config: reference/commands/dump-config.md

internal/cmd/config.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ type Config struct {
198198
chattr chattrCmdConfig
199199
data dataCmdConfig
200200
destroy destroyCmdConfig
201+
docker dockerPodmanCmdConfig
201202
doctor doctorCmdConfig
202203
dump dumpCmdConfig
203204
dumpConfig dumpConfigCmdConfig
@@ -208,7 +209,7 @@ type Config struct {
208209
init initCmdConfig
209210
managed managedCmdConfig
210211
mergeAll mergeAllCmdConfig
211-
podman podmanCmdConfig
212+
podman dockerPodmanCmdConfig
212213
ssh sshCmdConfig
213214
purge purgeCmdConfig
214215
reAdd reAddCmdConfig
@@ -362,6 +363,12 @@ func newConfig(options ...configOption) (*Config, error) {
362363
data: dataCmdConfig{
363364
format: newChoiceFlag("", writeDataFormatValues),
364365
},
366+
docker: dockerPodmanCmdConfig{
367+
exec: dockerPodmanExecCmdConfig{
368+
interactive: true,
369+
shell: true,
370+
},
371+
},
365372
dump: dumpCmdConfig{
366373
filter: chezmoi.NewEntryTypeFilter(chezmoi.EntryTypesAll, chezmoi.EntryTypesNone),
367374
format: newChoiceFlag("", writeDataFormatValues),
@@ -401,8 +408,8 @@ func newConfig(options ...configOption) (*Config, error) {
401408
filter: chezmoi.NewEntryTypeFilter(chezmoi.EntryTypesAll, chezmoi.EntryTypesNone),
402409
recursive: true,
403410
},
404-
podman: podmanCmdConfig{
405-
exec: podmanExecCmdConfig{
411+
podman: dockerPodmanCmdConfig{
412+
exec: dockerPodmanExecCmdConfig{
406413
interactive: true,
407414
shell: true,
408415
},
@@ -1864,6 +1871,8 @@ func (c *Config) newRootCmd() (*cobra.Command, error) {
18641871
c.newDecryptCommand(),
18651872
c.newDestroyCmd(),
18661873
c.newDiffCmd(),
1874+
c.newDockerPodmanCmd("docker", &c.docker),
1875+
c.newDockerPodmanCmd("podman", &c.podman),
18671876
c.newDoctorCmd(),
18681877
c.newDumpCmd(),
18691878
c.newDumpConfigCmd(),
@@ -1884,7 +1893,6 @@ func (c *Config) newRootCmd() (*cobra.Command, error) {
18841893
c.newManagedCmd(),
18851894
c.newMergeCmd(),
18861895
c.newMergeAllCmd(),
1887-
c.newPodmanCmd(),
18881896
c.newPurgeCmd(),
18891897
c.newReAddCmd(),
18901898
c.newRemoveCmd(),

internal/cmd/dockerpodmancmd.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
"github.com/twpayne/chezmoi/internal/chezmoi"
7+
)
8+
9+
type dockerPodmanCmdConfig struct {
10+
exec dockerPodmanExecCmdConfig
11+
run dockerPodmanRunCmdConfig
12+
}
13+
14+
type dockerPodmanExecCmdConfig struct {
15+
interactive bool
16+
_package bool
17+
shell bool
18+
}
19+
20+
type dockerPodmanRunCmdConfig struct {
21+
_package bool
22+
}
23+
24+
func (c *Config) newDockerPodmanCmd(command string, config *dockerPodmanCmdConfig) *cobra.Command {
25+
commandCmd := &cobra.Command{
26+
Use: command,
27+
Short: "Install chezmoi and your dotfiles in a " + command + " container",
28+
Annotations: newAnnotations(
29+
persistentStateModeNone,
30+
),
31+
}
32+
33+
commandExecCmd := &cobra.Command{
34+
Use: "exec container-id [args...]",
35+
Short: "Install chezmoi and your dotfiles in an existing " + command + " container",
36+
Args: cobra.MinimumNArgs(1),
37+
RunE: c.makeRunEWithSourceState(func(cmd *cobra.Command, args []string, sourceState *chezmoi.SourceState) error {
38+
commandArgs := []string{"exec"}
39+
if config.exec.interactive {
40+
commandArgs = append(commandArgs,
41+
"--interactive",
42+
"--tty",
43+
)
44+
}
45+
commandArgs = append(commandArgs, args[0])
46+
return c.runInstallInitShellSh(sourceState,
47+
command, commandArgs,
48+
runInstallInitShellOptions{
49+
args: args[1:],
50+
interactive: config.exec.interactive,
51+
_package: config.exec._package,
52+
shell: config.exec.shell,
53+
},
54+
)
55+
}),
56+
Annotations: newAnnotations(
57+
persistentStateModeReadWrite,
58+
),
59+
}
60+
commandExecCmd.Flags().
61+
BoolVarP(&config.exec.interactive, "interactive", "i", config.exec.interactive, "Run interactively")
62+
commandExecCmd.Flags().BoolVarP(&config.exec._package, "package", "p", config.exec._package, "Install with package")
63+
commandExecCmd.Flags().BoolVarP(&config.exec.shell, "shell", "s", config.exec.shell, "Execute shell afterwards")
64+
commandCmd.AddCommand(commandExecCmd)
65+
66+
commandRunCmd := &cobra.Command{
67+
Use: "run image-id [args...]",
68+
Short: "Install chezmoi and your dotfiles in a new " + command + " container",
69+
Args: cobra.MinimumNArgs(1),
70+
RunE: c.makeRunEWithSourceState(func(cmd *cobra.Command, args []string, sourceState *chezmoi.SourceState) error {
71+
return c.runInstallInitShellSh(sourceState,
72+
command, []string{"run", "--interactive", "--tty", args[0]},
73+
runInstallInitShellOptions{
74+
args: args[1:],
75+
interactive: true,
76+
_package: config.run._package,
77+
shell: true,
78+
},
79+
)
80+
}),
81+
Annotations: newAnnotations(
82+
persistentStateModeReadWrite,
83+
),
84+
}
85+
commandRunCmd.Flags().BoolVarP(&config.run._package, "package", "p", config.run._package, "Install with package")
86+
commandCmd.AddCommand(commandRunCmd)
87+
88+
return commandCmd
89+
}

internal/cmd/helps.gen.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/cmd/podmancmd.go

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)