joao/cmd/flush.go

78 lines
1.9 KiB
Go
Raw Permalink Normal View History

// Copyright © 2022 Roberto Hidalgo <joao@un.rob.mx>
2022-12-31 06:14:08 +00:00
// SPDX-License-Identifier: Apache-2.0
package cmd
2022-12-18 18:16:46 +00:00
import (
"fmt"
2022-12-19 03:09:05 +00:00
"git.rob.mx/nidito/chinampa/pkg/command"
2022-12-18 18:16:46 +00:00
"git.rob.mx/nidito/joao/pkg/config"
opclient "git.rob.mx/nidito/joao/pkg/op-client"
2022-12-18 18:16:46 +00:00
"github.com/sirupsen/logrus"
)
2023-01-10 07:02:10 +00:00
var Flush = &command.Command{
2022-12-18 18:16:46 +00:00
Path: []string{"flush"},
Summary: "flush configuration values to 1Password",
Description: `Creates or updates existing items for every ﹅CONFIG﹅ file provided. Does not delete 1Password items.`,
Arguments: command.Arguments{
{
Name: "config",
Description: "The configuration file(s) to flush",
Required: false,
Variadic: true,
Values: &command.ValueSource{
Files: &fileExtensions,
2022-12-18 18:16:46 +00:00
},
},
},
Options: command.Options{
"dry-run": {
Description: "Don't persist to 1Password",
Type: "bool",
},
"redact": {
Description: "Redact local file after flushing",
Type: "bool",
},
2022-12-18 18:16:46 +00:00
},
Action: func(cmd *command.Command) error {
paths := cmd.Arguments[0].ToValue().([]string)
2023-01-17 22:25:18 +00:00
dryRun := cmd.Options["dry-run"].ToValue().(bool)
2022-12-18 18:16:46 +00:00
2023-01-17 22:25:18 +00:00
if dryRun {
2022-12-18 18:16:46 +00:00
opclient.Use(&opclient.CLI{DryRun: true})
}
for _, path := range paths {
cfg, err := config.Load(path, false)
if err != nil {
return err
}
2023-01-17 22:25:18 +00:00
if dryRun {
logrus.Warnf("dry-run: comparing %s to %s", path, cfg.OPURL())
if err := cfg.DiffRemote(path, false, false, cmd.Cobra.OutOrStdout(), cmd.Cobra.OutOrStderr()); err != nil {
return err
}
logrus.Warnf("dry-run: did not update %s", cfg.OPURL())
continue
}
2022-12-18 18:16:46 +00:00
if err := opclient.Update(cfg.Vault, cfg.Name, cfg.ToOP()); err != nil {
return fmt.Errorf("could not flush to 1password: %w", err)
}
if cmd.Options["redact"].ToValue().(bool) {
if err := cfg.AsFile(path, config.OutputModeRedacted); err != nil {
return err
}
}
2023-01-17 22:25:18 +00:00
logrus.Infof("Flushed %s to %s", path, cfg.OPURL())
2022-12-18 18:16:46 +00:00
}
logrus.Info("Done")
return nil
},
2023-01-10 07:02:10 +00:00
}