Skip to content

Commit

Permalink
Add orb command
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomorain committed Jun 25, 2018
1 parent 684e99b commit 6906153
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
66 changes: 66 additions & 0 deletions cmd/orb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"context"
"fmt"

"github.com/pkg/errors"

"github.com/machinebox/graphql"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func newOrbCommand() *cobra.Command {

orbListCommand := &cobra.Command{
Use: "list",
Short: "List orbs",
RunE: listOrbs,
}

orbCommand := &cobra.Command{
Use: "orb",
Short: "Operate on orbs",
}

orbCommand.AddCommand(orbListCommand)

return orbCommand
}

func listOrbs(cmd *cobra.Command, args []string) error {

ctx := context.Background()

// Define a structure that matches the result of the GQL
// query, so that we can use mapstructure to convert from
// nested maps to a strongly typed struct.
type orbList struct {
ListOrbs struct {
TotalCount int
}
}

request := graphql.NewRequest(`
query ListOrbs {
orbs(first: 20, after: "") {
totalCount
}
}
`)

client := graphql.NewClient(viper.GetString("endpoint"))

var result orbList

err := client.Run(ctx, request, &result)

if err != nil {
return errors.Wrap(err, "GraphQL query failed")
}

fmt.Printf("Total Number Of Orbs: %d\n", result.ListOrbs.TotalCount)
return nil

}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func addCommands() {
rootCmd.AddCommand(collapseCommand)
rootCmd.AddCommand(configureCommand)
rootCmd.AddCommand(configCmd)
rootCmd.AddCommand(newOrbCommand())

// Cobra has a peculiar default behaviour:
// https://github.com/spf13/cobra/issues/340
Expand Down
16 changes: 11 additions & 5 deletions settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@ func UserHomeDir() string {
// EnsureSettingsFileExists does just that.
func EnsureSettingsFileExists(filepath, filename string) error {
// TODO - handle invalid YAML config files.
_, err := os.Stat(filepath)

if !os.IsNotExist(err) {
fullPath := path.Join(filepath, filename)

_, err := os.Stat(fullPath)

if err == nil {
return nil
}

if err = os.MkdirAll(filepath, 0700); err != nil {
if !os.IsNotExist(err) {
// Filesystem error
return err
}

if _, err = os.Create(path.Join(filepath, filename)); err != nil {
// Create folder
if err = os.MkdirAll(filepath, 0700); err != nil {
return err
}

return nil
_, err = os.Create(fullPath)
return err
}

0 comments on commit 6906153

Please sign in to comment.