Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Add support for writing config to output file
Browse files Browse the repository at this point in the history
  • Loading branch information
blakewatters committed Apr 18, 2020
1 parent 5a2f710 commit c563040
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
44 changes: 36 additions & 8 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package cmd

import (
"fmt"
"os"

"github.com/hokaccha/go-prettyjson"
"github.com/opsani/cli/opsani"
Expand All @@ -40,6 +41,12 @@ func NewAPIClientFromConfig() *opsani.Client {
if tracingEnabled {
c.EnableTrace()
}

// Set the output directory to pwd by default
dir, err := os.Getwd()
if err == nil {
c.SetOutputDirectory(dir)
}
return c
}

Expand All @@ -61,17 +68,34 @@ var appConfigSetCmd = &cobra.Command{
},
}

var appConfig = struct {
OutputFile string
}{}

var appConfigCmd = &cobra.Command{
Use: "config",
Short: "Manage app configuration",
Run: func(cmd *cobra.Command, args []string) {
client := NewAPIClientFromConfig()
config, err := client.GetConfig()
if err != nil {
panic(err)
if appConfig.OutputFile == "" {
config, err := client.GetConfig()
if err != nil {
panic(err)
}
if appConfig.OutputFile != "" {

} else {
s, _ := prettyjson.Marshal(config)
fmt.Println(string(s))
}
} else {
err := client.GetConfigToOutput(appConfig.OutputFile)
if err == nil {
fmt.Printf("Output written to \"%s\"", appConfig.OutputFile)
} else {
panic(err)
}
}
s, _ := prettyjson.Marshal(config)
fmt.Println(string(s))
},
}

Expand All @@ -84,10 +108,14 @@ var appCmd = &cobra.Command{
}

func init() {
configCmd.AddCommand(appConfigEditCmd)
configCmd.AddCommand(appConfigSetCmd)
appCmd.AddCommand(appConfigCmd)
rootCmd.AddCommand(appCmd)
appCmd.AddCommand(appConfigCmd)
appConfigCmd.AddCommand(appConfigEditCmd)
appConfigCmd.AddCommand(appConfigSetCmd)

// app config flags
appConfigCmd.Flags().StringVarP(&appConfig.OutputFile, "output", "o", "", "Write output to file instead of stdout")

// app config set flags
appConfigSetCmd.Flags().StringP("file", "f", "", "File containing configuration to apply")
}
21 changes: 21 additions & 0 deletions opsani/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ func (c *Client) GetConfig() (interface{}, error) {
return resp.Result(), err
}

// GetConfigToOutput retrieves the Opsani app configuration from the API and writes it to a file
func (c *Client) GetConfigToOutput(filename string) error {
_, err := c.restyClient.R().
SetOutput(filename).
Get(c.configURLPath())
return err
}

/**
Authentication actions
*/
Expand Down Expand Up @@ -152,3 +160,16 @@ func (c *Client) EnableTrace() *Client {
})
return c
}

// SetOutputDirectory sets the output directory for saving API responses
func (c *Client) SetOutputDirectory(dir string) {
c.restyClient.SetOutputDirectory(dir)
}

// WriteOutputToFile configures a request handler to write responses to the specified file
func (c *Client) WriteOutputToFile(filename string) {
c.restyClient.OnBeforeRequest(func(c *resty.Client, r *resty.Request) error {
r.SetOutput(filename)
return nil // if its success otherwise return error
})
}

0 comments on commit c563040

Please sign in to comment.