envoid/cmd/set.go
Sagi Dayan 29e31c5b21
All checks were successful
CI / check for spelling errors (pull_request) Successful in 21s
CI / code quality (lint/tests) (pull_request) Successful in 2m6s
CI / make sure build does not fail (pull_request) Successful in 2m22s
CI / notify-fail (pull_request) Has been skipped
CI / check for spelling errors (push) Successful in 21s
CI / code quality (lint/tests) (push) Successful in 2m4s
CI / make sure build does not fail (push) Successful in 2m24s
CI / notify-fail (push) Has been skipped
Added lint to the CI. formatted all code accordingly
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-12-17 13:10:42 +02:00

132 lines
2.8 KiB
Go

package cmd
import (
"fmt"
"git.dayanhub.com/sagi/envoid/internal/common"
"git.dayanhub.com/sagi/envoid/internal/datastore"
"git.dayanhub.com/sagi/envoid/internal/errors"
"git.dayanhub.com/sagi/envoid/internal/types"
"github.com/spf13/cobra"
)
type setCmdFlags struct {
envName *string
encrypt *bool
}
var setFlags = setCmdFlags{}
var setCmd = &cobra.Command{
Use: "set [flags] <key> <value>",
Short: "sets a variable in environment(s)",
Long: "",
Args: func(_ *cobra.Command, args []string) error {
expectedArgs := 2
if len(args) != expectedArgs {
return errors.NewInvalidCommandError("expected 2 args. <key> <value>")
}
return nil
},
RunE: func(_ *cobra.Command, args []string) error {
err := initProject()
if err != nil {
return err
}
if project.IsEmpty() {
return errors.NewProjectEmptyError(project.Name)
}
key := args[0]
val := args[1]
ds, err := datastore.NewDataStore()
if err != nil {
fmt.Printf("Error: %e", err)
}
defer ds.Close()
envs := project.Environments
if len(*setFlags.envName) != 0 {
e, err := project.GetEnv(*setFlags.envName)
if err != nil {
return err
}
envs = []*types.Environment{e}
}
err = ds.SetValue(key, val, setFlags.encrypt, envs)
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
}
return nil
},
}
var setEncryptCmd = &cobra.Command{
Use: "encrypt <key>",
Short: "encrypts an existing variable in environment(s)",
Long: "",
Args: func(_ *cobra.Command, args []string) error {
expectedArgs := 1
if len(args) != expectedArgs {
return errors.NewInvalidCommandError("expected 1 args. <key>")
}
return nil
},
RunE: func(_ *cobra.Command, args []string) error {
err := initProject()
if err != nil {
return err
}
if project.IsEmpty() {
return errors.NewProjectEmptyError(project.Name)
}
key := args[0]
ds, err := datastore.NewDataStore()
if err != nil {
fmt.Printf("Error: %e", err)
}
defer ds.Close()
envs := project.Environments
if len(*setFlags.envName) != 0 {
e, err := project.GetEnv(*setFlags.envName)
if err != nil {
return err
}
envs = []*types.Environment{e}
}
for _, env := range envs {
// Get value
v, err := ds.GetVar(env.Name, key)
if err != nil {
return err
}
if !v.Encrypted {
err := ds.SetValue(v.Key, v.Value, common.BoolP(true), []*types.Environment{env})
if err != nil {
return err
}
}
}
return nil
},
}
func init() {
setFlags.envName = setCmd.PersistentFlags().StringP("environment", "e", "", "environments name")
setFlags.encrypt = setCmd.Flags().BoolP("secret", "s", false, "value is a secret. encrypt this value")
err := setCmd.RegisterFlagCompletionFunc("environment", validEnvironmentNamesComplete)
if err != nil {
panic(err)
}
setCmd.AddCommand(setEncryptCmd)
}