Skip to content

Commit

Permalink
update README.md file && add flags for add and update command
Browse files Browse the repository at this point in the history
  • Loading branch information
xwjdsh committed Oct 8, 2017
1 parent 42f22c8 commit 4a823c1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 14 additions & 1 deletion actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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},
Expand Down
12 changes: 9 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 4a823c1

Please # to comment.