Skip to content

Commit

Permalink
Better listing of repos
Browse files Browse the repository at this point in the history
  • Loading branch information
anagrius committed Dec 1, 2018
1 parent 235683d commit 9c337e7
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 0 deletions.
47 changes: 47 additions & 0 deletions api/repositories.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package api

import "github.com/shurcooL/graphql"

type Repositories struct {
client *Client
}

type Repository struct {
Name string
RetentionDays int64 `graphql:"timeBasedRetention"`
RetentionSizeGB int64 `graphql:"storageSizeBasedRetention"`
SpaceUsed int64 `graphql:"compressedByteSize"`
}

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

func (c *Repositories) Get(name string) (Repository, error) {
var q struct {
Repository Repository `graphql:"repository(name: $name)"`
}

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

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

return q.Repository, graphqlErr
}

type RepoListItem struct {
Name string
SpaceUsed int64 `graphql:"compressedByteSize"`
}

func (c *Repositories) List() ([]RepoListItem, error) {
var q struct {
Repositories []RepoListItem `graphql:"repositories"`
}

variables := map[string]interface{}{}

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

return q.Repositories, graphqlErr
}
15 changes: 15 additions & 0 deletions cmd/output.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"fmt"

"github.com/ryanuber/columnize"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -34,3 +36,16 @@ func valueOrEmpty(v string) string {
}
return v
}

func ByteCountDecimal(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
}
52 changes: 52 additions & 0 deletions cmd/repos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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/humio/cli/api"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)

func newReposCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "repos",
Short: "Manage repositories",
}

cmd.AddCommand(newReposShowCmd())
cmd.AddCommand(newReposListCmd())

return cmd
}

func printRepoTable(cmd *cobra.Command, repo api.Repository) {

data := [][]string{
[]string{"Name", repo.Name},
[]string{"Space Used", ByteCountDecimal(repo.SpaceUsed)},
[]string{"Retention (Size)", ByteCountDecimal(repo.RetentionSizeGB / 1000000000)},
[]string{"Retention (Days)", fmt.Sprintf("%d", repo.RetentionDays)},
}

w := tablewriter.NewWriter(cmd.OutOrStdout())
w.AppendBulk(data)
w.SetBorder(false)
w.SetColumnSeparator(":")
w.SetColumnAlignment([]int{tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_LEFT})
w.Render()
}
74 changes: 74 additions & 0 deletions cmd/repos_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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 (
"sort"

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

func newReposListCmd() *cobra.Command {
var orderBySize, reverse bool

cmd := cobra.Command{
Use: "list [flags]",
Short: "List repositories.",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
client := NewApiClient(cmd)

repos, apiErr := client.Repositories().List()
exitOnError(cmd, apiErr, "error fetching repository")

sort.Slice(repos, func(i, j int) bool {
var a, b api.RepoListItem
if reverse {
a = repos[i]
b = repos[j]
} else {
a = repos[j]
b = repos[j]
}

if !orderBySize {
return a.Name < b.Name
} else {
return a.SpaceUsed > b.SpaceUsed
}
})

rows := make([][]string, len(repos))
for i, view := range repos {
rows[i] = []string{view.Name, ByteCountDecimal(view.SpaceUsed)}
}

w := tablewriter.NewWriter(cmd.OutOrStdout())
w.SetHeader([]string{"Name", "Space Used"})
w.AppendBulk(rows)
w.SetBorder(false)

w.Render()
cmd.Println()
},
}

cmd.Flags().BoolVarP(&orderBySize, "size", "s", false, "Order by size instead of name")
cmd.Flags().BoolVarP(&reverse, "reverse", "r", true, "Reverse sorting order")

return &cmd
}
43 changes: 43 additions & 0 deletions cmd/repos_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 newReposShowCmd() *cobra.Command {
cmd := cobra.Command{
Use: "show [flags] <repo>",
Short: "Show details about a repository.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
repoName := args[0]

client := NewApiClient(cmd)

repo, apiErr := client.Repositories().Get(repoName)
exitOnError(cmd, apiErr, "error fetching repository")

printRepoTable(cmd, repo)

fmt.Println()
},
}

return &cmd
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Common Management Commands:
rootCmd.AddCommand(newViewsCmd())
rootCmd.AddCommand(newCompletionCmd())
rootCmd.AddCommand(newLicenseCmd())
rootCmd.AddCommand(newReposCmd())
}

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

0 comments on commit 9c337e7

Please sign in to comment.