diff --git a/cmd/mjau/init_config.go b/cmd/mjau/init_config.go new file mode 100644 index 0000000..c86598c --- /dev/null +++ b/cmd/mjau/init_config.go @@ -0,0 +1,62 @@ +package mjau + +var InitSampleConfig = `# This is a sample mjau configuration file +# You can use this file as a template for your own configuration +environments: + - name: default + variables: + - key: host + value: httpbin.org + - key: uuid + value: "{{$uuid()}}" +requests: + - name: test + pre_commands: + - command: add_variable + description: "add variable test" + variable: test99 + value: "test99" + url: http://{{environment.host}}/get + method: GET + headers: + - key: Content-Type + value: application/json + commands: + - command: add_variable + description: "add variable test" + variable: test + value: "test:\n{{environment.host}}\n{{$timestamp()}}\n{{$random(100)}}\n{{$uuid()}}" + - command: add_json_variable + description: "add origin from response.body to test2" + variable: test2 + from_variable: response.body + path: origin + + - name: healthz + url: http://{{environment.host}}/get + method: GET + headers: + - key: Content-Type + value: application/json + commands: + - command: echo + description: "print response code" + value: "response code: {{response.status_code}}" + asserts: + - description: "status code is 200" + variable: response.status_code + comparison: "==" + value: 200 + - description: "status code is less than 400" + variable: response.status_code + comparison: "<=" + value: 400 + - description: "content type is application/json" + variable: response.headers.Content-Type + comparison: "==" + value: application/json + - description: "body contains origin" + variable: response.body + comparison: contains + value: "origin" +` diff --git a/cmd/mjau/root.go b/cmd/mjau/root.go index 64d4fe2..6af5bfb 100644 --- a/cmd/mjau/root.go +++ b/cmd/mjau/root.go @@ -38,6 +38,8 @@ func Execute() { rootCmd.PersistentFlags().BoolP("show-asserts", "A", false, "print asserts") + rootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output, prints everything") + rootCmd.PersistentFlags().StringP("env", "e", "default", "environment to use") if err := rootCmd.Execute(); err != nil { diff --git a/cmd/mjau/run.go b/cmd/mjau/run.go index 40fb9b5..62be72e 100644 --- a/cmd/mjau/run.go +++ b/cmd/mjau/run.go @@ -4,6 +4,7 @@ import ( "crypto/tls" "crypto/x509" "encoding/json" + "errors" "fmt" "io" "log" @@ -343,7 +344,9 @@ func RunRequest(cmd *cobra.Command, requestName string, config *Config) { for _, header := range request.Headers { req.Header.Set(header.Key, header.Value) if cmd.Flag("full-request").Value.String() == "true" || - cmd.Flag("request-headers").Value.String() == "true" { + cmd.Flag( + "request-headers", + ).Value.String() == "true" || cmd.Flag("verbose").Value.String() == "true" { fmt.Println(AnsiColor(header.Key, 53, 177, 226) + ": " + header.Value) } if header.Key == "User-Agent" { @@ -356,7 +359,9 @@ func RunRequest(cmd *cobra.Command, requestName string, config *Config) { "mjau/"+Version+" ("+runtime.GOOS+"; "+runtime.GOARCH+")", ) if cmd.Flag("full-request").Value.String() == "true" || - cmd.Flag("request-headers").Value.String() == "true" { + cmd.Flag( + "request-headers", + ).Value.String() == "true" || cmd.Flag("verbose").Value.String() == "true" { fmt.Println( AnsiColor( "User-Agent", @@ -372,7 +377,9 @@ func RunRequest(cmd *cobra.Command, requestName string, config *Config) { } if cmd.Flag("full-request").Value.String() == "true" || - cmd.Flag("request-body").Value.String() == "true" { + cmd.Flag( + "request-body", + ).Value.String() == "true" || cmd.Flag("verbose").Value.String() == "true" { fmt.Println("\n" + request.Body) } start := time.Now() @@ -404,7 +411,8 @@ func RunRequest(cmd *cobra.Command, requestName string, config *Config) { ) json := false for key, value := range resp.Header { - if cmd.Flag("headers").Value.String() == "true" { + if cmd.Flag("headers").Value.String() == "true" || + cmd.Flag("verbose").Value.String() == "true" { fmt.Println(AnsiColor(key, 53, 177, 226) + ": " + strings.Join(value, ", ")) } if key == "Content-Type" && strings.Contains(value[0], "application/json") { @@ -412,7 +420,8 @@ func RunRequest(cmd *cobra.Command, requestName string, config *Config) { } config.StoreVariable("response.headers."+key, strings.Join(value, ", ")) } - if cmd.Flag("headers").Value.String() == "true" { + if cmd.Flag("headers").Value.String() == "true" || + cmd.Flag("verbose").Value.String() == "true" { fmt.Println("") } if json { @@ -427,11 +436,13 @@ func RunRequest(cmd *cobra.Command, requestName string, config *Config) { RunCommands(request.Commands, config, cmd) } - if cmd.Flag("show-variables").Value.String() == "true" { + if cmd.Flag("show-variables").Value.String() == "true" || + cmd.Flag("verbose").Value.String() == "true" { config.ShowStoredVariables() } - if cmd.Flag("show-asserts").Value.String() == "true" { + if cmd.Flag("show-asserts").Value.String() == "true" || + cmd.Flag("verbose").Value.String() == "true" { if len(request.Asserts) > 0 { fmt.Println("👀 Asserts:") for _, assert := range request.Asserts { @@ -473,15 +484,18 @@ func RunRequest(cmd *cobra.Command, requestName string, config *Config) { } func RunCommands(commands []Command, config *Config, cmd *cobra.Command) { - if cmd.Flag("show-commands").Value.String() == "true" { + if cmd.Flag("show-commands").Value.String() == "true" || + cmd.Flag("verbose").Value.String() == "true" { fmt.Println("🔧 Commands:") } for _, command := range commands { - if cmd.Flag("show-commands").Value.String() == "true" { + if cmd.Flag("show-commands").Value.String() == "true" || + cmd.Flag("verbose").Value.String() == "true" { fmt.Println(" ✨ " + command.Description) } if command.Command == "echo" { - if cmd.Flag("show-commands").Value.String() == "true" { + if cmd.Flag("show-commands").Value.String() == "true" || + cmd.Flag("verbose").Value.String() == "true" { fmt.Println(" " + config.InsertVariables(command.Value)) } } @@ -501,7 +515,8 @@ func RunCommands(commands []Command, config *Config, cmd *cobra.Command) { } } } - if cmd.Flag("show-commands").Value.String() == "true" { + if cmd.Flag("show-commands").Value.String() == "true" || + cmd.Flag("verbose").Value.String() == "true" { fmt.Println("") } } @@ -509,11 +524,12 @@ func RunCommands(commands []Command, config *Config, cmd *cobra.Command) { func init() { rootCmd.AddCommand(runCmd) rootCmd.AddCommand(runAllCmd) + rootCmd.AddCommand(runInitCmd) } var runCmd = &cobra.Command{ Use: "run ", - Short: "Run one or more requests", + Short: "Run one or more requests separated by comma ,", Long: `Run one or more requests separated by comma ,`, Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { @@ -547,3 +563,24 @@ var runAllCmd = &cobra.Command{ }, } + +var runInitCmd = &cobra.Command{ + Use: "init", + Short: "Initialize Mjau, create a sample config file", + Long: `Initialize Mjau, create a sample config file`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Initializing Mjau, creating a sample config file") + configFile := cmd.Flag("config").Value.String() + + if _, err := os.Stat(configFile); errors.Is(err, os.ErrNotExist) { + os.WriteFile( + configFile, + []byte(InitSampleConfig), + 0644, + ) + } else { + fmt.Println("Config file already exists") + } + + }, +} diff --git a/mjau.yaml b/mjau.yaml index de3b552..e1ac814 100644 --- a/mjau.yaml +++ b/mjau.yaml @@ -1,3 +1,5 @@ +# This is a sample mjau configuration file +# You can use this file as a template for your own configuration environments: - name: default variables: @@ -55,4 +57,3 @@ requests: variable: response.body comparison: contains value: "origin" -