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 8c7c64f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 8 deletions.
3 changes: 2 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"fmt"

"github.com/CircleCI-Public/circleci-cli/logger"
"github.com/machinebox/graphql"
Expand All @@ -15,7 +16,7 @@ func NewClient(endpoint string, logger *logger.Logger) *graphql.Client {
client := graphql.NewClient(endpoint)

client.Log = func(s string) {
logger.Debug(s)
logger.Debug(fmt.Sprintf("[machinebox/graphql] %s", s))
}

return client
Expand Down
101 changes: 101 additions & 0 deletions cmd/orb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package cmd

import (
"context"
"fmt"

"github.com/CircleCI-Public/circleci-cli/client"
"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 {
Orbs struct {
TotalCount int
Edges []struct {
Cursor string
Node struct {
Name string
}
}
PageInfo struct {
HasNextPage bool
}
}
}

request := graphql.NewRequest(`
query ListOrbs ($after: String!) {
orbs(first: 20, after: $after) {
totalCount,
edges {
cursor,
node {
name
}
}
pageInfo {
hasNextPage
}
}
}
`)

client := client.NewClient(viper.GetString("endpoint"), Logger)

var result orbList
currentCursor := ""

for {
request.Var("after", currentCursor)
err := client.Run(ctx, request, &result)

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

Logger.Prettyify(result)

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

for i := range result.Orbs.Edges {
edge := result.Orbs.Edges[i]
currentCursor = edge.Cursor
Logger.Infof("Orb: %s\n", edge.Node.Name)
}

if !result.Orbs.PageInfo.HasNextPage {
break
}
}
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
6 changes: 4 additions & 2 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ type Logger struct {
// Later we pass this to client.NewClient so it can also log.
// By default debug and error go to os.Stderr, and info goes to os.Stdout
func NewLogger(verbose bool) *Logger {
return &Logger{
result := &Logger{
log.New(os.Stderr, "", 0),
log.New(os.Stdout, "", 0),
log.New(os.Stderr, "", 0),
verbose,
}
result.Debug("Verbose Logging: %t", verbose)
return result
}

// Debug prints a formatted message to stderr only if verbose is set.
Expand Down Expand Up @@ -75,7 +77,7 @@ func (l *Logger) FatalOnError(msg string, err error) {

// Prettyify accepts a map of data and pretty prints it.
// It's using json.MarshalIndent and printing with log.Logger.Infoln
func (l *Logger) Prettyify(data map[string]interface{}) {
func (l *Logger) Prettyify(data interface{}) {
bytes, err := json.MarshalIndent(data, "", " ")
if err != nil {
l.error.Fatalln(err)
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 8c7c64f

Please sign in to comment.