-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI refactoring+tests, Makefile, Usage update
- Loading branch information
1 parent
115e4e0
commit 9649f98
Showing
7 changed files
with
158 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters