/
editcmd.go
142 lines (128 loc) · 3.98 KB
/
editcmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package cmd
import (
"io/ioutil"
"runtime"
"github.com/spf13/cobra"
"github.com/twpayne/chezmoi/chezmoi2/internal/chezmoi"
)
type editCmdConfig struct {
Command string
Args []string
apply bool
include *chezmoi.IncludeSet
}
func (c *Config) newEditCmd() *cobra.Command {
editCmd := &cobra.Command{
Use: "edit targets...",
Short: "Edit the source state of a target",
Long: mustLongHelp("edit"),
Example: example("edit"),
RunE: c.makeRunEWithSourceState(c.runEditCmd),
Annotations: map[string]string{
modifiesDestinationDirectory: "true",
modifiesSourceDirectory: "true",
persistentStateMode: persistentStateModeReadWrite,
requiresSourceDirectory: "true",
runsCommands: "true",
},
}
flags := editCmd.Flags()
flags.BoolVarP(&c.Edit.apply, "apply", "a", c.Edit.apply, "apply edit after editing")
flags.VarP(c.Edit.include, "include", "i", "include entry types")
return editCmd
}
func (c *Config) runEditCmd(cmd *cobra.Command, args []string, sourceState *chezmoi.SourceState) error {
if len(args) == 0 {
if err := c.runEditor([]string{string(c.sourceDirAbsPath)}); err != nil {
return err
}
if c.Edit.apply {
if err := c.applyArgs(c.destSystem, c.destDirAbsPath, noArgs, applyArgsOptions{
include: c.Edit.include,
recursive: true,
umask: c.Umask,
preApplyFunc: c.defaultPreApplyFunc,
}); err != nil {
return err
}
}
return nil
}
targetRelPaths, err := c.targetRelPaths(sourceState, args, targetRelPathsOptions{
mustBeInSourceState: true,
})
if err != nil {
return err
}
editorArgs := make([]string, 0, len(targetRelPaths))
var decryptedDirAbsPath chezmoi.AbsPath
type transparentlyDecryptedFile struct {
sourceAbsPath chezmoi.AbsPath
decryptedAbsPath chezmoi.AbsPath
}
var transparentlyDecryptedFiles []transparentlyDecryptedFile
for _, targetRelPath := range targetRelPaths {
sourceStateEntry := sourceState.MustEntry(targetRelPath)
sourceRelPath := sourceStateEntry.SourceRelPath().RelPath()
var editorArg string
if sourceStateFile, ok := sourceStateEntry.(*chezmoi.SourceStateFile); ok && sourceStateFile.Attr.Encrypted {
if decryptedDirAbsPath == "" {
decryptedDir, err := ioutil.TempDir("", "chezmoi-decrypted")
if err != nil {
return err
}
decryptedDirAbsPath = chezmoi.AbsPath(decryptedDir)
defer func() {
_ = c.baseSystem.RemoveAll(decryptedDirAbsPath)
}()
if runtime.GOOS != "windows" {
if err := c.baseSystem.Chmod(decryptedDirAbsPath, 0o700); err != nil {
return err
}
}
}
// FIXME use RawContents and DecryptFile
decryptedAbsPath := decryptedDirAbsPath.Join(sourceRelPath)
contents, err := sourceStateFile.Contents()
if err != nil {
return err
}
if err := c.baseSystem.WriteFile(decryptedAbsPath, contents, 0o600); err != nil {
return err
}
transparentlyDecryptedFile := transparentlyDecryptedFile{
sourceAbsPath: c.sourceDirAbsPath.Join(sourceRelPath),
decryptedAbsPath: decryptedAbsPath,
}
transparentlyDecryptedFiles = append(transparentlyDecryptedFiles, transparentlyDecryptedFile)
editorArg = string(decryptedAbsPath)
} else {
sourceAbsPath := c.sourceDirAbsPath.Join(sourceRelPath)
editorArg = string(sourceAbsPath)
}
editorArgs = append(editorArgs, editorArg)
}
if err := c.runEditor(editorArgs); err != nil {
return err
}
for _, transparentlyDecryptedFile := range transparentlyDecryptedFiles {
contents, err := c.encryption.EncryptFile(string(transparentlyDecryptedFile.decryptedAbsPath))
if err != nil {
return err
}
if err := c.baseSystem.WriteFile(transparentlyDecryptedFile.sourceAbsPath, contents, 0o666); err != nil {
return err
}
}
if c.Edit.apply {
if err := c.applyArgs(c.destSystem, c.destDirAbsPath, args, applyArgsOptions{
include: c.Edit.include,
recursive: false,
umask: c.Umask,
preApplyFunc: c.defaultPreApplyFunc,
}); err != nil {
return err
}
}
return nil
}