package api import ( "bytes" "encoding/json" "errors" "fmt" "io" "io/ioutil" "net/http" "os" "regexp" "sort" "strconv" "strings" "syscall" "time" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/api" "github.com/cli/cli/internal/config" "github.com/cli/cli/internal/ghinstance" "github.com/cli/cli/internal/ghrepo" "github.com/cli/cli/pkg/cmdutil" "github.com/cli/cli/pkg/iostreams" "github.com/cli/cli/pkg/jsoncolor" "github.com/spf13/cobra" ) type ApiOptions struct { IO *iostreams.IOStreams Hostname string RequestMethod string RequestMethodPassed bool RequestPath string RequestInputFile string MagicFields []string RawFields []string RequestHeaders []string Previews []string ShowResponseHeaders bool Paginate bool Silent bool Template string CacheTTL time.Duration FilterOutput string Config func() (config.Config, error) HttpClient func() (*http.Client, error) BaseRepo func() (ghrepo.Interface, error) Branch func() (string, error) } func NewCmdApi(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command { opts := ApiOptions{ IO: f.IOStreams, Config: f.Config, HttpClient: f.HttpClient, BaseRepo: f.BaseRepo, Branch: f.Branch, } cmd := &cobra.Command{ Use: "api ", Short: "Make an authenticated GitHub API request", Long: heredoc.Docf(` Makes an authenticated HTTP request to the GitHub API and prints the response. The endpoint argument should either be a path of a GitHub API v3 endpoint, or "graphql" to access the GitHub API v4. Placeholder values ":owner", ":repo", and ":branch" in the endpoint argument will get replaced with values from the repository of the current directory. The default HTTP request method is "GET" normally and "POST" if any parameters were added. Override the method with %[1]s--method%[1]s. Pass one or more %[1]s--raw-field%[1]s values in "key=value" format to add JSON-encoded string parameters to the POST body. The %[1]s--field%[1]s flag behaves like %[1]s--raw-field%[1]s with magic type conversion based on the format of the value: - literal values "true", "false", "null", and integer numbers get converted to appropriate JSON types; - placeholder values ":owner", ":repo", and ":branch" get populated with values from the repository of the current directory; - if the value starts with "@", the rest of the value is interpreted as a filename to read the value from. Pass "-" to read from standard input. For GraphQL requests, all fields other than "query" and "operationName" are interpreted as GraphQL variables. Raw request body may be passed from the outside via a file specified by %[1]s--input%[1]s. Pass "-" to read from standard input. In this mode, parameters specified via %[1]s--field%[1]s flags are serialized into URL query parameters. In %[1]s--paginate%[1]s mode, all pages of results will sequentially be requested until there are no more pages of results. For GraphQL requests, this requires that the original query accepts an %[1]s$endCursor: String%[1]s variable and that it fetches the %[1]spageInfo{ hasNextPage, endCursor }%[1]s set of fields from a collection. The %[1]s--jq%[1]s option accepts a query in jq syntax and will print only the resulting values that match the query. This is equivalent to piping the output to %[1]sjq -r%[1]s, but does not require the jq utility to be installed on the system. To learn more about the query syntax, see: https://stedolan.github.io/jq/manual/v1.6/ With %[1]s--template%[1]s, the provided Go template is rendered using the JSON data as input. For the syntax of Go templates, see: https://golang.org/pkg/text/template/ The following functions are available in templates: - %[1]scolor