envoid/cmd/env.go
Sagi Dayan 05db3e503f
Some checks failed
ci/ci / Check for spelling errors (pull_request) Successful in 22s
ci/ci / GonalgCI lint (pull_request) Failing after 1m6s
ci/ci / Make sure build does not fail (pull_request) Has been skipped
ci/ci / notify-fail (pull_request) Successful in 14s
Added lint to the CI. formatted all code accordingly
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-12-16 21:17:44 +02:00

155 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)
}