this repo has no description

feat: extend init command, fallback to identities

Signed-off-by: A. Ottr <alex@otter.foo>

+73 -15
+28 -2
cmd/nox/main.go
··· 218 218 }, 219 219 }, 220 220 { 221 - Name: "init", 221 + Name: "init", 222 + Usage: "Initialize a new config or app", 223 + Flags: []cli.Flag{ 224 + &cli.StringFlag{ 225 + Name: "app", 226 + Usage: "name of the app", 227 + }, 228 + &cli.StringFlag{ 229 + Name: "repo", 230 + Usage: "git repository url", 231 + }, 232 + &cli.StringFlag{ 233 + Name: "branch", 234 + Value: "main", 235 + Usage: "git branch", 236 + }, 237 + &cli.StringFlag{ 238 + Name: "key", 239 + Usage: "path to age key", 240 + }, 241 + }, 222 242 Action: func(ctx context.Context, cmd *cli.Command) error { 223 - return config.InitConfig(configPath) 243 + return config.InitConfig( 244 + configPath, 245 + cmd.String("app"), 246 + cmd.String("repo"), 247 + cmd.String("branch"), 248 + cmd.String("key"), 249 + ) 224 250 }, 225 251 }, 226 252 {
+45 -13
internal/config/config.go
··· 33 33 } 34 34 35 35 type AgeConfig struct { 36 - Identity string `yaml:"identity"` 37 - Identities []string `yaml:"identities,omitempty"` 36 + Identity string `yaml:"identity,omitempty"` 37 + Identities []string `yaml:"identities"` 38 38 Recipients []string `yaml:"recipients,omitempty"` 39 39 } 40 40 ··· 81 81 return &cfg, nil 82 82 } 83 83 84 - func InitConfig(path string) error { 85 - 86 - _, err := os.Stat(path) 87 - if err == nil { 84 + func InitConfig( 85 + path, 86 + app, 87 + repo, 88 + branch, 89 + key string, 90 + ) error { 91 + _, openConfigErr := os.Stat(path) 92 + if openConfigErr == nil && app == "" { 88 93 return fmt.Errorf("config file already exists") 89 94 } 95 + var cfg Config 96 + // init new config if not exists 97 + if openConfigErr != nil { 98 + cfg = Config{ 99 + IntervalString: "10m", 100 + StatePath: ".nox-state.json", 101 + Age: AgeConfig{ 102 + Identities: []string{key}, 103 + }, 104 + GitConfig: GitConfig{ 105 + Repo: repo, 106 + Branch: branch, 107 + }, 108 + } 109 + } else if app != "" { 110 + if cfgPtr, err := Load(path); err != nil { 111 + return fmt.Errorf("failed to open config: %w", err) 112 + } else { 113 + cfg = *cfgPtr 114 + } 115 + // check if app exists 116 + if _, exists := cfg.Apps[app]; exists { 117 + return fmt.Errorf("app %s already exists", app) 118 + } 90 119 91 - cfg := Config{ 92 - IntervalString: "10m", 93 - StatePath: ".nox-state.json", 94 - GitConfig: GitConfig{ 95 - Repo: "", 96 - Branch: "main", 97 - }, 120 + var appConfig AppConfig 121 + // check for existing repo 122 + if repo != "" && cfg.GitConfig.Repo != repo && cfg.GitConfig.Branch != branch { 123 + appConfig.GitConfig = GitConfig{ 124 + Repo: repo, 125 + Branch: branch, 126 + } 127 + } 128 + cfg.Apps[app] = appConfig 98 129 } 130 + 99 131 cfgYaml, err := yaml.Marshal(cfg) 100 132 if err != nil { 101 133 return fmt.Errorf("failed to marshal config: %w", err)