Skip to content

Commit

Permalink
Fixed -e duplicating lines (#11)
Browse files Browse the repository at this point in the history
Added version
Updated build.sh

Co-authored-by: Dustin Ratcliffe <Dustin.K.Ratcliffe@centene.com>
  • Loading branch information
dustyrat and Dustin Ratcliffe committed Jun 8, 2020
1 parent 61ae4d7 commit c682308
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 22 deletions.
21 changes: 18 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
for OS in "linux" "windows" "darwin" "freebsd"; do
#!/bin/bash
ext() {
local array=$1 index=$2
local i="${array}_$index"
printf '%s' "${!i}"
}
declare "EXT_windows=.exe"

VERSION=$(git rev-parse HEAD)
TAG=$(git tag --points-at "$VERSION")
if [ -n "$TAG" ]; then
VERSION=$TAG
fi
echo VERSION: $VERSION
for OS in "darwin" "linux" "windows"; do
for ARCH in "386" "amd64"; do
CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build -o ${OS}_${ARCH}/post-it ./main.go
echo Building: release/${OS}_${ARCH}/post-it$(ext EXT $OS)
CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build -ldflags "-X main.version=$VERSION" -o release/${OS}_${ARCH}/post-it$(ext EXT $OS) ./main.go
done
done
done
7 changes: 4 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
func Execute(version string) {
cmd := &cobra.Command{
Use: "post-it",
Short: "post-it is a HTTP(S) CLI library for calling a variaty of urls from an input file.",
Use: "post-it",
Version: version,
Short: "post-it is a HTTP(S) CLI library for calling a variaty of urls from an input file.",
Long: `post-it is a HTTP(S) CLI library for calling a variaty of urls from an input file.
All methods use the request_body column for requests.
Expand Down
4 changes: 3 additions & 1 deletion internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ func (c *Controller) Run(file, method, rawURL string) error {
if c.Options.Flags.Body {
headers = append(headers, "response_body")
}
headers = append(headers, "error")
if c.Options.Flags.Errors {
headers = append(headers, "error")
}
c.Writer.Write(headers)
}

Expand Down
25 changes: 13 additions & 12 deletions internal/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ func (e *entry) Strings(flags options.Flags) []string {
}
}

if e.err != nil {
out = append(out, e.err.Error())
} else {
out = append(out, "")
if flags.Errors {
if e.err != nil {
out = append(out, e.err.Error())
} else {
out = append(out, "")
}
}

return out
}

Expand Down Expand Up @@ -109,13 +112,6 @@ func write(w *csv.Writer, opts options.Options, entry entry) {
request := entry.request
response := request.Response

if opts.Flags.Errors {
if entry.err != nil {
output := entry.Strings(opts.Flags)
w.Write(output)
}
}

if opts.Flags.Status == "any" {
output := entry.Strings(opts.Flags)
w.Write(output)
Expand All @@ -128,7 +124,7 @@ func write(w *csv.Writer, opts options.Options, entry entry) {
output := entry.Strings(opts.Flags)
w.Write(output)
}
} else {
} else if status < 0 {
a := -status
b := a + 100
if !internal.InRange(response.StatusCode, a, b) {
Expand All @@ -142,6 +138,11 @@ func write(w *csv.Writer, opts options.Options, entry entry) {
output := entry.Strings(opts.Flags)
w.Write(output)
}
} else if opts.Flags.Errors {
if entry.err != nil {
output := entry.Strings(opts.Flags)
w.Write(output)
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ package main

import "github.com/DustyRat/post-it/cmd"

var version string

func main() {
cmd.Execute()
cmd.Execute(version)
}
8 changes: 6 additions & 2 deletions test/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,15 @@ func get() http.HandlerFunc {
e.Str("handler", "get").Str("Content-Type", contentType).Str("Accept", accept).Str("id", id).Int64("resp_time", time.Now().Sub(start).Milliseconds()).Send()
}(e, start)

if id == "" {
if id == "5" {
respond(w, http.StatusNotFound, "text/html", http.StatusText(http.StatusNotFound))
return
}

if id == "10" {
time.Sleep(time.Duration(rand.Intn(10000)) * time.Millisecond)
return
}
now := time.Now()
respond(w, http.StatusOK, accept, response{
ID: id,
Expand Down Expand Up @@ -295,7 +300,6 @@ func respond(w http.ResponseWriter, code int, contentType string, payload interf
time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
// time.Sleep(time.Duration(rand.Intn(10000)) * time.Millisecond)
}

func unmarshal(accept string, body []byte, v interface{}) error {
Expand Down

0 comments on commit c682308

Please sign in to comment.