-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from CircleCI-Public/use-unstable
Stub out Orb list command
- Loading branch information
Showing
5 changed files
with
119 additions
and
8 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
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,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 | ||
|
||
} |
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
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