-
Notifications
You must be signed in to change notification settings - Fork 70
/
command_query.go
113 lines (95 loc) · 3.45 KB
/
command_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package nerdgraph
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/newrelic/newrelic-cli/internal/client"
"github.com/newrelic/newrelic-cli/internal/output"
"github.com/newrelic/newrelic-cli/internal/utils"
ng "github.com/newrelic/newrelic-client-go/v2/pkg/nerdgraph"
)
var (
queryFile string
variables string
variablesFile string
)
var cmdQuery = &cobra.Command{
Use: "query",
Short: "Execute a raw GraphQL query request to the NerdGraph API",
Long: `Execute a raw GraphQL query request to the NerdGraph API
The query command accepts a single argument in the form of a GraphQL query as a string.
This command accepts an optional flag, --variables, which should be a JSON string where the
keys are the variables to be referenced in the GraphQL query.
`,
Example: `newrelic nerdgraph query 'query($guid: EntityGuid!) { actor { entity(guid: $guid) { guid name domain entityType } } }' --variables '{"guid": "<GUID>"}'`,
Args: func(cmd *cobra.Command, args []string) error {
argsCount := len(args)
if argsCount < 1 && strings.TrimSpace(queryFile) == "" {
return errors.New("missing graph query argument")
}
if argsCount == 1 && strings.TrimSpace(queryFile) != "" {
return errors.New("command expects either argument or queryFile, not both")
}
if argsCount > 1 {
return errors.New("command expects only 1 argument")
}
return nil
},
PreRun: client.RequireClient,
Run: func(cmd *cobra.Command, args []string) {
var variablesParsed map[string]interface{}
var err error
// when variableFile is defined, read and parse it
if strings.TrimSpace(variablesFile) != "" {
var variableContent []byte
variableContent, err = ioutil.ReadFile(variablesFile)
if err != nil {
log.Fatal("unable to read variable file")
}
err = json.Unmarshal(variableContent, &variablesParsed)
if err != nil {
log.Fatal(err)
}
}
// parse variables passed as arguments, this will overwrite any existing variables from file
err = json.Unmarshal([]byte(variables), &variablesParsed)
if err != nil {
log.Fatal(err)
}
// when queryFile is defined, read and parse it, if not take argument query
query := ""
if strings.TrimSpace(queryFile) != "" {
var queryContent []byte
queryContent, err = ioutil.ReadFile(queryFile)
if err != nil {
log.Fatal("unable to read query file")
}
query = string(queryContent)
} else {
query = args[0]
}
result, err := client.NRClient.NerdGraph.QueryWithContext(utils.SignalCtx, query, variablesParsed)
if err != nil {
log.Fatal(err)
}
reqBodyBytes := new(bytes.Buffer)
encoder := json.NewEncoder(reqBodyBytes)
err = encoder.Encode(ng.QueryResponse{
Actor: result.(ng.QueryResponse).Actor,
Docs: result.(ng.QueryResponse).Docs,
RequestContext: result.(ng.QueryResponse).RequestContext,
})
utils.LogIfFatal(err)
utils.LogIfFatal(output.Print(reqBodyBytes))
},
}
func init() {
Command.AddCommand(cmdQuery)
cmdQuery.Flags().StringVar(&variables, "variables", "{}", "the variables to pass to the GraphQL query, represented as a JSON string (this will overwrite duplicate variables that are set through variablesFile")
cmdQuery.Flags().StringVar(&variablesFile, "variablesFile", "", "the variables to pass to GraphQL query, represented as a JSON file")
cmdQuery.Flags().StringVar(&queryFile, "queryFile", "", "the query to use, represented as a JSON file")
}