-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters