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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-12-19 03:09:05 +00:00
|
|
|
"git.rob.mx/nidito/chinampa/pkg/command"
|
2022-11-16 06:46:14 +00:00
|
|
|
"git.rob.mx/nidito/joao/pkg/config"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2023-01-10 07:02:10 +00:00
|
|
|
var Get = &command.Command{
|
2022-11-22 06:16:54 +00:00
|
|
|
Path: []string{"get"},
|
|
|
|
Summary: "retrieves configuration",
|
|
|
|
Description: `
|
|
|
|
looks at the filesystem or remotely, using 1password (over the CLI if available, or 1password-connect, if configured).
|
|
|
|
|
2022-12-31 06:14:08 +00:00
|
|
|
` + "`--output`" + ` can be one of:
|
2022-11-22 06:16:54 +00:00
|
|
|
- **raw**:
|
|
|
|
- when querying for scalar values this will return a non-quoted version of the values
|
|
|
|
- when querying for trees or lists, this will output JSON
|
|
|
|
- **yaml**: formats the value at the given path as YAML
|
|
|
|
- **json**: formats the value at the given path as JSON
|
|
|
|
- **op**: formats the whole configuration as a 1Password item`,
|
|
|
|
Arguments: command.Arguments{
|
|
|
|
{
|
|
|
|
Name: "config",
|
|
|
|
Description: "The configuration to get from",
|
|
|
|
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",
|
|
|
|
Default: ".",
|
|
|
|
Description: "A dot-delimited path to extract from CONFIG",
|
|
|
|
Values: &command.ValueSource{
|
2022-12-23 05:38:37 +00:00
|
|
|
Func: config.AutocompleteKeysAndParents,
|
2022-11-22 06:16:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Options: command.Options{
|
|
|
|
"output": {
|
|
|
|
ShortName: "o",
|
|
|
|
Description: "the format to use for rendering output",
|
|
|
|
Default: "raw",
|
|
|
|
Values: &command.ValueSource{
|
2022-12-20 05:49:37 +00:00
|
|
|
Static: &[]string{"raw", "json", "yaml", "diff-yaml", "op"},
|
2022-11-22 06:16:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"redacted": {
|
|
|
|
Description: "Do not print secret values",
|
|
|
|
Type: "bool",
|
2023-01-10 07:02:10 +00:00
|
|
|
Default: false,
|
2022-11-22 06:16:54 +00:00
|
|
|
},
|
|
|
|
"remote": {
|
|
|
|
Description: "Get values from 1password",
|
|
|
|
Type: "bool",
|
2023-01-10 07:02:10 +00:00
|
|
|
Default: false,
|
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)
|
2022-11-16 07:38:04 +00:00
|
|
|
|
2022-11-22 06:16:54 +00:00
|
|
|
remote := cmd.Options["remote"].ToValue().(bool)
|
|
|
|
format := cmd.Options["output"].ToValue().(string)
|
|
|
|
redacted := cmd.Options["redacted"].ToValue().(bool)
|
2022-11-16 07:38:04 +00:00
|
|
|
|
2022-12-09 06:43:07 +00:00
|
|
|
cfg, err := config.Load(path, remote)
|
2022-11-22 06:16:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-11-16 06:46:14 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 06:16:54 +00:00
|
|
|
if query == "" || query == "." {
|
2023-01-11 04:50:06 +00:00
|
|
|
var bytes []byte
|
2022-11-16 06:46:14 +00:00
|
|
|
switch format {
|
2022-12-20 05:49:37 +00:00
|
|
|
case "yaml", "raw", "diff-yaml":
|
|
|
|
modes := []config.OutputMode{}
|
|
|
|
if redacted {
|
|
|
|
modes = append(modes, config.OutputModeRedacted)
|
|
|
|
}
|
|
|
|
if format == "diff-yaml" {
|
|
|
|
modes = append(modes, config.OutputModeNoComments, config.OutputModeSorted)
|
|
|
|
}
|
2023-01-11 04:50:06 +00:00
|
|
|
bytes, err = cfg.AsYAML(modes...)
|
2022-11-22 06:16:54 +00:00
|
|
|
case "json", "op":
|
2023-01-11 04:50:06 +00:00
|
|
|
bytes, err = cfg.AsJSON(redacted, format == "op")
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown format %s", format)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-11-16 06:46:14 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-11 04:50:06 +00:00
|
|
|
_, err = cmd.Cobra.OutOrStdout().Write(bytes)
|
|
|
|
return err
|
2022-11-16 06:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(query, ".")
|
|
|
|
|
|
|
|
entry := cfg.Tree
|
|
|
|
for _, part := range parts {
|
2022-11-22 06:16:54 +00:00
|
|
|
entry = entry.ChildNamed(part)
|
2022-11-16 06:46:14 +00:00
|
|
|
if entry == nil {
|
|
|
|
return fmt.Errorf("value not found at %s of %s", part, query)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var bytes []byte
|
2022-11-22 06:16:54 +00:00
|
|
|
if len(entry.Content) > 0 {
|
2022-11-16 06:46:14 +00:00
|
|
|
val := entry.AsMap()
|
|
|
|
if format == "yaml" {
|
2022-11-22 06:16:54 +00:00
|
|
|
enc := yaml.NewEncoder(cmd.Cobra.OutOrStdout())
|
2022-11-16 07:38:04 +00:00
|
|
|
enc.SetIndent(2)
|
|
|
|
return enc.Encode(val)
|
2022-11-16 06:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err = json.Marshal(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-22 06:16:54 +00:00
|
|
|
bytes = []byte(entry.String())
|
2022-11-16 06:46:14 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 06:16:54 +00:00
|
|
|
_, err = cmd.Cobra.OutOrStdout().Write(bytes)
|
2022-11-16 06:46:14 +00:00
|
|
|
return err
|
|
|
|
},
|
2023-01-10 07:02:10 +00:00
|
|
|
}
|