diff --git a/apiclient/httpclient.go b/apiclient/httpclient.go index f178078a7..84d8a0ffa 100644 --- a/apiclient/httpclient.go +++ b/apiclient/httpclient.go @@ -29,7 +29,7 @@ import ( "github.com/apigee/apigeecli/clilog" ) -//PostHttpZip method is used to send resources, proxy bundles, shared flows etc. +// PostHttpZip method is used to send resources, proxy bundles, shared flows etc. func PostHttpZip(print bool, auth bool, method string, url string, headers map[string]string, zipfile string) (err error) { var req *http.Request @@ -80,7 +80,7 @@ func PostHttpZip(print bool, auth bool, method string, url string, headers map[s return nil } -//PostHttpOctet method is used to send resources, proxy bundles, shared flows etc. +// PostHttpOctet method is used to send resources, proxy bundles, shared flows etc. func PostHttpOctet(print bool, update bool, url string, proxyName string) (respBody []byte, err error) { file, err := os.Open(proxyName) if err != nil { @@ -188,7 +188,7 @@ func DownloadFile(url string, auth bool) (resp *http.Response, err error) { return resp, err } -//DownloadResource method is used to download resources, proxy bundles, sharedflows +// DownloadResource method is used to download resources, proxy bundles, sharedflows func DownloadResource(url string, name string, resType string) error { var filename string @@ -228,7 +228,7 @@ func DownloadResource(url string, name string, resType string) error { return nil } -//HttpClient method is used to GET,POST,PUT or DELETE JSON data +// HttpClient method is used to GET,POST,PUT or DELETE JSON data func HttpClient(print bool, params ...string) (respBody []byte, err error) { // The first parameter instructs whether the output should be printed // The second parameter is url. If only one parameter is sent, assume GET @@ -291,15 +291,18 @@ func HttpClient(print bool, params ...string) (respBody []byte, err error) { return handleResponse(print, resp) } -//PrettyPrint method prints formatted json +// PrettyPrint method prints formatted json func PrettyPrint(body []byte) error { - var prettyJSON bytes.Buffer - err := json.Indent(&prettyJSON, body, "", "\t") - if err != nil { - clilog.Error.Println("error parsing response: ", err) - return err + if !options.NoOutput { + var prettyJSON bytes.Buffer + err := json.Indent(&prettyJSON, body, "", "\t") + if err != nil { + clilog.Error.Println("error parsing response: ", err) + return err + } + + fmt.Println(prettyJSON.String()) } - fmt.Println(prettyJSON.String()) return nil } diff --git a/apiclient/options.go b/apiclient/options.go index 045b5e776..f77eed47b 100644 --- a/apiclient/options.go +++ b/apiclient/options.go @@ -21,7 +21,7 @@ import ( "github.com/apigee/apigeecli/clilog" ) -//BaseURL is the Apigee control plane endpoint +// BaseURL is the Apigee control plane endpoint var BaseURL = "https://apigee.googleapis.com/v1/organizations/" var StageBaseURL = "https://staging-apigee.sandbox.googleapis.com/v1/organizations/" @@ -36,12 +36,13 @@ type ApigeeClientOptions struct { SkipCheck bool //skip checking access token expiry SkipCache bool //skip writing access token to file PrintOutput bool //prints output from http calls + NoOutput bool //disables printing API responses ProxyUrl string //use a proxy url } var options *ApigeeClientOptions -//NewApigeeClient sets up options to invoke Apigee APIs +// NewApigeeClient sets up options to invoke Apigee APIs func NewApigeeClient(o ApigeeClientOptions) { if options == nil { options = new(ApigeeClientOptions) @@ -84,6 +85,11 @@ func NewApigeeClient(o ApigeeClientOptions) { } else { options.PrintOutput = false } + if o.NoOutput { + options.NoOutput = true + } else { + options.NoOutput = false + } //read preference file _ = ReadPreferencesFile() @@ -94,7 +100,7 @@ func UseStaging() { BaseURL = StageBaseURL } -//SetApigeeOrg sets the org variable +// SetApigeeOrg sets the org variable func SetApigeeOrg(org string) (err error) { if org == "" { if GetApigeeOrg() == "" { @@ -106,47 +112,47 @@ func SetApigeeOrg(org string) (err error) { return nil } -//GetApigeeOrg gets the org variable +// GetApigeeOrg gets the org variable func GetApigeeOrg() string { return options.Org } -//SetApigeeEnv set the env variable +// SetApigeeEnv set the env variable func SetApigeeEnv(env string) { options.Env = env } -//GetApigeeEnv gets the env variable +// GetApigeeEnv gets the env variable func GetApigeeEnv() string { return options.Env } -//SetApigeeToken sets the access token for use with Apigee API calls +// SetApigeeToken sets the access token for use with Apigee API calls func SetApigeeToken(token string) { options.Token = token } -//GetApigeeToken get the access token value in client opts (does not generate it) +// GetApigeeToken get the access token value in client opts (does not generate it) func GetApigeeToken() string { return options.Token } -//SetProjectID sets the project id +// SetProjectID sets the project id func SetProjectID(projectID string) { options.ProjectID = projectID } -//GetProjectID gets the project id +// GetProjectID gets the project id func GetProjectID() string { return options.ProjectID } -//SetServiceAccount +// SetServiceAccount func SetServiceAccount(serviceAccount string) { options.ServiceAccount = serviceAccount } -//GetServiceAccount +// GetServiceAccount func GetServiceAccount() string { if options.ServiceAccount == "" { envVar := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") @@ -157,48 +163,48 @@ func GetServiceAccount() string { return options.ServiceAccount } -//IsSkipCheck +// IsSkipCheck func IsSkipCheck() bool { return options.SkipCheck } -//IsSkipCache +// IsSkipCache func IsSkipCache() bool { return options.SkipCache } -//IsSkipLogInfo +// IsSkipLogInfo func IsSkipLogInfo() bool { return options.SkipLogInfo } -//SetSkipLogIngo +// SetSkipLogIngo func SetSkipLogInfo(l bool) { options.SkipLogInfo = l clilog.Init(l) } -//PrintOutput +// PrintOutput func SetPrintOutput(output bool) { options.PrintOutput = output } -//GetPrintOutput +// GetPrintOutput func GetPrintOutput() bool { return options.PrintOutput } -//GetProxyURL +// GetProxyURL func GetProxyURL() string { return options.ProxyUrl } -//SetProxyURL +// SetProxyURL func SetProxyURL(proxyurl string) { options.ProxyUrl = proxyurl } -//DryRun +// DryRun func DryRun() bool { if os.Getenv("APIGEECLI_DRYRUN") != "" { clilog.Warning.Println("Dry run mode enabled! unset APIGEECLI_DRYRUN to disable dry run") diff --git a/cmd/root.go b/cmd/root.go index c58934bb0..85e688bd9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -53,7 +53,7 @@ import ( "github.com/spf13/cobra" ) -//RootCmd to manage apigeecli +// RootCmd to manage apigeecli var RootCmd = &cobra.Command{ Use: "apigeecli", Short: "Utility to work with Apigee APIs.", @@ -87,7 +87,7 @@ func Execute() { } var accessToken, serviceAccount string -var disableCheck bool +var disableCheck, noOutput bool func init() { cobra.OnInitialize(initConfig) @@ -101,6 +101,9 @@ func init() { RootCmd.PersistentFlags().BoolVarP(&disableCheck, "disable-check", "", false, "Disable check for newer versions") + RootCmd.PersistentFlags().BoolVarP(&noOutput, "no-output", "", + false, "Disable printing API responses from the control plane") + RootCmd.AddCommand(apis.Cmd) RootCmd.AddCommand(org.Cmd) RootCmd.AddCommand(sync.Cmd) @@ -144,6 +147,7 @@ func initConfig() { PrintOutput: true, SkipLogInfo: skipLogInfo, SkipCache: skipCache, + NoOutput: noOutput, }) }