From c16ae93960c9753795392e067baea8d38f22feb6 Mon Sep 17 00:00:00 2001 From: Elliot <67360013+elliot40404@users.noreply.github.com> Date: Fri, 4 Aug 2023 22:29:47 +0530 Subject: [PATCH] Added print cmd --- .gitignore | 5 +++++ commands/list.go | 10 ++++----- commands/print.go | 22 +++++++++++++++++++ main.go | 14 +++++------- pkg/utils/utils.go | 55 ++++++++++++++++++++++++++++------------------ 5 files changed, 71 insertions(+), 35 deletions(-) create mode 100644 commands/print.go diff --git a/.gitignore b/.gitignore index e660fd9..41a46e6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,6 @@ bin/ +*.exe +.vscode +.env.* +.env +*.env \ No newline at end of file diff --git a/commands/list.go b/commands/list.go index 81bc514..01b8c7d 100644 --- a/commands/list.go +++ b/commands/list.go @@ -8,12 +8,10 @@ import ( ) func List(cCtx *cli.Context) error { - for idx, name := range utils.GetSSHConfigs(false) { - if cCtx.Bool("count") { - fmt.Printf("%v. %v\n", idx+1, name) - } else { - fmt.Printf("%v\n", name) - } + var output string + for _, name := range utils.GetSSHConfigs(false) { + output += fmt.Sprintf("%v\n", name) } + fmt.Print(output) return nil } diff --git a/commands/print.go b/commands/print.go new file mode 100644 index 0000000..651261d --- /dev/null +++ b/commands/print.go @@ -0,0 +1,22 @@ +package commands + +import ( + "fmt" + + "github.com/elliot40404/ssm/pkg/utils" + "github.com/urfave/cli/v2" +) + +func Print(cCtx *cli.Context) error { + // parse first argument as config name + // if config name is not provided, prompt for it + configName := cCtx.Args().First() + if configName == "" { + configName = selectMenu("Select Config", utils.GetSSHConfigs(false), false) + } + // get config + config := utils.GetSSHConfig(configName) + // print config + fmt.Println(config) + return nil +} diff --git a/main.go b/main.go index 5586306..270ce35 100644 --- a/main.go +++ b/main.go @@ -19,20 +19,18 @@ func main() { Aliases: []string{"ls"}, Usage: "list available ssh configs", Action: commands.List, - Flags: []cli.Flag{ - &cli.BoolFlag{ - Name: "count", - Value: false, - Usage: "print serial numbers", - Aliases: []string{"c"}, - }, - }, }, { Name: "add", Usage: "add new ssh config", Action: commands.Add, }, + { + Name: "print", + Usage: "print ssh config", + Action: commands.Print, + ArgsUsage: "", + }, }, } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index c0db909..479b0ee 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -9,27 +9,22 @@ import ( "strings" ) +var categoryRegex = regexp.MustCompile(`(?m)^#.+$`) + func GetSSHDir(configs bool) string { - var result string home, err := os.UserHomeDir() if err != nil { log.Fatal("Unable to fetch home dir") } + result := home + "/.ssh" if configs { - result = home + "/.ssh/config.d" - } else { - result = home + "/.ssh" + result += "/config.d" } - return result } func GetSSHFile() string { - home, err := os.UserHomeDir() - if err != nil { - log.Fatal("Unable to fetch home dir") - } - return home + "/.ssh/config" + return GetSSHDir(false) + "/config" } func ReadSSHConfig(path string) string { @@ -72,17 +67,18 @@ func CheckIfExists(basepath string) { } func GetSSHConfigs(withBase bool) []string { - var items []string - configs, err := os.ReadDir(GetSSHDir(true)) + sshDir := GetSSHDir(true) + configs, err := os.ReadDir(sshDir) if err != nil { - log.Fatal("Error Reading configs") + log.Fatal("Error reading configs") } + items := make([]string, 0, len(configs)) for _, config := range configs { - if !withBase { - items = append(items, config.Name()) - } else { - items = append(items, GetSSHDir(true)+"/"+config.Name()) + item := config.Name() + if withBase { + item = sshDir + "/" + item } + items = append(items, item) } return items } @@ -101,13 +97,11 @@ func BasicPrompt(prompt string, nullable bool) string { } func GetCategories() []string { - var categories []string - re := regexp.MustCompile(`(?m)^#.+`) - categories = append(categories, re.FindAllString(ReadSSHConfig(GetSSHDir(false)), -1)...) - return categories + return categoryRegex.FindAllString(ReadSSHConfig(GetSSHDir(false)), -1) } func GetSSHKeys() []string { + // TODO: Also consider other keys var categories []string files, err := os.ReadDir(GetSSHDir(false)) if err != nil { @@ -163,3 +157,22 @@ func LinkConfig(name, category string) { } file.WriteAt(content, 0) } + +func GetSSHConfig(name string) string { + // read file + file, err := os.Open(GetSSHDir(true) + "/" + name) + if err != nil { + log.Fatal("Error Reading Config") + } + defer file.Close() + buffer := make([]byte, 1024) + var data string + for { + n, err := file.Read(buffer) + if err != nil { + break // End of file reached or an error occurred + } + data = string(buffer[:n]) + } + return data +}