Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xwjdsh committed Oct 2, 2017
0 parents commit 6fad2fb
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
manssh
manssh.exe
35 changes: 35 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"os"

"github.com/urfave/cli"
)

func main() {
app := cli.NewApp()
app.Name = name
app.Usage = useage
app.Version = version
app.Flags = flags()
app.Commands = commands()
app.Run(os.Args)
}

func flags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{Name: "config, c", Value: fmt.Sprintf("%s/.ssh/config", os.Getenv("HOME")), Destination: &path},
}
}

func commands() []cli.Command {
return []cli.Command{
{Name: "add", Usage: "add a record to ssh config", Action: add},
{Name: "list", Usage: "list all existing ssh config record", Action: list},
{Name: "update", Usage: "update existing ssh config record", Action: update},
{Name: "delete", Usage: "delete existing ssh config record", Action: delete},
{Name: "rename", Usage: "rename existing ssh config record", Action: rename},
{Name: "export", Usage: "export ssh config record", Action: export},
}
}
86 changes: 86 additions & 0 deletions sshconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"fmt"
"io/ioutil"
"strings"

"github.com/fatih/color"
"github.com/mikkeloscar/sshconfig"
"github.com/urfave/cli"
)

var (
path string
whiteBoldColor = color.New(color.FgWhite, color.Bold)
yellowBoldColor = color.New(color.FgYellow, color.Bold)
)

func formatHost(host *sshconfig.SSHHost) string {
return fmt.Sprintf("%s@%s:%d", host.User, host.HostName, host.Port)
}

func list(c *cli.Context) error {
hosts, _ := sshconfig.ParseSSHConfig(path)
if len(c.Args()) > 0 {
searchHosts := []*sshconfig.SSHHost{}
for _, host := range hosts {
values := []string{host.HostName, host.User, fmt.Sprintf("%d", host.Port)}
values = append(values, host.Host...)
if query(values, c.Args()) {
searchHosts = append(searchHosts, host)
}
}
hosts = searchHosts
}
whiteBoldColor.Printf("Display %d records:\n\n", len(hosts))
for _, host := range hosts {
yellowBoldColor.Printf(" %s", strings.Join(host.Host, " "))
fmt.Printf(" -> %s\n\n", formatHost(host))
}
return nil
}

func add(c *cli.Context) error {
fmt.Println("add command")
return nil
}

func update(c *cli.Context) error {
fmt.Println("update command")
return nil
}

func delete(c *cli.Context) error {
fmt.Println("delete command")
return nil
}

func rename(c *cli.Context) error {
fmt.Println("rename command")
return nil
}

func export(c *cli.Context) error {
if len(c.Args()) == 0 {
cli.ShowSubcommandHelp(c)
fmt.Println()
return cli.NewExitError("arguments is missing", 1)
}

if len(c.Args()) > 1 {
cli.ShowSubcommandHelp(c)
fmt.Println()
return cli.NewExitError("too many arguments", 1)
}

data, err := ioutil.ReadFile(path)
if err != nil {
return cli.NewExitError(err, 1)
}
err = ioutil.WriteFile(c.Args().First(), data, 0644)
if err != nil {
return cli.NewExitError(err, 1)
}
return nil
}
27 changes: 27 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import "strings"

const (
name = "manssh"
version = "0.0.1"
useage = "management ssh config easier"
)

func query(values, keys []string) bool {
for _, key := range keys {
if !contains(values, key) {
return false
}
}
return true
}

func contains(values []string, key string) bool {
for _, value := range values {
if strings.Contains(value, key) {
return true
}
}
return false
}

0 comments on commit 6fad2fb

Please # to comment.