joao/cmd/flush.go

67 lines
1.6 KiB
Go
Raw 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
opclient "git.rob.mx/nidito/joao/internal/op-client"
"git.rob.mx/nidito/joao/pkg/config"
"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)
if dryRun := cmd.Options["dry-run"].ToValue().(bool); dryRun {
opclient.Use(&opclient.CLI{DryRun: true})
}
for _, path := range paths {
cfg, err := config.Load(path, false)
if err != nil {
return err
}
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); err != nil {
return err
}
}
2022-12-18 18:16:46 +00:00
}
logrus.Info("Done")
return nil
},
2023-01-10 07:02:10 +00:00
}