Skip to content

Commit

Permalink
Added print cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
elliot40404 committed Aug 4, 2023
1 parent 489e3a9 commit c16ae93
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
bin/
*.exe
.vscode
.env.*
.env
*.env
10 changes: 4 additions & 6 deletions commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 22 additions & 0 deletions commands/print.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 6 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<config name>",
},
},
}

Expand Down
55 changes: 34 additions & 21 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit c16ae93

Please sign in to comment.