From c0953419d25c6b646737f0432a8d9cbc60f3e1dd Mon Sep 17 00:00:00 2001 From: Darko Luketic Date: Tue, 12 Nov 2019 14:55:08 +0100 Subject: [PATCH] go mod & remove add command --- go.mod | 5 +++++ go.sum | 12 ++++++++++++ main.go | 32 +++++++++++++------------------- 3 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..62e8b00 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/dalu/spamip + +go 1.13 + +require github.com/urfave/cli v1.22.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d75daa5 --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY= +github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 6610f6c..4136543 100644 --- a/main.go +++ b/main.go @@ -15,12 +15,12 @@ const ( zoneFilePath = "/var/bind/pri/localhost.zone" ) -var re = regexp.MustCompile(`^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$`) +var re = regexp.MustCompile(`^([0-9]{1,3}|\*)\.([0-9]{1,3}|\*)\.([0-9]{1,3}|\*)\.([0-9]{1,3}|\*)$`) func main() { app := cli.NewApp() app.Name = "spamip" - app.Version = "0.1" + app.Version = "0.2" app.Compiled = time.Now() app.Authors = []cli.Author{ { @@ -42,24 +42,18 @@ func main() { }, } - app.Commands = []cli.Command{ - { - Name: "add", - Aliases: []string{"a"}, - Usage: "add an IP address to the zone file", - Action: func(c *cli.Context) error { - if len(c.Args()) != 1 { - return errors.New("add takes exactly 1 argument, an IPv4 address") - } - if !re.MatchString(c.Args().First()) { - return errors.New(c.Args().First() + "is not an IPv4 address") - } - s := re.FindStringSubmatch(c.Args().First()) - text := fmt.Sprintf("%s.%s.%s.%s\tIN\tA\t127.0.0.3\n", s[4], s[3], s[2], s[1]) - return appendText(text, c.GlobalString("file")) - }, - }, + app.Action = func(c *cli.Context) error { + if len(c.Args()) != 1 { + return errors.New("add takes exactly 1 argument, an IPv4 address") + } + if !re.MatchString(c.Args().First()) { + return errors.New(c.Args().First() + "is not an IPv4 address") + } + s := re.FindStringSubmatch(c.Args().First()) + text := fmt.Sprintf("%s.%s.%s.%s\tIN\tA\t127.0.0.3\n", s[4], s[3], s[2], s[1]) + return appendText(text, c.GlobalString("file")) } + err := app.Run(os.Args) if err != nil { log.Fatal(err)