/
cmd.go
109 lines (94 loc) · 2.46 KB
/
cmd.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
package cmd
import (
"errors"
"fmt"
"os"
"strconv"
"github.com/spf13/cobra"
)
// Command annotations.
const (
doesNotRequireValidConfig = "chezmoi_does_not_require_valid_config"
modifiesConfigFile = "chezmoi_modifies_config_file"
modifiesDestinationDirectory = "chezmoi_modifies_destination_directory"
modifiesSourceDirectory = "chezmoi_modifies_source_directory"
persistentStateMode = "chezmoi_persistent_state_mode"
requiresConfigDirectory = "chezmoi_requires_config_directory"
requiresSourceDirectory = "chezmoi_requires_source_directory"
runsCommands = "chezmoi_runs_commands"
)
// Persistent state modes.
const (
persistentStateModeEmpty = "empty"
persistentStateModeReadOnly = "read-only"
persistentStateModeReadMockWrite = "read-mock-write"
persistentStateModeReadWrite = "read-write"
)
var noArgs = []string(nil)
// An ErrExitCode indicates the the main program should exit with the given
// code.
type ErrExitCode int
func (e ErrExitCode) Error() string { return "" }
// A VersionInfo contains a version.
type VersionInfo struct {
Version string
Commit string
Date string
BuiltBy string
}
// Main runs chezmoi and returns an exit code.
func Main(versionInfo VersionInfo, args []string) int {
if err := runMain(versionInfo, args); err != nil {
if s := err.Error(); s != "" {
fmt.Fprintf(os.Stderr, "chezmoi: %s\n", s)
}
errExitCode := ErrExitCode(1)
_ = errors.As(err, &errExitCode)
return int(errExitCode)
}
return 0
}
func asset(name string) ([]byte, error) {
asset, ok := assets[name]
if !ok {
return nil, fmt.Errorf("%s: not found", name)
}
return asset, nil
}
func boolAnnotation(cmd *cobra.Command, key string) bool {
value, ok := cmd.Annotations[key]
if !ok {
return false
}
boolValue, err := strconv.ParseBool(value)
if err != nil {
panic(err)
}
return boolValue
}
func example(command string) string {
return helps[command].example
}
func mustLongHelp(command string) string {
help, ok := helps[command]
if !ok {
panic(fmt.Sprintf("%s: no long help", command))
}
return help.long
}
func markPersistentFlagsRequired(cmd *cobra.Command, flags ...string) {
for _, flag := range flags {
if err := cmd.MarkPersistentFlagRequired(flag); err != nil {
panic(err)
}
}
}
func runMain(versionInfo VersionInfo, args []string) error {
config, err := newConfig(
withVersionInfo(versionInfo),
)
if err != nil {
return err
}
return config.execute(args)
}