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-11-22 06:16:54 +00:00
|
|
|
|
|
|
|
import (
|
2022-12-14 05:41:03 +00:00
|
|
|
"fmt"
|
2023-11-26 20:57:42 +00:00
|
|
|
"io"
|
2022-11-22 06:16:54 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2022-12-19 03:09:05 +00:00
|
|
|
"git.rob.mx/nidito/chinampa/pkg/command"
|
2022-11-22 06:16:54 +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-14 05:41:03 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-11-22 06:16:54 +00:00
|
|
|
)
|
|
|
|
|
2023-01-10 07:02:10 +00:00
|
|
|
var Set = &command.Command{
|
2022-11-22 06:16:54 +00:00
|
|
|
Path: []string{"set"},
|
|
|
|
Summary: "updates configuration values",
|
|
|
|
Description: `
|
2022-12-17 05:40:43 +00:00
|
|
|
Updates the value at ﹅PATH﹅ in a local ﹅CONFIG﹅ file. Specify ﹅--secret﹅ to keep the value secret, or ﹅--delete﹅ to delete the key at PATH.
|
2022-11-22 06:16:54 +00:00
|
|
|
|
2022-12-23 05:38:37 +00:00
|
|
|
Will read values from stdin (or ﹅--from﹅ a file) and store it at the ﹅PATH﹅ of ﹅CONFIG﹅, optionally ﹅--flush﹅ing to 1Password.
|
|
|
|
`,
|
2022-11-22 06:16:54 +00:00
|
|
|
Arguments: command.Arguments{
|
|
|
|
{
|
|
|
|
Name: "config",
|
|
|
|
Description: "The configuration file to modify",
|
|
|
|
Required: true,
|
|
|
|
Values: &command.ValueSource{
|
2023-01-14 23:29:02 +00:00
|
|
|
Files: &fileExtensions,
|
2022-11-22 06:16:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "path",
|
|
|
|
Required: true,
|
|
|
|
Description: "A dot-delimited path to set in CONFIG",
|
|
|
|
Values: &command.ValueSource{
|
|
|
|
SuggestRaw: true,
|
|
|
|
Suggestion: true,
|
2022-12-23 05:38:37 +00:00
|
|
|
Func: config.AutocompleteKeys,
|
2022-11-22 06:16:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Options: command.Options{
|
|
|
|
"input": {
|
|
|
|
ShortName: "i",
|
|
|
|
Description: "the file to read input from",
|
|
|
|
Default: "/dev/stdin",
|
|
|
|
Values: &command.ValueSource{
|
|
|
|
Files: &[]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"secret": {
|
|
|
|
Description: "Store value as a secret string",
|
|
|
|
Type: "bool",
|
|
|
|
},
|
2022-12-17 05:40:43 +00:00
|
|
|
"delete": {
|
|
|
|
Description: "Delete the value at the given PATH",
|
|
|
|
Type: "bool",
|
|
|
|
},
|
2022-11-22 06:16:54 +00:00
|
|
|
"json": {
|
|
|
|
Description: "Treat input as JSON-encoded",
|
|
|
|
Type: "bool",
|
|
|
|
},
|
2022-12-14 05:41:03 +00:00
|
|
|
"flush": {
|
2022-12-17 05:40:43 +00:00
|
|
|
Description: "Save to 1Password after saving to PATH",
|
2022-12-14 05:41:03 +00:00
|
|
|
Type: "bool",
|
|
|
|
},
|
2022-11-22 06:16:54 +00:00
|
|
|
},
|
|
|
|
Action: func(cmd *command.Command) error {
|
|
|
|
path := cmd.Arguments[0].ToValue().(string)
|
|
|
|
query := cmd.Arguments[1].ToValue().(string)
|
|
|
|
|
|
|
|
var cfg *config.Config
|
|
|
|
var err error
|
|
|
|
secret := cmd.Options["secret"].ToValue().(bool)
|
2022-12-17 05:40:43 +00:00
|
|
|
delete := cmd.Options["delete"].ToValue().(bool)
|
2022-11-22 06:16:54 +00:00
|
|
|
input := cmd.Options["input"].ToValue().(string)
|
|
|
|
parseJSON := cmd.Options["json"].ToValue().(bool)
|
2022-12-14 05:41:03 +00:00
|
|
|
flush := cmd.Options["flush"].ToValue().(bool)
|
2022-11-22 06:16:54 +00:00
|
|
|
|
2022-12-17 05:40:43 +00:00
|
|
|
if secret && delete {
|
|
|
|
return fmt.Errorf("cannot --delete and set a --secret at the same time")
|
2022-11-22 06:16:54 +00:00
|
|
|
}
|
|
|
|
|
2022-12-17 05:40:43 +00:00
|
|
|
if secret && parseJSON {
|
|
|
|
return fmt.Errorf("cannot set a --secret that is JSON encoded, encode individual values instead")
|
|
|
|
}
|
|
|
|
|
2022-12-19 03:09:05 +00:00
|
|
|
if delete && input != "/dev/stdin" {
|
2023-01-11 04:50:06 +00:00
|
|
|
logrus.Warn("Ignoring --input while deleting")
|
2022-12-17 05:40:43 +00:00
|
|
|
}
|
2022-11-22 06:16:54 +00:00
|
|
|
|
2022-12-17 05:40:43 +00:00
|
|
|
cfg, err = config.Load(path, false)
|
2022-11-22 06:16:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-17 05:40:43 +00:00
|
|
|
parts := strings.Split(query, ".")
|
|
|
|
|
|
|
|
if delete {
|
|
|
|
if err := cfg.Delete(parts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2023-01-11 04:50:06 +00:00
|
|
|
var valueBytes []byte
|
|
|
|
if input == "/dev/stdin" {
|
2023-11-26 20:57:42 +00:00
|
|
|
valueBytes, err = io.ReadAll(cmd.Cobra.InOrStdin())
|
2023-01-11 04:50:06 +00:00
|
|
|
} else {
|
|
|
|
valueBytes, err = os.ReadFile(input)
|
|
|
|
}
|
2022-12-17 05:40:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := cfg.Set(parts, valueBytes, secret, parseJSON); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-22 06:16:54 +00:00
|
|
|
}
|
|
|
|
|
2023-01-14 23:29:02 +00:00
|
|
|
if err := cfg.AsFile(path); err != nil {
|
2022-11-22 06:16:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
if flush {
|
|
|
|
if err := opclient.Update(cfg.Vault, cfg.Name, cfg.ToOP()); err != nil {
|
|
|
|
return fmt.Errorf("could not flush to 1password: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Info("Done")
|
2022-11-22 06:16:54 +00:00
|
|
|
return err
|
|
|
|
},
|
2023-01-10 07:02:10 +00:00
|
|
|
}
|