package cmd import ( "encoding/json" "errors" "fmt" "git.denysoft.de/CubeBit/ssh-hub/cmd/add" cobraDelete "git.denysoft.de/CubeBit/ssh-hub/cmd/delete" "git.denysoft.de/CubeBit/ssh-hub/cmd/exec" "git.denysoft.de/CubeBit/ssh-hub/cmd/list" "git.denysoft.de/CubeBit/ssh-hub/cmd/replace" "github.com/spf13/cobra" "github.com/spf13/viper" "os" ) var validArgs = []string{"list", "replace", "add", "delete", "exec"} var rootCmd = &cobra.Command{ Use: "ssh-hub", Short: "ssh-hub is a simple SSH manager. Storing all your ssh commands and managing them", Long: "ssh-hub was created in order to automatically manage ssh commands and different Groups.\n " + "\tCreate groups based on projects, environments etc and add there your ssh commands.\n " + "\tWe will carefully manage all known sessions and groups.", ValidArgs: validArgs, } func Execute() { if err := rootCmd.Execute(); err != nil { fmt.Println(os.Stderr, err) os.Exit(1) } } func init() { cobra.OnInitialize(initCfg) rootCmd.AddCommand(list.Cmd()) rootCmd.AddCommand(exec.Cmd()) rootCmd.AddCommand(add.Cmd()) rootCmd.AddCommand(cobraDelete.Cmd()) rootCmd.AddCommand(replace.Cmd()) } func initCfg() { //TODO: if parameter read config from file provided read from specific file const defaultCfg = "/.ssh-hub-config.json" homePath, err := os.UserHomeDir() cobra.CheckErr(err) if _, err := os.Stat(homePath + defaultCfg); errors.Is(err, os.ErrNotExist) { createdFile, err := os.Create(homePath + defaultCfg) cobra.CheckErr(err) bytes, err := json.Marshal(map[string]string{}) cobra.CheckErr(err) _, err = createdFile.Write(bytes) cobra.CheckErr(err) } else { cobra.CheckErr(err) } configFile, err := os.ReadFile(homePath + defaultCfg) var result map[string]string err = json.Unmarshal(configFile, &result) cobra.CheckErr(err) viper.Set("data", result) } /* ssh-hub ├── ssh - Execute an SSH command (with autocomplete) ├── add - Add a new command or group │ ├── command - Save an SSH command ├── exec - Execute a saved command or group ├── delete - Remove a command or group ├── autocomplete - Enable or generate shell autocompletion └── help - Display help information for commands */