Skip to content

Commit

Permalink
Basic support for views
Browse files Browse the repository at this point in the history
  • Loading branch information
anagrius committed Nov 27, 2018
1 parent 89091df commit c00a6d0
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 1 deletion.
38 changes: 38 additions & 0 deletions api/views.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package api

import "github.com/shurcooL/graphql"

type Views struct {
client *Client
}

type RolePermission struct {
Role struct {
Name string
}
View struct {
Name string
}
QueryPrefix string
}

type View struct {
Name string
Roles []RolePermission
}

func (c *Client) Views() *Views { return &Views{client: c} }

func (c *Views) Get(name string) (View, error) {
var q struct {
View View `graphql:"searchDomain(name: $name)"`
}

variables := map[string]interface{}{
"name": graphql.String(name),
}

graphqlErr := c.client.Query(&q, variables)

return q.View, graphqlErr
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ or
Common commands:
users <subcommand>
parsers <subcommand>
views <subcommand>
`,
}

Expand All @@ -85,6 +86,7 @@ Common commands:
rootCmd.AddCommand(newIngestCmd())
rootCmd.AddCommand(newLoginCmd())
rootCmd.AddCommand(newIngestTokensCmd())
rootCmd.AddCommand(newViewsCmd())
}

// initConfig reads in config file and ENV variables if set.
Expand Down
2 changes: 1 addition & 1 deletion cmd/users_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func newUsersShowCmd() *cobra.Command {
user, err := client.Users().Get(username)

if err != nil {
return fmt.Errorf("Error fetching token list: %s", err)
return fmt.Errorf("Error fetching user: %s", err)
}

printUserTable(user)
Expand Down
80 changes: 80 additions & 0 deletions cmd/views.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright © 2018 Humio Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"os"

"github.com/humio/cli/api"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)

func newViewsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "views",
Short: "Manage views",
}

cmd.AddCommand(newViewsShowCmd())

return cmd
}

func printViewTable(view api.View) {

data := [][]string{
[]string{"Name", view.Name},
}

w := tablewriter.NewWriter(os.Stdout)
w.AppendBulk(data)
w.SetBorder(false)
w.SetColumnSeparator(":")
w.SetColumnAlignment([]int{tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_LEFT})

fmt.Println()
w.Render()
fmt.Println()
}

func printViewRoleTable(view api.View) {

data := [][]string{}

for _, role := range view.Roles {
data = append(data, []string{role.Role.Name, role.QueryPrefix})
}

w := tablewriter.NewWriter(os.Stdout)
w.AppendBulk(data)
w.SetBorder(true)
w.SetHeader([]string{"Role", "Query Prefix"})
w.SetColumnSeparator(":")
w.SetColumnAlignment([]int{tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_LEFT})

fmt.Println()
w.Render()
fmt.Println()
}

func viewRoleNames(user api.User) []string {
names := make([]string, len(user.Roles))
for i, r := range user.Roles {
names[i] = r.Name
}
return names
}
49 changes: 49 additions & 0 deletions cmd/views_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright © 2018 Humio Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

func newViewsShowCmd() *cobra.Command {
cmd := cobra.Command{
Use: "show [flags] <view>",
Short: "Show details about a view.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
viewName := args[0]

// Get the HTTP client
client := NewApiClient(cmd)

view, err := client.Views().Get(viewName)

if err != nil {
return fmt.Errorf("Error fetching view: %s", err)
}

printViewTable(view)

printViewRoleTable(view)

return nil
},
}

return &cmd
}

0 comments on commit c00a6d0

Please sign in to comment.