Skip to content

Commit

Permalink
add init command and verbose option
Browse files Browse the repository at this point in the history
  • Loading branch information
fr3h4g committed Nov 2, 2024
1 parent 6eec3c8 commit 6d8dbfc
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 13 deletions.
62 changes: 62 additions & 0 deletions cmd/mjau/init_config.go
Original file line number Diff line number Diff line change
@@ -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"
`
2 changes: 2 additions & 0 deletions cmd/mjau/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
61 changes: 49 additions & 12 deletions cmd/mjau/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -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" {
Expand All @@ -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",
Expand All @@ -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()
Expand Down Expand Up @@ -404,15 +411,17 @@ 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") {
json = true
}
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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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))
}
}
Expand All @@ -501,19 +515,21 @@ 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("")
}
}

func init() {
rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(runAllCmd)
rootCmd.AddCommand(runInitCmd)
}

var runCmd = &cobra.Command{
Use: "run <request(s)>",
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) {
Expand Down Expand Up @@ -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")
}

},
}
3 changes: 2 additions & 1 deletion mjau.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -55,4 +57,3 @@ requests:
variable: response.body
comparison: contains
value: "origin"

0 comments on commit 6d8dbfc

Please sign in to comment.