Skip to content

Commit

Permalink
Add ignore case option for list command
Browse files Browse the repository at this point in the history
  • Loading branch information
xwjdsh committed Jan 16, 2018
1 parent 5e364a5 commit c937480
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/manssh/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
)

func list(c *cli.Context) error {
hosts := manssh.List(path, c.Args()...)
hosts := manssh.List(path, c.Args(), c.Bool("ignorecase"))
printSuccessFlag()
printMessage("Listing %d records.\n\n", len(hosts))
printHosts(hosts)
Expand Down
3 changes: 3 additions & 0 deletions cmd/manssh/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func commands() []cli.Command {
Usage: "List or query SSH alias records",
Action: list,
Aliases: []string{"l"},
Flags: []cli.Flag{
cli.BoolFlag{Name: "ignorecase, ic", Usage: "ignore case while searching"},
},
},
{
Name: "update",
Expand Down
4 changes: 2 additions & 2 deletions sshconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func ParseConfig(path string) (*ssh_config.Config, map[string]*ssh_config.Host)
}

// List ssh alias, filter by optional keyword
func List(path string, keywords ...string) []*HostConfig {
func List(path string, keywords []string, ignoreCase ...bool) []*HostConfig {
cfg, _ := ParseConfig(path)
hosts := []*HostConfig{}

Expand Down Expand Up @@ -74,7 +74,7 @@ func List(path string, keywords ...string) []*HostConfig {
if isGlobal && len(h.Config) == 0 {
continue
}
if len(keywords) > 0 && !Query(values, keywords) {
if keywords != nil && len(keywords) > 0 && !Query(values, keywords, len(ignoreCase) > 0 && ignoreCase[0]) {
continue
}
if !isGlobal {
Expand Down
28 changes: 15 additions & 13 deletions sshconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Host test2
Host test3
hostname 192.168.99.30
user root
user ROOT
port 77
`

Expand All @@ -35,9 +35,11 @@ func TestList(t *testing.T) {
return
}
Convey("init", t, func() {
So(len(List(f.Name())), ShouldEqual, 3)
So(len(List(f.Name(), "77")), ShouldEqual, 2)
So(len(List(f.Name(), "test", "77", "30")), ShouldEqual, 1)
So(len(List(f.Name(), nil)), ShouldEqual, 3)
So(len(List(f.Name(), []string{"77"})), ShouldEqual, 2)
So(len(List(f.Name(), []string{"root"})), ShouldEqual, 2)
So(len(List(f.Name(), []string{"root"}, true)), ShouldEqual, 3)
So(len(List(f.Name(), []string{"test", "77", "30"})), ShouldEqual, 1)
})
}

Expand All @@ -56,7 +58,7 @@ func TestAdd(t *testing.T) {
add := &HostConfig{Aliases: "test4", Connect: "root@2.2.2.2"}
So(Add(f.Name(), add), ShouldBeNil)
So(add, ShouldResemble, &HostConfig{Aliases: "test4", Connect: "root@2.2.2.2:22", Config: map[string]string{}})
So(List(f.Name(), "test4"), ShouldResemble, []*HostConfig{add})
So(List(f.Name(), []string{"test4"}), ShouldResemble, []*HostConfig{add})
})
}

Expand All @@ -80,14 +82,14 @@ func TestUpdate(t *testing.T) {
So(Update(f.Name(), update3, "test4"), ShouldBeNil)

So(update1, ShouldResemble, &HostConfig{Aliases: "test1", Connect: "root@2.2.2.2:77", Config: map[string]string{}})
So(List(f.Name(), "test1"), ShouldResemble, []*HostConfig{update1})
So(List(f.Name(), []string{"test1"}), ShouldResemble, []*HostConfig{update1})

So(update2, ShouldResemble, &HostConfig{Aliases: "test2", Connect: "wendell@192.168.99.20:77", Config: map[string]string{}})
So(List(f.Name(), "test2"), ShouldResemble, []*HostConfig{update2})
So(List(f.Name(), []string{"test2"}), ShouldResemble, []*HostConfig{update2})

So(update3, ShouldResemble, &HostConfig{Aliases: "test4", Connect: "root@192.168.99.30:77", Config: map[string]string{}})
So(List(f.Name(), "test3"), ShouldBeEmpty)
So(List(f.Name(), "test4"), ShouldResemble, []*HostConfig{update3})
So(update3, ShouldResemble, &HostConfig{Aliases: "test4", Connect: "ROOT@192.168.99.30:77", Config: map[string]string{}})
So(List(f.Name(), []string{"test3"}), ShouldBeEmpty)
So(List(f.Name(), []string{"test4"}), ShouldResemble, []*HostConfig{update3})
})
}

Expand All @@ -104,8 +106,8 @@ func TestDelete(t *testing.T) {
So(Delete(f.Name(), "test4"), ShouldNotBeNil)

So(Delete(f.Name(), "test1", "test2"), ShouldBeNil)
So(List(f.Name(), "test1"), ShouldBeEmpty)
So(List(f.Name(), "test2"), ShouldBeEmpty)
So(len(List(f.Name())), ShouldEqual, 1)
So(List(f.Name(), []string{"test1"}), ShouldBeEmpty)
So(List(f.Name(), []string{"test2"}), ShouldBeEmpty)
So(len(List(f.Name(), nil)), ShouldEqual, 1)
})
}
12 changes: 9 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,23 @@ func ArgumentsCheck(argCount, min, max int) error {
}

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

func contains(values []string, key string) bool {
func contains(values []string, key string, ignoreCase bool) bool {
if ignoreCase {
key = strings.ToLower(key)
}
for _, value := range values {
if ignoreCase {
value = strings.ToLower(value)
}
if strings.Contains(value, key) {
return true
}
Expand Down
11 changes: 7 additions & 4 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ func TestQuery(t *testing.T) {
Convey("init", t, func() {
values := []string{"test1", "test2", "another1", "another2"}
Convey("check", func() {
So(Query(values, []string{"test", "2"}), ShouldBeTrue)
So(Query(values, []string{"another", "1"}), ShouldBeTrue)
So(Query(values, []string{"test", "2"}, false), ShouldBeTrue)
So(Query(values, []string{"another", "2"}, false), ShouldBeTrue)

So(Query(values, []string{"test", "3"}), ShouldBeFalse)
So(Query(values, []string{"another", "3"}), ShouldBeFalse)
So(Query(values, []string{"TEST", "1"}, false), ShouldBeFalse)
So(Query(values, []string{"Another", "1"}, false), ShouldBeFalse)

So(Query(values, []string{"TEST", "1"}, true), ShouldBeTrue)
So(Query(values, []string{"Another", "1"}, true), ShouldBeTrue)
})
})
}
Expand Down

0 comments on commit c937480

Please # to comment.