Skip to content

Commit

Permalink
Modify the code with warning
Browse files Browse the repository at this point in the history
  • Loading branch information
xwjdsh committed Jun 14, 2018
1 parent 6764a14 commit 497faab
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
8 changes: 4 additions & 4 deletions cmd/manssh/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
path string
)

func list(c *cli.Context) error {
func listCmd(c *cli.Context) error {
hosts, err := manssh.List(path, manssh.ListOption{
Keywords: c.Args(),
IgnoreCase: c.Bool("ignorecase"),
Expand All @@ -29,7 +29,7 @@ func list(c *cli.Context) error {
return nil
}

func add(c *cli.Context) error {
func addCmd(c *cli.Context) error {
// Check arguments count
if err := utils.ArgumentsCheck(c.NArg(), 1, 2); err != nil {
return printErrorWithHelp(c, err)
Expand Down Expand Up @@ -72,7 +72,7 @@ func add(c *cli.Context) error {
return nil
}

func update(c *cli.Context) error {
func updateCmd(c *cli.Context) error {
if err := utils.ArgumentsCheck(c.NArg(), 1, 2); err != nil {
return printErrorWithHelp(c, err)
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func update(c *cli.Context) error {
return nil
}

func delete(c *cli.Context) error {
func deleteCmd(c *cli.Context) error {
if err := utils.ArgumentsCheck(c.NArg(), 1, -1); err != nil {
return printErrorWithHelp(c, err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/manssh/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func commands() []cli.Command {
{
Name: "add",
Usage: "Add a new ssh alias record",
Action: add,
Action: addCmd,
Aliases: []string{"a"},
Flags: []cli.Flag{
cli.GenericFlag{Name: "config, c", Value: &kvFlag{}},
Expand All @@ -19,7 +19,7 @@ func commands() []cli.Command {
{
Name: "list",
Usage: "List all or query ssh alias records",
Action: list,
Action: listCmd,
Aliases: []string{"l"},
Flags: []cli.Flag{
cli.BoolFlag{Name: "ignorecase, ic", Usage: "ignore case while searching"},
Expand All @@ -29,7 +29,7 @@ func commands() []cli.Command {
{
Name: "update",
Usage: "Update the specified ssh alias",
Action: update,
Action: updateCmd,
Aliases: []string{"u"},
Flags: []cli.Flag{
cli.GenericFlag{Name: "config, c", Value: &kvFlag{}},
Expand All @@ -41,7 +41,7 @@ func commands() []cli.Command {
{
Name: "delete",
Usage: "Delete one or more ssh aliases",
Action: delete,
Action: deleteCmd,
Aliases: []string{"d"},
},
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/manssh/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func printErrorWithHelp(c *cli.Context, err error) error {
}

func printHosts(showPath bool, hosts []*manssh.HostConfig) {
aliases := []string{}
noConnectAliases := []string{}
var aliases []string
var noConnectAliases []string
hostMap := map[string]*manssh.HostConfig{}

for _, host := range hosts {
Expand All @@ -47,8 +47,8 @@ func printHost(showPath bool, host *manssh.HostConfig) {
fmt.Printf("\t%s", color.MagentaString(host.Alias))
if showPath && len(host.PathMap) > 0 {

paths := []string{}
for path, _ := range host.PathMap {
var paths []string
for path := range host.PathMap {
if homeDir := utils.GetHomeDir(); strings.HasPrefix(path, homeDir) {
path = strings.Replace(path, homeDir, "~", 1)
}
Expand Down
2 changes: 1 addition & 1 deletion models.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewHostConfig(alias, path string, host *ssh_config.Host) *HostConfig {
return &HostConfig{
Alias: alias,
Path: path,
PathMap: map[string][]*ssh_config.Host{path: []*ssh_config.Host{host}},
PathMap: map[string][]*ssh_config.Host{path: {host}},
OwnConfig: map[string]string{},
ImplicitConfig: map[string]string{},
}
Expand Down
40 changes: 20 additions & 20 deletions sshconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func readFile(p string) (*ssh_config.Config, error) {
}

func deleteHostFromConfig(config *ssh_config.Config, host *ssh_config.Host) {
hs := []*ssh_config.Host{}
var hs []*ssh_config.Host
for _, h := range config.Hosts {
if h == host {
continue
Expand Down Expand Up @@ -150,7 +150,7 @@ func List(p string, lo ListOption) ([]*HostConfig, error) {
return nil, err
}

result := []*HostConfig{}
var result []*HostConfig
for _, host := range aliasMap {
values := []string{host.Alias}
for _, v := range host.OwnConfig {
Expand Down Expand Up @@ -202,12 +202,12 @@ func Add(p string, ao *AddOption) (*HostConfig, error) {
return nil, err
}
}
nodes := []ssh_config.Node{}
var nodes []ssh_config.Node
for k, v := range ao.Config {
nodes = append(nodes, ssh_config.NewKV(strings.ToLower(k), v))
}
// Parse connect string
user, hostname, port := utils.ParseConnct(ao.Connect)
user, hostname, port := utils.ParseConnect(ao.Connect)
if user != "" {
nodes = append(nodes, ssh_config.NewKV("user", user))
}
Expand Down Expand Up @@ -268,7 +268,7 @@ func Update(p string, uo *UpdateOption) (*HostConfig, error) {

if uo.Connect != "" {
// Parse connect string
user, hostname, port := utils.ParseConnct(uo.Connect)
user, hostname, port := utils.ParseConnect(uo.Connect)
if user != "" {
updateHost.OwnConfig["user"] = user
}
Expand All @@ -288,9 +288,9 @@ func Update(p string, uo *UpdateOption) (*HostConfig, error) {
}
}

for path, hosts := range updateHost.PathMap {
for fp, hosts := range updateHost.PathMap {
for i, host := range hosts {
if path == updateHost.Path {
if fp == updateHost.Path {
pattern, _ := ssh_config.NewPattern(uo.NewAlias)
newHost := &ssh_config.Host{
Patterns: []*ssh_config.Pattern{pattern},
Expand All @@ -302,13 +302,13 @@ func Update(p string, uo *UpdateOption) (*HostConfig, error) {
if i == 0 {
*host = *newHost
} else {
deleteHostFromConfig(configMap[path], host)
deleteHostFromConfig(configMap[fp], host)
}
} else {
if i == 0 {
configMap[path].Hosts = append(configMap[path].Hosts, newHost)
configMap[fp].Hosts = append(configMap[fp].Hosts, newHost)
}
patterns := []*ssh_config.Pattern{}
var patterns []*ssh_config.Pattern
for _, pattern := range host.Patterns {
if pattern.String() != uo.NewAlias {
patterns = append(patterns, pattern)
Expand All @@ -318,9 +318,9 @@ func Update(p string, uo *UpdateOption) (*HostConfig, error) {
}
} else {
if len(host.Patterns) == 1 {
deleteHostFromConfig(configMap[path], host)
deleteHostFromConfig(configMap[fp], host)
} else {
patterns := []*ssh_config.Pattern{}
var patterns []*ssh_config.Pattern
for _, pattern := range host.Patterns {
if pattern.String() != uo.NewAlias {
patterns = append(patterns, pattern)
Expand All @@ -329,7 +329,7 @@ func Update(p string, uo *UpdateOption) (*HostConfig, error) {
host.Patterns = patterns
}
}
if err := writeConfig(path, configMap[path]); err != nil {
if err := writeConfig(fp, configMap[fp]); err != nil {
return nil, err
}
}
Expand All @@ -351,16 +351,16 @@ func Delete(p string, aliases ...string) ([]*HostConfig, error) {
return nil, err
}

deleteHosts := []*HostConfig{}
var deleteHosts []*HostConfig
for _, alias := range aliases {
deleteHost := aliasMap[alias]
deleteHosts = append(deleteHosts, deleteHost)
for path, hosts := range deleteHost.PathMap {
for fp, hosts := range deleteHost.PathMap {
for _, host := range hosts {
if len(host.Patterns) == 1 {
deleteHostFromConfig(configMap[path], host)
deleteHostFromConfig(configMap[fp], host)
} else {
patterns := []*ssh_config.Pattern{}
var patterns []*ssh_config.Pattern
for _, pattern := range host.Patterns {
if pattern.String() != alias {
patterns = append(patterns, pattern)
Expand All @@ -369,7 +369,7 @@ func Delete(p string, aliases ...string) ([]*HostConfig, error) {
host.Patterns = patterns
}
}
if err := writeConfig(path, configMap[path]); err != nil {
if err := writeConfig(fp, configMap[fp]); err != nil {
return nil, err
}
}
Expand All @@ -382,9 +382,9 @@ func checkAlias(aliasMap map[string]*HostConfig, expectExist bool, aliases ...st
for _, alias := range aliases {
ok := aliasMap[alias] != nil
if !ok && expectExist {
return fmt.Errorf("alias[%s] not found.", alias)
return fmt.Errorf("alias[%s] not found", alias)
} else if ok && !expectExist {
return fmt.Errorf("alias[%s] already exists.", alias)
return fmt.Errorf("alias[%s] already exists", alias)
}
}
return nil
Expand Down
10 changes: 5 additions & 5 deletions sshconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ func TestList(t *testing.T) {
return
}
Convey("init", t, func() {
list, err := List(f.Name(), nil)
list, err := List(f.Name(), ListOption{})
So(err, ShouldBeNil)
So(len(list), ShouldEqual, 3)

list, err = List(f.Name(), []string{"77"})
list, err = List(f.Name(), ListOption{Keywords: []string{"77"}})
So(err, ShouldBeNil)
So(len(list), ShouldEqual, 2)

list, err = List(f.Name(), []string{"root"})
list, err = List(f.Name(), ListOption{Keywords: []string{"root"}})
So(err, ShouldBeNil)
So(len(list), ShouldEqual, 2)

list, err = List(f.Name(), []string{"root"}, true)
list, err = List(f.Name(), ListOption{Keywords: []string{"root"}, IgnoreCase: true})
So(err, ShouldBeNil)
So(len(list), ShouldEqual, 3)

list, err = List(f.Name(), []string{"test", "77", "30"})
list, err = List(f.Name(), ListOption{Keywords: []string{"test", "77", "30"}})
So(err, ShouldBeNil)
So(len(list), ShouldEqual, 1)
})
Expand Down

0 comments on commit 497faab

Please sign in to comment.