envoid/cmd/set.go
Sagi Dayan 687d1ee475
Some checks failed
ci/ci / check for spelling errors (pull_request) Successful in 22s
ci/ci / code quality (pull_request) Failing after 24s
ci/ci / make sure build does not fail (pull_request) Has been skipped
ci/ci / notify-fail (pull_request) Successful in 18s
Added lint to the CI. formatted all code accordingly
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-12-16 23:25:28 +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)
}