2024-11-22 15:29:25 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.dayanhub.com/sagi/envoid/internal/datastore"
|
|
|
|
"git.dayanhub.com/sagi/envoid/internal/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type envCmdFlags struct {
|
|
|
|
rmEnvName *string
|
|
|
|
addEnvName *string
|
|
|
|
baseEnvName *string
|
|
|
|
}
|
|
|
|
|
|
|
|
var envFlags = &envCmdFlags{}
|
|
|
|
|
|
|
|
var envCmd = &cobra.Command{
|
|
|
|
Use: "env",
|
|
|
|
Short: "manage environments",
|
|
|
|
Long: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
var rmEnvCmd = &cobra.Command{
|
|
|
|
Use: "rm",
|
|
|
|
Short: "removes an environment",
|
|
|
|
Long: "",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := initProject(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ds, err := datastore.NewDataStore()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer ds.Close()
|
2024-12-16 18:22:58 +00:00
|
|
|
|
2024-11-22 15:29:25 +00:00
|
|
|
err = ds.RemoveEnv(*envFlags.rmEnvName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-12-16 18:22:58 +00:00
|
|
|
|
2024-11-22 15:29:25 +00:00
|
|
|
project.RemoveEnv(*envFlags.rmEnvName)
|
|
|
|
configuration.Save()
|
2024-12-16 18:22:58 +00:00
|
|
|
|
2024-11-22 15:29:25 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var addEnvCmd = &cobra.Command{
|
|
|
|
Use: "add",
|
|
|
|
Short: "adds an environment",
|
|
|
|
Long: "",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := initProject(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(strings.TrimSpace(*envFlags.addEnvName)) == 0 {
|
|
|
|
return errors.NewInvalidFlagValueError("environment", *envFlags.addEnvName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := project.GetEnv(*envFlags.addEnvName); err == nil {
|
|
|
|
return errors.NewEnvironmentExistsError(*envFlags.addEnvName)
|
|
|
|
}
|
|
|
|
|
|
|
|
ds, err := datastore.NewDataStore()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer ds.Close()
|
|
|
|
|
|
|
|
if len(*envFlags.baseEnvName) != 0 {
|
|
|
|
_, err = project.GetEnv(*envFlags.baseEnvName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = ds.CreateEnvOffExsisting(*envFlags.addEnvName, *envFlags.baseEnvName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = ds.CreateEnv(*envFlags.addEnvName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = project.NewEnv(*envFlags.addEnvName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
configuration.Save()
|
2024-12-16 18:22:58 +00:00
|
|
|
|
2024-11-22 15:29:25 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var lsEnvCmd = &cobra.Command{
|
|
|
|
Use: "ls",
|
|
|
|
Short: "list all environments in this project",
|
|
|
|
Long: "",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := initProject(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if project.IsEmpty() {
|
|
|
|
return errors.NewProjectEmptyError(project.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, env := range project.Environments {
|
|
|
|
fmt.Printf("%s\n", env.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// add
|
|
|
|
envFlags.addEnvName = addEnvCmd.Flags().StringP("environment", "e", "", "environment name")
|
2024-12-16 18:22:58 +00:00
|
|
|
|
2024-11-22 15:29:25 +00:00
|
|
|
err := addEnvCmd.MarkFlagRequired("environment")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-12-16 18:22:58 +00:00
|
|
|
|
|
|
|
envFlags.baseEnvName = addEnvCmd.Flags().StringP("base-environment",
|
|
|
|
"b", "", "base environment name (copy data from base)")
|
|
|
|
|
2024-11-22 15:29:25 +00:00
|
|
|
err = addEnvCmd.RegisterFlagCompletionFunc("base-environment", validEnvironmentNamesComplete)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-12-16 18:22:58 +00:00
|
|
|
// rm
|
2024-11-22 15:29:25 +00:00
|
|
|
envFlags.rmEnvName = rmEnvCmd.Flags().StringP("environment", "e", "", "environment name")
|
2024-12-16 18:22:58 +00:00
|
|
|
|
2024-11-22 15:29:25 +00:00
|
|
|
err = rmEnvCmd.MarkFlagRequired("environment")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-12-16 18:22:58 +00:00
|
|
|
|
2024-11-22 15:29:25 +00:00
|
|
|
err = rmEnvCmd.RegisterFlagCompletionFunc("environment", validEnvironmentNamesComplete)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// ls
|
|
|
|
envCmd.AddCommand(addEnvCmd)
|
|
|
|
envCmd.AddCommand(rmEnvCmd)
|
|
|
|
envCmd.AddCommand(lsEnvCmd)
|
|
|
|
}
|