envoid/cmd/env.go
Sagi Dayan 29e31c5b21
All checks were successful
CI / check for spelling errors (pull_request) Successful in 21s
CI / code quality (lint/tests) (pull_request) Successful in 2m6s
CI / make sure build does not fail (pull_request) Successful in 2m22s
CI / notify-fail (pull_request) Has been skipped
CI / check for spelling errors (push) Successful in 21s
CI / code quality (lint/tests) (push) Successful in 2m4s
CI / make sure build does not fail (push) Successful in 2m24s
CI / notify-fail (push) Has been skipped
Added lint to the CI. formatted all code accordingly
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-12-17 13:10:42 +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)
}