From 4a823c1bb2242305aefd413125ea8e5ce4291dff Mon Sep 17 00:00:00 2001 From: Wendell Sun Date: Mon, 9 Oct 2017 00:39:55 +0800 Subject: [PATCH] update README.md file && add flags for add and update command --- README.md | 18 +++++++++++++++++- actions.go | 15 ++++++++++++++- main.go | 9 ++++++++- utils.go | 12 +++++++++--- 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index bb6d7c2..4429d6a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,18 @@ # manssh -Manage your ssh alias configs. +manssh is a conmand line tool for manage your ssh alias config easily, inspire by [storm](https://github.com/emre/storm) project, power by Go. + +## Install + +#### Go + +```shell +go get github.com/xwjdsh/manssh +``` + +#### Manual + +Download it from [releases](https://github.com/xwjdsh/manssh/releases), and extact it to your `PATH` environment. + +## Licence + +[MIT License](https://github.com/xwjdsh/manssh/blob/master/LICENSE) diff --git a/actions.go b/actions.go index b9a335f..b8f9e05 100644 --- a/actions.go +++ b/actions.go @@ -47,6 +47,12 @@ func add(c *cli.Context) error { return cli.NewExitError(fmt.Sprintf("ssh alias('%s') already exists.", newAlias), 1) } host := parseHost(newAlias, hostStr, nil) + if identityFile := c.String("file"); identityFile != "" { + host.IdentityFile = identityFile + } + if proxyCommand := c.String("proxy"); proxyCommand != "" { + host.ProxyCommand = proxyCommand + } hosts = append(hosts, host) if err := saveHosts(hosts); err != nil { return err @@ -71,7 +77,8 @@ func update(c *cli.Context) error { return cli.NewExitError(fmt.Sprintf("ssh alias('%s') not found.", alias), 1) } newUser, newHostname, newPort, newAlias := c.String("user"), c.String("host"), c.String("port"), c.String("alias") - if c.NArg() == 1 && newUser == "" && newHostname == "" && newPort == "" && newAlias == "" { + newIdentityFile, newProxy := c.String("file"), c.String("proxy") + if c.NArg() == 1 && newUser == "" && newHostname == "" && newPort == "" && newAlias == "" && newIdentityFile == "" && newProxy == "" { printErrorFlag() return cli.NewExitError("too few arguments.", 1) } @@ -97,6 +104,12 @@ func update(c *cli.Context) error { } } } + if newIdentityFile != "" { + host.IdentityFile = newIdentityFile + } + if newProxy != "" { + host.ProxyCommand = newProxy + } if err := saveHosts(hosts); err != nil { return err } diff --git a/main.go b/main.go index e01b721..91029e1 100644 --- a/main.go +++ b/main.go @@ -24,7 +24,12 @@ func flags() []cli.Flag { func commands() []cli.Command { return []cli.Command{ - {Name: "add", Usage: "add a new ssh alias record", Action: add}, + {Name: "add", Usage: "add a new ssh alias record", Action: add, + Flags: []cli.Flag{ + cli.StringFlag{Name: "file, f"}, + cli.StringFlag{Name: "proxy, p"}, + }, + }, {Name: "list", Usage: "list all ssh alias records", Action: list}, { Name: "update", Usage: "update existing ssh alias record", Action: update, @@ -33,6 +38,8 @@ func commands() []cli.Command { cli.StringFlag{Name: "host, H"}, cli.StringFlag{Name: "port, p"}, cli.StringFlag{Name: "alias, a"}, + cli.StringFlag{Name: "file, f"}, + cli.StringFlag{Name: "proxy, P"}, }, }, {Name: "delete", Usage: "delete existing ssh alias record", Action: delete}, diff --git a/utils.go b/utils.go index 3bc6ba9..43cf9fd 100644 --- a/utils.go +++ b/utils.go @@ -30,9 +30,15 @@ func saveHosts(hosts []*sshconfig.SSHHost) error { var buffer bytes.Buffer for _, host := range hosts { buffer.WriteString(fmt.Sprintf("Host %s\n", strings.Join(host.Host, " "))) - buffer.WriteString(fmt.Sprintf(" user %s\n", host.User)) - buffer.WriteString(fmt.Sprintf(" hostname %s\n", host.HostName)) - buffer.WriteString(fmt.Sprintf(" port %d\n", host.Port)) + buffer.WriteString(fmt.Sprintf(" User %s\n", host.User)) + buffer.WriteString(fmt.Sprintf(" HostName %s\n", host.HostName)) + buffer.WriteString(fmt.Sprintf(" Port %d\n", host.Port)) + if host.IdentityFile != "" { + buffer.WriteString(fmt.Sprintf(" IdentityFile %s\n", host.IdentityFile)) + } + if host.ProxyCommand != "" { + buffer.WriteString(fmt.Sprintf(" ProxyCommand %s\n", host.ProxyCommand)) + } } if err := ioutil.WriteFile(path, buffer.Bytes(), 0644); err != nil { printErrorFlag()