-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
214 lines (199 loc) · 5.75 KB
/
Copy pathmain.go
File metadata and controls
214 lines (199 loc) · 5.75 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"context"
"fmt"
"maps"
"os"
"path/filepath"
"slices"
"strings"
"github.com/charmbracelet/huh/spinner"
"github.com/charmbracelet/log"
"github.com/quintisimo/macfigure/internal/lock"
"github.com/quintisimo/macfigure/internal/programs"
"github.com/quintisimo/macfigure/internal/programs/brew"
"github.com/quintisimo/macfigure/internal/programs/config"
"github.com/quintisimo/macfigure/internal/programs/cron"
"github.com/quintisimo/macfigure/internal/programs/dock"
"github.com/quintisimo/macfigure/internal/programs/home"
"github.com/quintisimo/macfigure/internal/programs/nsglobaldomain"
"github.com/quintisimo/macfigure/internal/programs/secret"
"github.com/urfave/cli/v3"
)
var version = "dev"
func main() {
defaultLogLevel := "info"
logLevels := map[string]log.Level{
"debug": log.DebugLevel,
defaultLogLevel: log.InfoLevel,
"warn": log.WarnLevel,
"error": log.ErrorLevel,
}
logLevelKeys := slices.Collect(maps.Keys(logLevels))
var parsedConfig config.Config
var configDir string
var configFlag *cli.StringFlag = &cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Path to the configuration `file`",
TakesFile: true,
Required: true,
Action: func(ctx context.Context, cmd *cli.Command, path string) error {
var parsingErr error
configDir = filepath.Dir(path)
parsedConfig, parsingErr = config.LoadFromPath(context.Background(), path)
return parsingErr
},
}
cli := &cli.Command{
Name: "macfigure",
Usage: "A tool to manage macOS configurations",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "dry-run",
Aliases: []string{"d"},
Value: false,
Usage: "Perform a dry run without making any changes",
},
},
Commands: []*cli.Command{
{
Name: "version",
Usage: "Cli version",
Action: func(ctx context.Context, cmd *cli.Command) error {
fmt.Printf("Version: %s\n", version)
return nil
},
},
{
Name: "sync",
Usage: "Sync system with config",
Flags: []cli.Flag{
configFlag,
&cli.StringFlag{
Name: "loglevel",
Aliases: []string{"l"},
Usage: fmt.Sprintf("Set log level, can be `%s`", strings.Join(logLevelKeys, ", ")),
Value: defaultLogLevel,
Validator: func(s string) error {
if !slices.Contains(logLevelKeys, s) {
return fmt.Errorf("invalid log level: %s", s)
}
return nil
},
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
return spinner.New().
Title("Applying config...").
ActionWithErr(func(context.Context) error {
dryRun := cmd.Bool("dry-run")
logLevel := logLevels[cmd.String("loglevel")]
lockConfig, lockConfigErr := lock.Get(configDir, logLevel, dryRun)
if lockConfigErr != nil {
return lockConfigErr
}
programsErr := programs.RunInParallel([]programs.Execution{
&brew.BrewProgram{
Program: programs.Program[brew.Brew]{
Name: "brew",
Input: parsedConfig.Brew,
},
},
&nsglobaldomain.NSGlobalDomainProgram{
Program: programs.Program[nsglobaldomain.Nsglobaldomain]{
Name: "nsglobaldomain",
Input: parsedConfig.Nsglobaldomain,
},
},
&home.HomeProgram{
Program: programs.Program[[]home.Home]{
Name: "home",
Input: parsedConfig.Home,
},
ExisitingHome: lockConfig,
},
&cron.CronProgram{
Program: programs.Program[[]cron.Cron]{
Name: "cron",
Input: parsedConfig.Cron,
},
},
&secret.SecretProgram{
Program: programs.Program[[]secret.Secret]{
Name: "secret",
Input: parsedConfig.Secret,
},
ExistingSecret: lockConfig,
},
&dock.DockProgram{
Program: programs.Program[dock.Dock]{
Name: "dock",
Input: parsedConfig.Dock,
},
},
}, logLevel, dryRun)
if programsErr != nil {
return programsErr
}
lock.DeleteRemoved(lockConfig, logLevel, dryRun)
return lock.Create(parsedConfig.Home, parsedConfig.Secret, configDir, logLevel, dryRun)
}).
Run()
},
},
{
Name: "secret",
Usage: "Manage secrets with age",
Flags: []cli.Flag{
configFlag,
},
Commands: []*cli.Command{
{
Name: "keychain",
Usage: "Manage secrets encryption and decryption key in the macOS keychain",
Commands: []*cli.Command{
{
Name: "generate",
Usage: "Generate age key and store it in the macOS keychain",
Action: func(ctx context.Context, cmd *cli.Command) error {
return secret.GenerateKeys()
},
},
{
Name: "get",
Usage: "Get the stored age public key from the macOS keychain",
Action: func(ctx context.Context, cmd *cli.Command) error {
if encryptionKeyPrintErr := secret.EncryptionKeyItem.Print(); encryptionKeyPrintErr != nil {
return encryptionKeyPrintErr
}
if decryptionKeyPrintErr := secret.DecryptionKeyItem.Print(); decryptionKeyPrintErr != nil {
return decryptionKeyPrintErr
}
return nil
},
},
},
},
{
Name: "edit",
Usage: "Edit a secrets file",
Action: func(ctx context.Context, cmd *cli.Command) error {
secretPath, secretErr := secret.List(parsedConfig.Secret)
if secretErr != nil {
return secretErr
}
if secretPath != "" {
return secret.Edit(secretPath)
}
return nil
},
},
},
},
},
}
if cliErr := cli.Run(context.Background(), os.Args); cliErr != nil {
log.Fatal(cliErr)
}
}