83 lines
1.7 KiB
Go
83 lines
1.7 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.dayanhub.com/sagi/envoid/internal/datastore"
|
|
intErrors "git.dayanhub.com/sagi/envoid/internal/errors"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type printenvCmdFlags struct {
|
|
envName *string
|
|
}
|
|
|
|
var printenvFlags = printenvCmdFlags{}
|
|
|
|
var printenvCmd = &cobra.Command{
|
|
Use: "printenv",
|
|
Short: "prints the whole environment in a .env format",
|
|
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) != 0 {
|
|
return intErrors.NewInvalidCommandError("expected 0 args.")
|
|
}
|
|
err = checkAmbiguousEnv(*printenvFlags.envName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env := project.Environments[0]
|
|
if len(*printenvFlags.envName) > 0 {
|
|
e, err := project.GetEnv(*printenvFlags.envName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env = e
|
|
}
|
|
ds, err := datastore.NewDataStore()
|
|
if err != nil {
|
|
fmt.Printf("Error: %e", err)
|
|
}
|
|
|
|
defer ds.Close()
|
|
vars, err := ds.GetAll(env.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, v := range vars {
|
|
if v.Encrypted {
|
|
fmt.Println("# SENSITIVE VAR BELOW")
|
|
}
|
|
if len(v.Value) != len(strings.ReplaceAll(v.Value, " ", "")) {
|
|
// value contain spaces. need to wrap with ""
|
|
fmt.Printf("%s=\"%s\"\n", v.Key, v.Value)
|
|
} else {
|
|
fmt.Printf("%s=%s\n", v.Key, v.Value)
|
|
}
|
|
if v.Encrypted {
|
|
fmt.Println("###")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
printenvFlags.envName = printenvCmd.Flags().StringP("environment", "e", "", "environments name")
|
|
|
|
err := printenvCmd.RegisterFlagCompletionFunc("environment", validEnvironmentNamesComplete)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|