Merge pull request #37 from davidnewhall/dn2_pkg

Convert main to a package.
This commit is contained in:
David Newhall II 2019-06-13 01:12:43 -07:00 committed by GitHub
commit 076b22af4e
7 changed files with 40 additions and 29 deletions

View File

@ -4,15 +4,17 @@
BINARY:=unifi-poller
URL:=https://github.com/davidnewhall/$(BINARY)
MAINT=David Newhall II <david at sleepers dot pro>
DESC=This daemon polls a Unifi controller at a short interval and stores the collected metric data in an Influx Database.
DESC=This daemon polls a Unifi controller at a short interval and stores the collected measurements in an Influx Database.
OSX_PKG_PREFIX=com.github.davidnewhall
GOLANGCI_LINT_ARGS=--enable-all -D gochecknoglobals
PACKAGE:=./cmd/$(BINARY)
LIBRARY:=./pkg/$(BINARY)
ITERATION:=$(shell git rev-list --count HEAD||echo 0)
ifeq ($(VERSION),)
VERSION:=$(shell git tag -l --merged | tail -n1 | tr -d v||echo development)
endif
ITERATION:=$(shell git rev-list --count HEAD||echo 0)
OSX_PKG_PREFIX=com.github.davidnewhall
GOLANGCI_LINT_ARGS=--enable-all -D gochecknoglobals
# rpm is wierd and changes - to _ in versions.
RPMVERSION:=$(shell echo $(VERSION) | tr -- - _)
all: man build
@ -60,17 +62,17 @@ README.html: md2roff
build: $(BINARY)
$(BINARY):
go build -o $(BINARY) -ldflags "-w -s -X main.Version=$(VERSION)" $(PACKAGE)
go build -o $(BINARY) -ldflags "-w -s -X github.com/davidnewhall/unifi-poller/pkg/unifi-poller.Version=$(VERSION)" $(PACKAGE)
linux: $(BINARY).linux
$(BINARY).linux:
# Building linux binary.
GOOS=linux go build -o $(BINARY).linux -ldflags "-w -s -X main.Version=$(VERSION)" $(PACKAGE)
GOOS=linux go build -o $(BINARY).linux -ldflags "-w -s -X github.com/davidnewhall/unifi-poller/pkg/unifi-poller.Version=$(VERSION)" $(PACKAGE)
macos: $(BINARY).macos
$(BINARY).macos:
# Building darwin binary.
GOOS=darwin go build -o $(BINARY).macos -ldflags "-w -s -X main.Version=$(VERSION)" $(PACKAGE)
GOOS=darwin go build -o $(BINARY).macos -ldflags "-w -s -X github.com/davidnewhall/unifi-poller/pkg/unifi-poller.Version=$(VERSION)" $(PACKAGE)
# Packages
@ -166,7 +168,7 @@ $(BINARY).rb: v$(VERSION).tar.gz.sha256
# Run code tests and lint.
test: lint
# Testing.
go test -race -covermode=atomic $(PACKAGE)
go test -race -covermode=atomic $(PACKAGE) $(LIBRARY)
lint:
# Checking lint.
golangci-lint run $(GOLANGCI_LINT_ARGS)

View File

@ -0,0 +1,24 @@
package main
import (
"fmt"
"log"
"os"
unifipoller "github.com/davidnewhall/unifi-poller/pkg/unifi-poller"
)
func main() {
unifi := &unifipoller.UnifiPoller{}
if unifi.ParseFlags(os.Args[1:]); unifi.ShowVer {
fmt.Printf("unifi-poller v%s\n", unifipoller.Version)
return // don't run anything else w/ version request.
}
if err := unifi.GetConfig(); err != nil {
unifi.Flag.Usage()
log.Fatalf("[ERROR] config file '%v': %v", unifi.ConfigFile, err)
}
if err := unifi.Run(); err != nil {
log.Fatalln("[ERROR]", err)
}
}

View File

@ -1,4 +1,4 @@
package main
package unifipoller
import (
"time"

View File

@ -1,4 +1,4 @@
package main
package unifipoller
import (
"log"

View File

@ -1,4 +1,4 @@
package main
package unifipoller
import (
"fmt"

View File

@ -1,4 +1,4 @@
package main
package unifipoller
import (
"log"

View File

@ -1,4 +1,4 @@
package main
package unifipoller
import (
"fmt"
@ -13,21 +13,6 @@ import (
flag "github.com/spf13/pflag"
)
func main() {
u := &UnifiPoller{}
if u.ParseFlags(os.Args[1:]); u.ShowVer {
fmt.Printf("unifi-poller v%s\n", Version)
return // don't run anything else.
}
if err := u.GetConfig(); err != nil {
u.Flag.Usage()
log.Fatalf("[ERROR] config file '%v': %v", u.ConfigFile, err)
}
if err := u.Run(); err != nil {
log.Fatalln("[ERROR]", err)
}
}
// ParseFlags runs the parser.
func (u *UnifiPoller) ParseFlags(args []string) {
u.Flag = flag.NewFlagSet("unifi-poller", flag.ExitOnError)