-
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.
Ensure config is created before executing any commands
This will walk the user through setting up their token unless otherwise configured, i.e. via flags Diagnostic command prints out host, and if token is found. Moved GQL call to Query command for testing.
- Loading branch information
Zachary Scott
committed
May 31, 2018
1 parent
d6df3a9
commit 2f78690
Showing
3 changed files
with
167 additions
and
48 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,64 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/machinebox/graphql" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var queryCmd = &cobra.Command{ | ||
Use: "query", | ||
Short: "Query the CircleCI GraphQL API.", | ||
Run: query, | ||
} | ||
|
||
func query(cmd *cobra.Command, args []string) { | ||
host := viper.GetString("host") | ||
token := viper.GetString("token") | ||
client := graphql.NewClient(host + "/graphql") | ||
|
||
query := ` | ||
query IntrospectionQuery { | ||
__schema { | ||
queryType { name } | ||
mutationType { name } | ||
subscriptionType { name } | ||
types { | ||
...FullType | ||
} | ||
directives { | ||
name | ||
description | ||
} | ||
} | ||
} | ||
fragment FullType on __Type { | ||
kind | ||
name | ||
description | ||
fields(includeDeprecated: true) { | ||
name | ||
} | ||
}` | ||
|
||
req := graphql.NewRequest(query) | ||
req.Header.Set("Authorization", token) | ||
|
||
ctx := context.Background() | ||
var resp map[string]interface{} | ||
|
||
fmt.Println("Querying", host, "with:\n", query, "\n") | ||
if err := client.Run(ctx, req, &resp); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
b, _ := json.MarshalIndent(resp, "", " ") | ||
fmt.Println("Result: \n") | ||
fmt.Println(string(b)) | ||
} |
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