Skip to content

Commit

Permalink
add support to hide output #95
Browse files Browse the repository at this point in the history
  • Loading branch information
srinandan committed Oct 10, 2022
1 parent 447f9ab commit e9f8ee2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
25 changes: 14 additions & 11 deletions apiclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
48 changes: 27 additions & 21 deletions apiclient/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/"

Expand All @@ -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)
Expand Down Expand Up @@ -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()

Expand All @@ -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() == "" {
Expand All @@ -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")
Expand All @@ -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")
Expand Down
8 changes: 6 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -87,7 +87,7 @@ func Execute() {
}

var accessToken, serviceAccount string
var disableCheck bool
var disableCheck, noOutput bool

func init() {
cobra.OnInitialize(initConfig)
Expand All @@ -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)
Expand Down Expand Up @@ -144,6 +147,7 @@ func initConfig() {
PrintOutput: true,
SkipLogInfo: skipLogInfo,
SkipCache: skipCache,
NoOutput: noOutput,
})
}

Expand Down

0 comments on commit e9f8ee2

Please sign in to comment.