2022-12-09 06:43:07 +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-12-09 06:43:07 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2023-08-05 04:48:29 +00:00
|
|
|
"os"
|
2022-12-09 06:43:07 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2022-12-17 05:40:43 +00:00
|
|
|
type opDetails struct {
|
|
|
|
Vault string `yaml:"vault"`
|
|
|
|
Name string `yaml:"name"`
|
2022-12-18 18:16:46 +00:00
|
|
|
NameTemplate string `yaml:"nameTemplate"` // nolint: tagliatelle
|
2022-12-17 05:40:43 +00:00
|
|
|
Repo string
|
|
|
|
}
|
|
|
|
|
|
|
|
type singleModeConfig struct {
|
2022-12-18 18:16:46 +00:00
|
|
|
Config *opDetails `yaml:"_config,omitempty"` // nolint: tagliatelle
|
2022-12-17 05:40:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 06:43:07 +00:00
|
|
|
func argIsYAMLFile(path string) bool {
|
|
|
|
return strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml")
|
|
|
|
}
|
|
|
|
|
2023-11-28 01:42:35 +00:00
|
|
|
// VaultAndNameFrom path/buffer reads a path (unless a buffer is provided) and gets the 1Password
|
|
|
|
// item name and vault name:
|
|
|
|
// first, it looks at the embedded `_config: !!joao` YAML item.
|
|
|
|
// if it still needs a vault or name, it looks for the repo config, erroring if none found
|
|
|
|
// otherwise, it'll fill in missing values from the found repo config
|
2023-01-11 06:57:58 +00:00
|
|
|
func VaultAndNameFrom(path string, buf []byte) (name string, vault string, err error) {
|
2022-12-09 06:43:07 +00:00
|
|
|
smc := &singleModeConfig{}
|
2023-11-28 01:42:35 +00:00
|
|
|
|
|
|
|
// if a buffer was not provided, read from filesystem
|
2022-12-09 06:43:07 +00:00
|
|
|
if buf == nil {
|
|
|
|
var err error
|
2023-08-05 04:48:29 +00:00
|
|
|
buf, err = os.ReadFile(path)
|
2022-12-09 06:43:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("could not read file %s", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 01:42:35 +00:00
|
|
|
// decode single-mode config
|
2022-12-14 05:41:03 +00:00
|
|
|
if err = yaml.Unmarshal(buf, &smc); err == nil && smc.Config != nil {
|
2023-11-28 01:42:35 +00:00
|
|
|
name = smc.Config.Name
|
|
|
|
vault = smc.Config.Vault
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have both name and vault, return early
|
|
|
|
if name != "" && vault != "" {
|
|
|
|
return name, vault, nil
|
2022-12-09 06:43:07 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 01:42:35 +00:00
|
|
|
// look for whole-repo config
|
2022-12-09 06:43:07 +00:00
|
|
|
rmc, err := findRepoConfig(path)
|
|
|
|
if err != nil {
|
2023-11-28 01:42:35 +00:00
|
|
|
return name, vault, err
|
2022-12-09 06:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if rmc == nil {
|
2023-11-28 01:42:35 +00:00
|
|
|
// no repo config found
|
|
|
|
return name, vault, fmt.Errorf("could not find repo config for %s", path)
|
2022-12-09 06:43:07 +00:00
|
|
|
}
|
2023-11-28 01:42:35 +00:00
|
|
|
logrus.Debugf("Found repo config at %s", rmc.Repo)
|
2022-12-09 06:43:07 +00:00
|
|
|
|
2023-11-28 01:42:35 +00:00
|
|
|
if name == "" {
|
|
|
|
if rmc.NameTemplate == "" {
|
|
|
|
rmc.NameTemplate = "{{ DirName }}:{{ FileName}}"
|
|
|
|
}
|
|
|
|
logrus.Tracef("Generating name for path %s from template %s", path, rmc.NameTemplate)
|
2022-12-09 06:43:07 +00:00
|
|
|
|
2023-11-28 01:42:35 +00:00
|
|
|
tpl := template.Must(template.New("help").Funcs(template.FuncMap{
|
|
|
|
"DirName": func() string {
|
|
|
|
return filepath.Base(filepath.Dir(path))
|
|
|
|
},
|
|
|
|
"FileName": func() string {
|
|
|
|
return strings.Split(filepath.Base(path), ".")[0]
|
|
|
|
},
|
|
|
|
}).Parse(rmc.NameTemplate))
|
2022-12-09 06:43:07 +00:00
|
|
|
|
2023-11-28 01:42:35 +00:00
|
|
|
var nameBuf bytes.Buffer
|
|
|
|
err = tpl.Execute(&nameBuf, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("could not generate item name for %s using template %s: %s", path, rmc.NameTemplate, err)
|
|
|
|
}
|
|
|
|
name = nameBuf.String()
|
|
|
|
logrus.Tracef("Setting name for path %s from repo config %s", path, name)
|
2022-12-09 06:43:07 +00:00
|
|
|
}
|
2023-11-28 01:42:35 +00:00
|
|
|
|
|
|
|
if rmc.Vault != "" && vault == "" {
|
|
|
|
logrus.Tracef("Setting vault for path %s from repo config %s", path, rmc.Vault)
|
|
|
|
vault = rmc.Vault
|
|
|
|
}
|
|
|
|
|
|
|
|
return name, vault, nil
|
2022-12-09 06:43:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 05:49:37 +00:00
|
|
|
func isNumeric(s string) bool {
|
|
|
|
for _, v := range s {
|
|
|
|
if v < '0' || v > '9' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2022-12-09 06:43:07 +00:00
|
|
|
}
|