Skip to content

Commit

Permalink
Merge pull request #7 from humio/add_repo_create
Browse files Browse the repository at this point in the history
Add option to create a repo
  • Loading branch information
anagrius authored Jun 12, 2019
2 parents 17fb466 + fe9967e commit ba03856
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
27 changes: 26 additions & 1 deletion api/repositories.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package api

import "github.com/shurcooL/graphql"
import (
"fmt"

"github.com/shurcooL/graphql"
)

type Repositories struct {
client *Client
Expand Down Expand Up @@ -45,3 +49,24 @@ func (c *Repositories) List() ([]RepoListItem, error) {

return q.Repositories, graphqlErr
}

func (c *Repositories) Create(name string) error {
var m struct {
CreateRepository struct {
Repository Repository
} `graphql:"createRepository(name: $name)"`
}

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

graphqlErr := c.client.Mutate(&m, variables)

if graphqlErr != nil {
// The graphql error message is vague if the repo already exists, so add a hint.
return fmt.Errorf("%+v. Does the repo already exist?", graphqlErr)
}

return nil
}
1 change: 1 addition & 0 deletions cmd/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func newReposCmd() *cobra.Command {

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

return cmd
}
Expand Down
47 changes: 47 additions & 0 deletions cmd/repos_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 newReposCreateCmd() *cobra.Command {
cmd := cobra.Command{
Use: "create [flags] <repo>",
Short: "Create a repository.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
repoName := args[0]

client := NewApiClient(cmd)

apiErr := client.Repositories().Create(repoName)
exitOnError(cmd, apiErr, "error creating repository")
fmt.Println(fmt.Sprintf("Sucessfully created repo %s", repoName))

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

printRepoTable(cmd, repo)

fmt.Println()
},
}

return &cmd
}

0 comments on commit ba03856

Please sign in to comment.