envoid/cmd/env.go
Sagi Dayan d5d1eecb89
Some checks failed
Codespell / Check for spelling errors (push) Failing after 1m26s
Initial commit
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-12-12 20:18:00 +02:00

145 lines
3.1 KiB
Go

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()
err = ds.RemoveEnv(*envFlags.rmEnvName)
if err != nil {
return err
}
project.RemoveEnv(*envFlags.rmEnvName)
configuration.Save()
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()
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")
err := addEnvCmd.MarkFlagRequired("environment")
if err != nil {
panic(err)
}
envFlags.baseEnvName = addEnvCmd.Flags().StringP("base-environment", "b", "", "base environment name (copy data from base)")
err = addEnvCmd.RegisterFlagCompletionFunc("base-environment", validEnvironmentNamesComplete)
if err != nil {
panic(err)
}
//rm
envFlags.rmEnvName = rmEnvCmd.Flags().StringP("environment", "e", "", "environment name")
err = rmEnvCmd.MarkFlagRequired("environment")
if err != nil {
panic(err)
}
err = rmEnvCmd.RegisterFlagCompletionFunc("environment", validEnvironmentNamesComplete)
if err != nil {
panic(err)
}
// ls
envCmd.AddCommand(addEnvCmd)
envCmd.AddCommand(rmEnvCmd)
envCmd.AddCommand(lsEnvCmd)
}