Skip to content

Commit c00a6d0

Browse files
committed
Basic support for views
1 parent 89091df commit c00a6d0

File tree

5 files changed

+170
-1
lines changed

5 files changed

+170
-1
lines changed

api/views.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package api
2+
3+
import "github.com/shurcooL/graphql"
4+
5+
type Views struct {
6+
client *Client
7+
}
8+
9+
type RolePermission struct {
10+
Role struct {
11+
Name string
12+
}
13+
View struct {
14+
Name string
15+
}
16+
QueryPrefix string
17+
}
18+
19+
type View struct {
20+
Name string
21+
Roles []RolePermission
22+
}
23+
24+
func (c *Client) Views() *Views { return &Views{client: c} }
25+
26+
func (c *Views) Get(name string) (View, error) {
27+
var q struct {
28+
View View `graphql:"searchDomain(name: $name)"`
29+
}
30+
31+
variables := map[string]interface{}{
32+
"name": graphql.String(name),
33+
}
34+
35+
graphqlErr := c.client.Query(&q, variables)
36+
37+
return q.View, graphqlErr
38+
}

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ or
6565
Common commands:
6666
users <subcommand>
6767
parsers <subcommand>
68+
views <subcommand>
6869
`,
6970
}
7071

@@ -85,6 +86,7 @@ Common commands:
8586
rootCmd.AddCommand(newIngestCmd())
8687
rootCmd.AddCommand(newLoginCmd())
8788
rootCmd.AddCommand(newIngestTokensCmd())
89+
rootCmd.AddCommand(newViewsCmd())
8890
}
8991

9092
// initConfig reads in config file and ENV variables if set.

cmd/users_show.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func newUsersShowCmd() *cobra.Command {
3434
user, err := client.Users().Get(username)
3535

3636
if err != nil {
37-
return fmt.Errorf("Error fetching token list: %s", err)
37+
return fmt.Errorf("Error fetching user: %s", err)
3838
}
3939

4040
printUserTable(user)

cmd/views.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright © 2018 Humio Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"fmt"
19+
"os"
20+
21+
"github.com/humio/cli/api"
22+
"github.com/olekukonko/tablewriter"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
func newViewsCmd() *cobra.Command {
27+
cmd := &cobra.Command{
28+
Use: "views",
29+
Short: "Manage views",
30+
}
31+
32+
cmd.AddCommand(newViewsShowCmd())
33+
34+
return cmd
35+
}
36+
37+
func printViewTable(view api.View) {
38+
39+
data := [][]string{
40+
[]string{"Name", view.Name},
41+
}
42+
43+
w := tablewriter.NewWriter(os.Stdout)
44+
w.AppendBulk(data)
45+
w.SetBorder(false)
46+
w.SetColumnSeparator(":")
47+
w.SetColumnAlignment([]int{tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_LEFT})
48+
49+
fmt.Println()
50+
w.Render()
51+
fmt.Println()
52+
}
53+
54+
func printViewRoleTable(view api.View) {
55+
56+
data := [][]string{}
57+
58+
for _, role := range view.Roles {
59+
data = append(data, []string{role.Role.Name, role.QueryPrefix})
60+
}
61+
62+
w := tablewriter.NewWriter(os.Stdout)
63+
w.AppendBulk(data)
64+
w.SetBorder(true)
65+
w.SetHeader([]string{"Role", "Query Prefix"})
66+
w.SetColumnSeparator(":")
67+
w.SetColumnAlignment([]int{tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_LEFT})
68+
69+
fmt.Println()
70+
w.Render()
71+
fmt.Println()
72+
}
73+
74+
func viewRoleNames(user api.User) []string {
75+
names := make([]string, len(user.Roles))
76+
for i, r := range user.Roles {
77+
names[i] = r.Name
78+
}
79+
return names
80+
}

cmd/views_show.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright © 2018 Humio Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/spf13/cobra"
21+
)
22+
23+
func newViewsShowCmd() *cobra.Command {
24+
cmd := cobra.Command{
25+
Use: "show [flags] <view>",
26+
Short: "Show details about a view.",
27+
Args: cobra.ExactArgs(1),
28+
RunE: func(cmd *cobra.Command, args []string) error {
29+
viewName := args[0]
30+
31+
// Get the HTTP client
32+
client := NewApiClient(cmd)
33+
34+
view, err := client.Views().Get(viewName)
35+
36+
if err != nil {
37+
return fmt.Errorf("Error fetching view: %s", err)
38+
}
39+
40+
printViewTable(view)
41+
42+
printViewRoleTable(view)
43+
44+
return nil
45+
},
46+
}
47+
48+
return &cmd
49+
}

0 commit comments

Comments
 (0)