-
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.
- Loading branch information
1 parent
684e99b
commit 6906153
Showing
3 changed files
with
78 additions
and
5 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,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 | ||
|
||
} |
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