2024-11-22 15:29:25 +00:00
|
|
|
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: "",
|
2024-12-16 18:22:58 +00:00
|
|
|
Args: func(_ *cobra.Command, args []string) error {
|
|
|
|
expectedArgs := 2
|
|
|
|
if len(args) != expectedArgs {
|
2024-11-22 15:29:25 +00:00
|
|
|
return errors.NewInvalidCommandError("expected 2 args. <key> <value>")
|
|
|
|
}
|
2024-12-16 18:22:58 +00:00
|
|
|
|
2024-11-22 15:29:25 +00:00
|
|
|
return nil
|
|
|
|
},
|
2024-12-16 18:22:58 +00:00
|
|
|
RunE: func(_ *cobra.Command, args []string) error {
|
2024-11-22 15:29:25 +00:00
|
|
|
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())
|
|
|
|
}
|
2024-12-16 18:22:58 +00:00
|
|
|
|
2024-11-22 15:29:25 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var setEncryptCmd = &cobra.Command{
|
|
|
|
Use: "encrypt <key>",
|
|
|
|
Short: "encrypts an existing variable in environment(s)",
|
|
|
|
Long: "",
|
2024-12-16 18:22:58 +00:00
|
|
|
Args: func(_ *cobra.Command, args []string) error {
|
|
|
|
expectedArgs := 1
|
|
|
|
if len(args) != expectedArgs {
|
2024-11-22 15:29:25 +00:00
|
|
|
return errors.NewInvalidCommandError("expected 1 args. <key>")
|
|
|
|
}
|
2024-12-16 18:22:58 +00:00
|
|
|
|
2024-11-22 15:29:25 +00:00
|
|
|
return nil
|
|
|
|
},
|
2024-12-16 18:22:58 +00:00
|
|
|
RunE: func(_ *cobra.Command, args []string) error {
|
2024-11-22 15:29:25 +00:00
|
|
|
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")
|
2024-12-16 18:22:58 +00:00
|
|
|
|
2024-11-22 15:29:25 +00:00
|
|
|
err := setCmd.RegisterFlagCompletionFunc("environment", validEnvironmentNamesComplete)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-12-16 18:22:58 +00:00
|
|
|
|
2024-11-22 15:29:25 +00:00
|
|
|
setCmd.AddCommand(setEncryptCmd)
|
|
|
|
}
|