1
0
Fork 0
mirror of https://github.com/Luzifer/vault-patch.git synced 2024-12-20 12:51:19 +00:00

Modernize code

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-04-10 18:44:42 +02:00
parent 94c92a010e
commit 2b4216cb3d
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E

56
main.go
View file

@ -2,12 +2,11 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"github.com/hashicorp/vault/api" "github.com/hashicorp/vault/api"
homedir "github.com/mitchellh/go-homedir" homedir "github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/Luzifer/go_helpers/v2/env" "github.com/Luzifer/go_helpers/v2/env"
"github.com/Luzifer/rconfig/v2" "github.com/Luzifer/rconfig/v2"
@ -30,7 +29,7 @@ func vaultTokenFromDisk() string {
return "" return ""
} }
data, err := ioutil.ReadFile(vf) data, err := os.ReadFile(vf) //#nosec G304 // Intended to load file from disk
if err != nil { if err != nil {
return "" return ""
} }
@ -38,42 +37,45 @@ func vaultTokenFromDisk() string {
return string(data) return string(data)
} }
func loadConfig() error { func initApp() (err error) {
rconfig.SetVariableDefaults(map[string]string{ rconfig.SetVariableDefaults(map[string]string{"vault-token": vaultTokenFromDisk()})
"vault-token": vaultTokenFromDisk(), rconfig.AutoEnv(true)
})
if err := rconfig.Parse(&cfg); err != nil { if err = rconfig.Parse(&cfg); err != nil {
return err return fmt.Errorf("parsing CLI options: %w", err)
} }
if cfg.VersionAndExit { logLevel, err := logrus.ParseLevel(cfg.LogLevel)
fmt.Printf("vault-patch %s\n", version) if err != nil {
os.Exit(0) return fmt.Errorf("parsing log level: %w", err)
} }
if logLevel, err := log.ParseLevel(cfg.LogLevel); err == nil { logrus.SetLevel(logLevel)
log.SetLevel(logLevel)
} else {
return fmt.Errorf("Unable to parse log level: %s", err)
}
return nil return nil
} }
func main() { func main() {
if err := loadConfig(); err != nil { var err error
log.Fatalf("Unable to load CLI config: %s", err)
if err = initApp(); err != nil {
logrus.WithError(err).Fatal("initializing app")
} }
if len(rconfig.Args()) < 2 { if cfg.VersionAndExit {
log.Fatalf("Usage: vault-patch [options] path data") fmt.Printf("vault-patch %s\n", version) //nolint:forbidigo
os.Exit(0)
}
if len(rconfig.Args()) < 2 { //nolint:gomnd
logrus.Fatal("Usage: vault-patch [options] <path> <data>")
} }
client, err := api.NewClient(&api.Config{ client, err := api.NewClient(&api.Config{
Address: cfg.VaultAddress, Address: cfg.VaultAddress,
}) })
if err != nil { if err != nil {
log.Fatalf("Unable to create Vault client: %s", err) logrus.WithError(err).Fatal("creating Vault client")
} }
client.SetToken(cfg.VaultToken) client.SetToken(cfg.VaultToken)
@ -82,21 +84,21 @@ func main() {
s, err := client.Logical().Read(key) s, err := client.Logical().Read(key)
if err != nil || s == nil { if err != nil || s == nil {
log.Fatalf("Could not read key %q from vault: %s", key, err) logrus.WithError(err).Fatal("reading key from vault")
} }
data := s.Data data := s.Data
if data == nil { if data == nil {
data = make(map[string]interface{}) data = make(map[string]any)
} }
for k, v := range env.ListToMap(rconfig.Args()[2:len(rconfig.Args())]) { for k, v := range env.ListToMap(rconfig.Args()[2:]) {
data[k] = v data[k] = v
} }
if _, err := client.Logical().Write(key, data); err != nil { if _, err := client.Logical().Write(key, data); err != nil {
log.Fatalf("Could not write data to key %q: %s", key, err) logrus.WithError(err).Fatal("writing data to key")
} }
log.Printf("Data successfully written to key %q", key) logrus.Print("data successfully written to key")
} }