2024-11-22 15:29:25 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"git.dayanhub.com/sagi/envoid/internal/datastore"
|
|
|
|
"git.dayanhub.com/sagi/envoid/internal/errors"
|
|
|
|
"git.dayanhub.com/sagi/envoid/internal/types"
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type importCmdFlags struct {
|
|
|
|
envName *string
|
|
|
|
}
|
|
|
|
|
|
|
|
var importFlags = importCmdFlags{}
|
|
|
|
|
|
|
|
var importCmd = &cobra.Command{
|
|
|
|
Use: "import <file>",
|
|
|
|
Short: "import a .env file into environment(s)",
|
|
|
|
Long: "This will not encrypt any value. You can then use `set encrypt KEY_NAME` to encrypt",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
err := initProject()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if project.IsEmpty() {
|
|
|
|
return errors.NewProjectEmptyError(project.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
envs := project.Environments
|
|
|
|
if len(*importFlags.envName) != 0 {
|
|
|
|
e, err := project.GetEnv(*importFlags.envName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
envs = []*types.Environment{e}
|
|
|
|
}
|
2024-12-16 18:22:58 +00:00
|
|
|
|
2024-11-22 15:29:25 +00:00
|
|
|
if len(args) != 1 {
|
2024-12-16 18:22:58 +00:00
|
|
|
return errors.NewInvalidCommandError("Missing a file to parse")
|
2024-11-22 15:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m, err := godotenv.Parse(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ds, err := datastore.NewDataStore()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error: %e", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer ds.Close()
|
|
|
|
for k, v := range m {
|
|
|
|
err = ds.SetValue(k, v, nil, envs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
importFlags.envName = importCmd.Flags().StringP("environment", "e", "", "environments name")
|
2024-12-16 18:22:58 +00:00
|
|
|
|
2024-11-22 15:29:25 +00:00
|
|
|
err := importCmd.RegisterFlagCompletionFunc("environment", validEnvironmentNamesComplete)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|