* added list and exec commands

This commit is contained in:
Denys Seredenko
2025-01-12 11:38:01 +01:00
commit b8f7766de8
6 changed files with 200 additions and 0 deletions

47
cmd/exec/exec.go Normal file
View File

@@ -0,0 +1,47 @@
package exec
import (
"errors"
"fmt"
"github.com/creack/pty"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io"
"os"
"os/exec"
)
var execCmd = &cobra.Command{
Use: "exec",
Short: "Execute command of specific alias",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
data := viper.GetStringMapString("data")
if _, ok := data[args[0]]; !ok {
return errors.New("such alias was not found")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
data := viper.GetStringMapString("data")
command := exec.Command("sh", "-c", data[args[0]])
ptmx, err := pty.Start(command)
if err != nil {
fmt.Fprintf(os.Stderr, "Error starting PTY: %v\n", err)
os.Exit(1)
}
defer func() { _ = ptmx.Close() }()
go func() { _, _ = io.Copy(ptmx, os.Stdin) }()
_, _ = io.Copy(os.Stdout, ptmx)
},
}
func Cmd() *cobra.Command {
return execCmd
}

29
cmd/list/list.go Normal file
View File

@@ -0,0 +1,29 @@
package list
import (
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
)
var listCmd = &cobra.Command{
Use: "list",
Short: "List all known commands and their aliases",
Run: func(cmd *cobra.Command, args []string) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Alias", "Command"})
commands := viper.GetStringMapString("data")
for alias, command := range commands {
t.AppendRow(table.Row{alias, command})
}
t.Render()
},
}
func Cmd() *cobra.Command {
return listCmd
}

78
cmd/root.go Normal file
View File

@@ -0,0 +1,78 @@
package cmd
import (
"encoding/json"
"errors"
"fmt"
"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())
}
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
*/