Skip to content

Commit

Permalink
CLI refactoring+tests, Makefile, Usage update
Browse files Browse the repository at this point in the history
  • Loading branch information
antelman107 committed Jul 12, 2020
1 parent 115e4e0 commit 9649f98
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 50 deletions.
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
build: build_windows build_linux build_mac

build_windows:
GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o ./net-wait-go-windows.exe . && upx ./net-wait-go-windows.exe

build_linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o ./net-wait-go-linux . && upx ./net-wait-go-linux

build_mac:
GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o ./net-wait-go-mac . && upx ./net-wait-go-mac
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ $ net-wait-go
-debug
debug messages toggler
-delay uint
delay in milliseconds (default 100)
break between requests in milliseconds (default 50)
-packet string
UDP packet to be sent
-proto string
tcp (default "tcp")
-wait uint
delay of single request in milliseconds (default 100)
```

## 1 service check
Expand Down
60 changes: 60 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"bytes"
"encoding/base64"
"errors"
"flag"
"strings"
)

type flags struct {
proto string
addrs string
addrsSlice []string
deadlineMS uint
delayMS uint
breakMS uint
debug bool
packetBase64 string
packetBytes []byte
}

var (
ErrFlagsNotSet = errors.New("addrs are not set")
)

func getFlags(binaryName string, args []string) (*flags, string, error) {
flagSet := flag.NewFlagSet(binaryName, flag.ContinueOnError)
var buf bytes.Buffer
flagSet.SetOutput(&buf)

var conf = &flags{}
flagSet.StringVar(&conf.proto, "proto", "tcp", "tcp")
flagSet.StringVar(&conf.addrs, "addrs", "", "address:port(,address:port,address:port,...)")
flagSet.UintVar(&conf.deadlineMS, "deadline", 10000, "deadline in milliseconds")
flagSet.UintVar(&conf.delayMS, "wait", 100, "delay of single request in milliseconds")
flagSet.UintVar(&conf.breakMS, "delay", 50, "break between requests in milliseconds")
flagSet.BoolVar(&conf.debug, "debug", false, "debug messages toggler")
flagSet.StringVar(&conf.packetBase64, "packet", "", "UDP packet to be sent")

err := flagSet.Parse(args)
if err != nil {
return nil, buf.String(), err
}

conf.addrsSlice = strings.FieldsFunc(conf.addrs, func(c rune) bool {
return c == ','
})

if len(conf.addrsSlice) == 0 {
return nil, "", ErrFlagsNotSet
}

conf.packetBytes, err = base64.StdEncoding.DecodeString(conf.packetBase64)
if err != nil {
return nil, "", err
}

return conf, buf.String(), nil
}
57 changes: 57 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"encoding/base64"
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_getFlags(t *testing.T) {
var tests = []struct {
name string
args []string
flags *flags
err error
}{
{"success",
[]string{
"-proto", "udp", "-addrs", "46.174.53.245:27015,185.158.113.136:27015",
"-packet", "/////1RTb3VyY2UgRW5naW5lIFF1ZXJ5AA==", "-debug", "true"},
&flags{proto: "udp", addrs: "46.174.53.245:27015,185.158.113.136:27015",
addrsSlice: []string{"46.174.53.245:27015", "185.158.113.136:27015"},
packetBase64: "/////1RTb3VyY2UgRW5naW5lIFF1ZXJ5AA==", packetBytes: []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x54, 0x53, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x20, 0x45, 0x6E, 0x67, 0x69, 0x6E, 0x65, 0x20, 0x51, 0x75, 0x65, 0x72, 0x79, 0x00},
debug: true,
deadlineMS: 10000,
delayMS: 100,
breakMS: 50,
},
nil,
},

{"empty addrs",
[]string{
"-proto", "udp",
"-packet", "/////1RTb3VyY2UgRW5naW5lIFF1ZXJ5AA==", "-debug", "true"},
nil,
errors.New("addrs are not set"),
},

{"invalid base64 packet",
[]string{
"-proto", "udp", "-addrs", "46.174.53.245:27015,185.158.113.136:27015",
"-packet", "/////1RTb3VyY2UgRW5naW5lIFF1ZXJ5AA==111", "-debug", "true"},
nil,
base64.CorruptInputError(36),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualFlags, _, err := getFlags("prog", tt.args)
assert.Equal(t, tt.flags, actualFlags, "flags are not equal")
assert.Equal(t, tt.err, err, "errors are not equal")
})
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/antelman107/net-wait-go

go 1.14

require github.com/stretchr/testify v1.6.1
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
62 changes: 13 additions & 49 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,32 @@
package main

import (
"encoding/base64"
"flag"
"fmt"
"log"
"os"
"strings"
"time"

"github.com/antelman107/net-wait-go/wait"
)

func main() {
var proto string
flag.StringVar(&proto, "proto", "tcp", "tcp")

var addrs string
flag.StringVar(&addrs, "addrs", "", "address:port")

var deadlineMS uint
flag.UintVar(&deadlineMS, "deadline", 10000, "deadline in milliseconds")

var delayMS uint
flag.UintVar(&delayMS, "wait", 100, "delay of single request in milliseconds")

var breakMS uint
flag.UintVar(&breakMS, "delay", 50, "break between requests in milliseconds")

var debug bool
flag.BoolVar(&debug, "debug", false, "debug messages toggler")

var packetBase64 string
flag.StringVar(&packetBase64, "packet", "", "UDP packet to be sent")

flag.Parse()

addrsSlice := strings.FieldsFunc(addrs, func(c rune) bool {
return c == ','
})

if len(addrsSlice) == 0 {
log.Println("addrs are not set")
flag.Usage()

os.Exit(2)
}

packetBytes, err := base64.StdEncoding.DecodeString(packetBase64)
if err != nil {
fmt.Println("packet base64 decode error:", err)
flag.Usage()

conf, output, err := getFlags(os.Args[0], os.Args[1:])
if err == flag.ErrHelp {
fmt.Println(output)
os.Exit(2)
} else if err != nil {
fmt.Println(err, "\n", output)
os.Exit(1)
}

if wait.New(
wait.WithProto(proto),
wait.WithWait(time.Duration(delayMS)*time.Millisecond),
wait.WithBreak(time.Duration(breakMS)*time.Millisecond),
wait.WithDeadline(time.Duration(deadlineMS)*time.Millisecond),
wait.WithDebug(debug),
wait.WithUDPPacket(packetBytes),
).Do(addrsSlice) {
wait.WithProto(conf.proto),
wait.WithWait(time.Duration(conf.delayMS)*time.Millisecond),
wait.WithBreak(time.Duration(conf.breakMS)*time.Millisecond),
wait.WithDeadline(time.Duration(conf.deadlineMS)*time.Millisecond),
wait.WithDebug(conf.debug),
wait.WithUDPPacket(conf.packetBytes),
).Do(conf.addrsSlice) {
return
}

Expand Down

0 comments on commit 9649f98

Please sign in to comment.