2022-11-16 06:46:14 +00:00
|
|
|
// Copyright © 2022 Roberto Hidalgo <joao@un.rob.mx>
|
2022-12-31 06:14:08 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2022-11-16 06:46:14 +00:00
|
|
|
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"
|
2023-12-01 03:38:57 +00:00
|
|
|
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{
|
2023-01-14 23:29:02 +00:00
|
|
|
Files: &fileExtensions,
|
2022-12-18 18:16:46 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Options: command.Options{
|
|
|
|
"dry-run": {
|
|
|
|
Description: "Don't persist to 1Password",
|
|
|
|
Type: "bool",
|
|
|
|
},
|
2023-01-14 23:29:02 +00:00
|
|
|
"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)
|
|
|
|
}
|
2023-01-14 23:29:02 +00:00
|
|
|
|
|
|
|
if cmd.Options["redact"].ToValue().(bool) {
|
2023-01-15 08:19:05 +00:00
|
|
|
if err := cfg.AsFile(path, config.OutputModeRedacted); err != nil {
|
2023-01-14 23:29:02 +00:00
|
|
|
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
|
|
|
}
|