83 lines
1.6 KiB
Go
83 lines
1.6 KiB
Go
package cmd
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.dayanhub.com/sagi/envoid/internal/datastore"
|
|
intErrors "git.dayanhub.com/sagi/envoid/internal/errors"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type getCmdFlags struct {
|
|
envName *string
|
|
}
|
|
|
|
var getFlags = getCmdFlags{}
|
|
|
|
var getCmd = &cobra.Command{
|
|
Use: "get <key>",
|
|
Short: "Gets a variable",
|
|
Long: "",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
err := initProject()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if project.IsEmpty() {
|
|
return intErrors.NewProjectEmptyError(project.Name)
|
|
}
|
|
|
|
if len(args) != 1 {
|
|
return intErrors.NewInvalidCommandError("expected 1 args. <key>")
|
|
}
|
|
|
|
key := args[0]
|
|
|
|
env := project.Environments[0]
|
|
err = checkAmbiguousEnv(*getFlags.envName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(*getFlags.envName) > 0 {
|
|
e, err := project.GetEnv(*getFlags.envName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env = e
|
|
}
|
|
|
|
ds, err := datastore.NewDataStore()
|
|
if err != nil {
|
|
fmt.Printf("Error: %e", err)
|
|
}
|
|
|
|
defer ds.Close()
|
|
|
|
envVar, err := ds.GetVar(env.Name, key)
|
|
if err != nil {
|
|
fmt.Printf("Error: %s\n", err.Error())
|
|
if errors.Is(err, &intErrors.NoKeyFoundError{}) {
|
|
os.Exit(1)
|
|
} else if errors.Is(err, intErrors.NewInvalidPasswordError()) {
|
|
os.Exit(1)
|
|
} else {
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
fmt.Println(envVar.Value)
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
getFlags.envName = getCmd.Flags().StringP("environment", "e", "", "environment name")
|
|
|
|
err := getCmd.RegisterFlagCompletionFunc("environment", validEnvironmentNamesComplete)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|