joao/cmd/fetch.go

69 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 (
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"
"github.com/sirupsen/logrus"
)
2023-01-10 07:02:10 +00:00
var Fetch = &command.Command{
2022-12-18 18:16:46 +00:00
Path: []string{"fetch"},
Summary: "fetches configuration values from 1Password",
Description: `Fetches secrets for local ﹅CONFIG﹅ files from 1Password.`,
Arguments: command.Arguments{
{
Name: "config",
Description: "The configuration file(s) to fetch",
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 the filesystem",
Type: "bool",
},
},
Action: func(cmd *command.Command) error {
paths := cmd.Arguments[0].ToValue().([]string)
for _, path := range paths {
2023-01-17 22:25:18 +00:00
local, err := config.Load(path, false)
2022-12-18 18:16:46 +00:00
if err != nil {
return err
}
2023-01-17 22:25:18 +00:00
if dryRun := cmd.Options["dry-run"].ToValue().(bool); dryRun {
logrus.Warnf("dry-run: comparing %s to %s", local.OPURL(), path)
if err := local.DiffRemote(path, false, true, cmd.Cobra.OutOrStdout(), cmd.Cobra.OutOrStderr()); err != nil {
return err
}
logrus.Warnf("dry-run: did not update %s", path)
continue
}
remote, err := config.Load(path, true)
2022-12-18 18:16:46 +00:00
if err != nil {
return err
}
if err = local.Merge(remote); err != nil {
return err
}
2023-01-17 22:25:18 +00:00
if err := local.AsFile(path); err != nil {
return err
2022-12-18 18:16:46 +00:00
}
2023-01-17 22:25:18 +00:00
logrus.Infof("Fetched %s => %s", remote.OPURL(), path)
2022-12-18 18:16:46 +00:00
}
logrus.Info("Done")
return nil
},
2023-01-10 07:02:10 +00:00
}