Files
ssh-hub/cmd/root.go
Denys Seredenko ca2d6bbc67
All checks were successful
Go / build-test-publish (1.23.4) (push) Successful in 43s
added some extra commands
2025-01-14 16:27:50 +01:00

83 lines
2.4 KiB
Go

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"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
)
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.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Welcome to ssh-hub!")
},
}
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())
}
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
│ └── group - Create a group of commands
├── exec - Execute a saved command or group
├── remove - Remove a command or group
├── groups - Manage groups
│ ├── add - Add a command to a group
│ ├── remove - Remove a command from a group
│ └── list - List commands in a group
├── autocomplete - Enable or generate shell autocompletion
└── help - Display help information for commands
*/