Skip to content

Commit

Permalink
Fixes discrepancy with collins-cli
Browse files Browse the repository at this point in the history
This fixes two issues with the query command. The first issue being that
if -a is passed more than once we respect it. The second being that we
now match the collins-cli output and show the values for -a if it is
passed.

Fixes: #5
  • Loading branch information
michaeljs1990 committed Feb 8, 2019
1 parent dc2a3f0 commit 5c351f8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
35 changes: 24 additions & 11 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func querySubcommand() cli.Command {
Usage: "Asset status (and optional state after :)",
Category: "Query options",
},
cli.StringFlag{
cli.StringSliceFlag{
Name: "a, attribute",
Usage: "Arbitrary attributes and values to match in query. : between key and value",
Category: "Query options",
Expand Down Expand Up @@ -133,16 +133,6 @@ func queryBuildOptions(c *cli.Context, hostname string) collins.AssetFindOpts {
opts.Status = status[0]
}

if c.IsSet("attribute") {
attribute := strings.Split(c.String("attribute"), ":")
// HACK: Check to make sure the user didn't slip in a hostname since the ruby
// client seems to have some nice magic around how it handles this that isn't
// possible in go without a bit of work.
if attribute[0] != "hostname" || hostname == "" {
opts.Attribute = strings.Join(attribute, ";")
}
}

if c.IsSet("type") {
opts.Type = c.String("type")
}
Expand Down Expand Up @@ -189,6 +179,19 @@ func buildOptionsQuery(c *cli.Context, hostname string) string {
cql = append(cql, "(HOSTNAME = "+hostname+")")
}

if c.IsSet("attribute") || c.IsSet("a") {
for _, attr := range c.StringSlice("attribute") {
attrSplit := strings.SplitN(attr, ":", 2)
if len(attrSplit) != 2 {
logAndDie("--attribute and -a requires attribute:value, missing :value")
}
attrKey := strings.ToUpper(attrSplit[0])
attrValue := strings.ToUpper(attrSplit[1])

cql = append(cql, "("+attrKey+" = "+attrValue+")")
}
}

operation := c.String("operation")
if operation != "AND" && operation != "OR" {
logAndDie("Operation (or o) flag may only be set to AND or OR")
Expand All @@ -210,6 +213,16 @@ func queryGetColumns(c *cli.Context) []string {
"secondary_role",
}

if c.IsSet("attribute") || c.IsSet("a") {
for _, attr := range c.StringSlice("attribute") {
attrSplit := strings.SplitN(attr, ":", 2)
if len(attrSplit) != 2 {
logAndDie("--attribute and -a requires attribute:value, missing :value")
}
uniqueSet = uniqueSet.Add(attrSplit[0])
}
}

if c.IsSet("columns") {
uniqueSet = strings.Split(c.String("columns"), ",")
}
Expand Down
25 changes: 2 additions & 23 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func queryContext(fn func(*cli.Context), cmd []string) {
Usage: "Asset status (and optional state after :)",
Category: "Query options",
},
cli.StringFlag{
cli.StringSliceFlag{
Name: "a, attribute",
Usage: "Arbitrary attributes and values to match in query. : between key and value",
Category: "Query options",
Expand Down Expand Up @@ -291,15 +291,10 @@ func TestBuildOptionsQuery(t *testing.T) {

queryContext(func(ctx *cli.Context) {
out := buildOptionsQuery(ctx, "dev")
expected := "(HOSTNAME = dev)"
expected := "(HOSTNAME = dev) AND (TEST = THING)"
if out != expected {
t.Error("Expected ", expected, " got ", out)
}

out2 := queryBuildOptions(ctx, "dev")
if out2.Attribute != "test;thing" {
t.Error("Attribute was net set properly from passed in flag")
}
}, []string{"cmd", "query", "-a", "test:thing", "dev"})

queryContext(func(ctx *cli.Context) {
Expand Down Expand Up @@ -342,29 +337,13 @@ func TestQueryBuildOptions(t *testing.T) {

}, []string{"cmd", "query", "-n", "somenode", "-T", "SOME_TYPE"})

queryContext(func(ctx *cli.Context) {
out := queryBuildOptions(ctx, "")
if out.Attribute != "gabe;test" {
t.Error("Unable to set attribute when building options")
}
}, []string{"cmd", "query", "-a", "gabe:test"})

queryContext(func(ctx *cli.Context) {
out := queryBuildOptions(ctx, "hi")
want := ""
if out.Attribute != "" {
t.Error("Want:", want, " Got:", out.Attribute)
}
}, []string{"cmd", "query", "-a", "hostname:test", "hi"})

queryContext(func(ctx *cli.Context) {
out := queryBuildOptions(ctx, "hi")
want := "pool;test"
if out.Attribute != want {
t.Error("Want:", want, " Got:", out.Attribute)
}
}, []string{"cmd", "query", "-a", "pool:test", "hi"})

}

func TestQuery(t *testing.T) {
Expand Down

0 comments on commit 5c351f8

Please sign in to comment.