envoid/cmd/root.go
Sagi Dayan 33f99dd65b
All checks were successful
build bin / Make sure build does not fail (push) Successful in 2m51s
Codespell / Check for spelling errors (push) Successful in 22s
Initial commit
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-12-15 17:41:31 +02:00

99 lines
2.5 KiB
Go

package cmd
import (
"fmt"
"os"
"git.dayanhub.com/sagi/envoid/internal/config"
"git.dayanhub.com/sagi/envoid/internal/types"
"git.dayanhub.com/sagi/envoid/internal/variables"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)
var configuration *config.Config = nil
var workingDir string
var project *types.Project = nil
var rootCmd = &cobra.Command{
Use: "envoid [command]",
Short: "envoid is an easy to use .env manager for personal (non production) use",
Long: `envoid is an easy to use .env manager for personal (non production) use.
envoid works offline and creates different encrypted environments for each project. It's mainly used to store,encrypt (with a passphrase) and share
.env variables`,
Version: fmt.Sprintf("%s commit %s", variables.Version, variables.Commit),
}
var docCmd = &cobra.Command{
Use: "doc",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
err := doc.GenMarkdownTree(rootCmd, "./docs")
if err != nil {
return err
}
return nil
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func checkAmbiguousEnv(envName string) error {
if len(envName) == 0 && len(project.Environments) > 1 {
return fmt.Errorf("You have more than 1 environment. please provide environment name")
}
return nil
}
func initProject() error {
p, err := configuration.GetProject(workingDir)
if err != nil {
return err
}
project = p
return nil
}
func validEnvironmentNamesComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
envs := []string{}
if err := initProject(); err == nil {
for _, e := range project.Environments {
envs = append(envs, e.Name)
}
}
return envs, cobra.ShellCompDirectiveDefault
}
func validProjectPathComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
paths := []string{}
for _, p := range configuration.Projects {
paths = append(paths, fmt.Sprintf("%s\t%s", p.Path, p.Name))
}
return paths, cobra.ShellCompDirectiveDefault
}
func init() {
rootCmd.AddCommand(docCmd)
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(setCmd)
rootCmd.AddCommand(rmCmd)
rootCmd.AddCommand(getCmd)
rootCmd.AddCommand(envCmd)
rootCmd.AddCommand(printenvCmd)
rootCmd.AddCommand(importCmd)
rootCmd.AddCommand(projectCmd)
configuration = config.GetConfig()
pwd, err := os.Getwd()
if err != nil {
fmt.Printf("[error] %s. please run 'ssecret init'\n", err.Error())
os.Exit(1)
}
workingDir = pwd
}