From 8235e29ceda9bfb9466ab33d2e6e6ac95c3e462c Mon Sep 17 00:00:00 2001 From: Ricky Pike Date: Fri, 24 Mar 2023 08:04:44 -0500 Subject: [PATCH] upgrade tooling, convert to GH actions (#13) * upgrade tooling, convert to GH actions * source format changes * satisfy linter --- .github/dependabot.yml | 13 +++++++ .github/workflows/builder.yml | 42 ++++++++++++++++++++++ .github/workflows/release.yml | 29 +++++++++++++++ .gitignore | 1 + .golangci.yml | 7 ++++ .goreleaser.yml | 55 +++++++++++++++++++++++++++++ .travis.yml | 22 ------------ cmd/main.go | 2 +- cmd/main_test.go | 12 +++---- cmd/message.go | 10 +++--- cmd/validate.go | 4 +-- go.mod | 9 +++-- go.sum | 66 +++++------------------------------ message.go | 26 +++++++------- message_test.go | 52 +++++++++++++-------------- scripts/pushover-test.sh | 4 +++ validate.go | 18 +++++----- validate_test.go | 26 +++++++------- 18 files changed, 242 insertions(+), 156 deletions(-) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/builder.yml create mode 100644 .github/workflows/release.yml create mode 100644 .golangci.yml create mode 100644 .goreleaser.yml delete mode 100644 .travis.yml create mode 100755 scripts/pushover-test.sh diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..51839b5 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,13 @@ +version: 2 +updates: +- package-ecosystem: gomod + directory: / + schedule: + interval: weekly + open-pull-requests-limit: 5 + target-branch: dev +- package-ecosystem: github-actions + directory: / + schedule: + interval: weekly + open-pull-requests-limit: 5 diff --git a/.github/workflows/builder.yml b/.github/workflows/builder.yml new file mode 100644 index 0000000..4f6879c --- /dev/null +++ b/.github/workflows/builder.yml @@ -0,0 +1,42 @@ +on: [push, pull_request] +name: Build +jobs: + test: + name: Build and Test + strategy: + matrix: + go-version: [1.20.x] + os: [ubuntu-latest] + runs-on: ${{ matrix.os }} + steps: + - name: Install Go + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go-version }} + + - name: Checkout code + uses: actions/checkout@v3 + + - name: Code format + run: diff -u <(echo -n) <(gofmt -d -s .) + + - name: Vet + run: go vet ./... + + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + + - name: Unit tests + run: go test -race -coverprofile=coverage.out ./... + + - name: Function coverage + run: go tool cover "-func=coverage.out" + + - name: Upload coverage report + uses: codecov/codecov-action@v3 + with: + file: ./coverage.out + + - name: Build and Test + run: | + ./scripts/pushover-test.sh diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..2c84b7a --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,29 @@ +name: Release + +on: + push: + tags: + - '*' + +jobs: + goreleaser: + name: GoReleaser + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - + name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: 1.20.x + - + name: Run GoReleaser + uses: goreleaser/goreleaser-action@v4 + with: + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index c48a22a..3c46c5b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ bin coverage.* +dist/ diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..2e37b11 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,7 @@ +linters: + enable: + - gosec +linters-settings: + gosec: + excludes: + - G404 diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..b24ebd4 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,55 @@ +release: + draft: true +before: + hooks: + - go mod tidy +builds: + - main: ./cmd + binary: pushover + ldflags: + - -s -w -X main.versionText={{.Version}} + env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + goarch: + - amd64 + - arm + - arm64 + ignore: + - goos: linux + goarch: 386 + - goos: windows + goarch: 386 + - goos: windows + goarch: arm + - goos: windows + goarch: arm64 + - goos: darwin + goarch: 386 +archives: + - name_template: "pushover_{{ .Version }}_{{ .Os }}_{{ .Arch }}" + format_overrides: + - goos: windows + format: zip + replacements: + darwin: darwin + linux: linux + windows: windows + 386: 386 + amd64: amd64 + files: + - README.md +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ .Tag }}-next" +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' + diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 451884e..0000000 --- a/.travis.yml +++ /dev/null @@ -1,22 +0,0 @@ -language: go - -go: - - 1.13.x - -# Only clone the most recent commit. -git: - depth: 1 - -script: - - make test && bash <(curl -s https://codecov.io/bash) - - make - -deploy: - provider: releases - api_key: $GITHUB_TOKEN - file_glob: true - file: bin/* - skip_cleanup: true - draft: true - on: - tags: true \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go index 01eb01d..6d0c1fc 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -47,5 +47,5 @@ https://pushover.net/.`, addMessageCmd(rootCmd) addValidateCmd(rootCmd) - rootCmd.Execute() + _ = rootCmd.Execute() } diff --git a/cmd/main_test.go b/cmd/main_test.go index fa9d848..0811f09 100644 --- a/cmd/main_test.go +++ b/cmd/main_test.go @@ -11,12 +11,12 @@ import ( const id = "deadbeef-dead-beef-dead-deadbeefdead" func serverMessageHandler(w http.ResponseWriter, r *http.Request) { - r.ParseForm() - r.ParseMultipartForm(0) + _ = r.ParseForm() + _ = r.ParseMultipartForm(0) // Check html and monospace - html, _ := r.Form["html"] - monospace, _ := r.Form["monospace"] + html := r.Form["html"] + monospace := r.Form["monospace"] if len(html) > 0 && len(monospace) > 0 && html[0] == "1" && monospace[0] == "1" { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, `{"html":"cannot be set with monospace","monospace":"cannot be set with html","errors":["html and monospace are mutually exclusive"],"status":0,"request":"%s"}`, id) @@ -83,9 +83,9 @@ func TestPushoverMessageCLI(t *testing.T) { } func serverValidateHandler(w http.ResponseWriter, r *http.Request) { - r.ParseForm() + _ = r.ParseForm() - token, _ := r.Form["token"] + token := r.Form["token"] if len(token) > 0 && token[0] == "fail" { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, `{"token":"invalid","errors":["application token is invalid"],"status":0,"request":"e8488e4e-dbe1-4795-a253-3ef644aa14a6"}`) diff --git a/cmd/message.go b/cmd/message.go index 578fec7..58846e1 100644 --- a/cmd/message.go +++ b/cmd/message.go @@ -113,11 +113,11 @@ Required options are: --message `, Run: func(cmd *cobra.Command, args []string) { - if html == true { + if html { htmlField = enable } - if monospace == true { + if monospace { monospaceValue = enable } @@ -174,11 +174,11 @@ Required options are: // Required options messageCmd.Flags().StringVarP(&token, optionToken, "t", "", "Application's API token") - messageCmd.MarkFlagRequired(optionToken) + _ = messageCmd.MarkFlagRequired(optionToken) messageCmd.Flags().StringVarP(&user, optionUser, "u", "", "User/Group key") - messageCmd.MarkFlagRequired(optionUser) + _ = messageCmd.MarkFlagRequired(optionUser) messageCmd.Flags().StringVarP(&message, optionMessage, "m", "", "Notification message") - messageCmd.MarkFlagRequired(optionMessage) + _ = messageCmd.MarkFlagRequired(optionMessage) // Optional options messageCmd.Flags().StringVarP(&pushoverURL, optionPushoverURL, "", "", "Pushover API URL") diff --git a/cmd/validate.go b/cmd/validate.go index 323090d..f95f67a 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -114,9 +114,9 @@ Required options are: // Required options validateCmd.Flags().StringVarP(&token, optionToken, "t", "", "Application's API token") - validateCmd.MarkFlagRequired(optionToken) + _ = validateCmd.MarkFlagRequired(optionToken) validateCmd.Flags().StringVarP(&user, optionUser, "u", "", "User/Group key") - validateCmd.MarkFlagRequired(optionUser) + _ = validateCmd.MarkFlagRequired(optionUser) // Optional options validateCmd.Flags().StringVarP(&pushoverURL, optionPushoverURL, "", "", "Pushover API URL") diff --git a/go.mod b/go.mod index d53eb99..756e90f 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,13 @@ module github.com/arcanericky/pushover -go 1.13 +go 1.20 require ( - github.com/spf13/cobra v0.0.5 + github.com/spf13/cobra v1.6.1 golang.org/x/net v0.7.0 ) + +require ( + github.com/inconshreveable/mousetrap v1.0.1 // indirect + github.com/spf13/pflag v1.0.5 // indirect +) diff --git a/go.sum b/go.sum index 1e27524..f2aabb6 100644 --- a/go.sum +++ b/go.sum @@ -1,60 +1,12 @@ -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= -github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= -github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= -github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= -github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= -github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= -github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= -github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= -github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= -github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= -github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= -github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s= -github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= -github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= -github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= -github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= -github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= -github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= +github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= +github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 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= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/message.go b/message.go index 8790504..4ca63b3 100644 --- a/message.go +++ b/message.go @@ -166,12 +166,12 @@ type MessageResponse struct { // message, triggering a notification on a user's // device or a group's devices. // -// resp, err := pushover.MessageContext(context.Background(), -// pushover.MessageRequest{ -// Token: token, -// User: user, -// Message: message, -// }) +// resp, err := pushover.MessageContext(context.Background(), +// pushover.MessageRequest{ +// Token: token, +// User: user, +// Message: message, +// }) func MessageContext(ctx context.Context, request MessageRequest) (*MessageResponse, error) { var requestData io.Reader var contentType string @@ -219,11 +219,11 @@ func MessageContext(ctx context.Context, request MessageRequest) (*MessageRespon requestBody := &bytes.Buffer{} writer := multipart.NewWriter(requestBody) part, _ := writer.CreateFormFile("attachment", request.ImageName) - io.Copy(part, request.ImageReader) + _, _ = io.Copy(part, request.ImageReader) for _, v := range fields { if len(v.value) > 0 { - writer.WriteField(v.field, v.value) + _ = writer.WriteField(v.field, v.value) } } writer.Close() @@ -305,11 +305,11 @@ func MessageContext(ctx context.Context, request MessageRequest) (*MessageRespon // message, triggering a notification on a user's // device or a group's devices. // -// resp, err := pushover.Message(pushover.MessageRequest{ -// Token: token, -// User: user, -// Message: message, -// }) +// resp, err := pushover.Message(pushover.MessageRequest{ +// Token: token, +// User: user, +// Message: message, +// }) func Message(request MessageRequest) (*MessageResponse, error) { return MessageContext(context.Background(), request) } diff --git a/message_test.go b/message_test.go index 4613362..f20f74a 100644 --- a/message_test.go +++ b/message_test.go @@ -42,11 +42,11 @@ Invalid Priority const id = "deadbeef-dead-beef-dead-deadbeefdead" func serverHandler(w http.ResponseWriter, r *http.Request) { - r.ParseForm() - r.ParseMultipartForm(0) + _ = r.ParseForm() + _ = r.ParseMultipartForm(0) // Check message - value, _ := r.Form["message"] + value := r.Form["message"] if len(value) == 0 || len(value[0]) == 0 { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, `{"message":"cannot be blank","errors":["message cannot be blank"],"status":0,"request":"%s"}`, id) @@ -54,7 +54,7 @@ func serverHandler(w http.ResponseWriter, r *http.Request) { } // Check token - value, _ = r.Form["token"] + value = r.Form["token"] if len(value) == 0 || len(value[0]) == 0 { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, `{"token":"invalid","errors":["application token is invalid"],"status":0,"request":"%s"}`, id) @@ -62,7 +62,7 @@ func serverHandler(w http.ResponseWriter, r *http.Request) { } // Check user - user, _ := r.Form["user"] + user := r.Form["user"] if len(user) == 0 || len(user[0]) == 0 { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, `{"user":"invalid","errors":["user identifier is not a valid user, group, or subscribed user key"],"status":0,"request":"%s"}`, id) @@ -70,15 +70,15 @@ func serverHandler(w http.ResponseWriter, r *http.Request) { } // Check html and monospace - html, _ := r.Form["html"] - monospace, _ := r.Form["monospace"] + html := r.Form["html"] + monospace := r.Form["monospace"] if len(html) > 0 && len(monospace) > 0 && html[0] == "1" && monospace[0] == "1" { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, `{"html":"cannot be set with monospace","monospace":"cannot be set with html","errors":["html and monospace are mutually exclusive"],"status":0,"request":"%s"}`, id) return } - value, _ = r.Form["priority"] + value = r.Form["priority"] if len(value) > 0 && len(value[0]) > 0 { priority, err := strconv.Atoi(value[0]) if err != nil || priority < -2 || priority > 2 { @@ -133,7 +133,7 @@ func TestPushoverMessage(t *testing.T) { // Default Pushover URL messagesURL = apiServer.URL - r, e := MessageContext(context.TODO(), request) + r, _ := MessageContext(context.TODO(), request) if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id || r.Errors[0] != "message cannot be blank" || r.ErrorParameters["message"] != "cannot be blank" { t.Error("Default Pushover URL") @@ -141,20 +141,20 @@ func TestPushoverMessage(t *testing.T) { // Invalid Pushover URL request.PushoverURL = "\x7f" - r, e = MessageContext(context.TODO(), request) + _, e := MessageContext(context.TODO(), request) if _, ok := e.(*ErrInvalidRequest); !ok { t.Error("Invalid Pushover URL") } // Handling of no message request.PushoverURL = apiServer.URL - r, e = MessageContext(context.TODO(), request) + r, _ = MessageContext(context.TODO(), request) if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id || r.Errors[0] != "message cannot be blank" || r.ErrorParameters["message"] != "cannot be blank" { t.Error("Handling of no message without validation") } - r, e = Message(request) + r, _ = Message(request) if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id || r.Errors[0] != "message cannot be blank" || r.ErrorParameters["message"] != "cannot be blank" { t.Error("Handling of no message without validation") @@ -162,13 +162,13 @@ func TestPushoverMessage(t *testing.T) { // Handling of no token request.Message = "test message" - r, e = MessageContext(context.TODO(), request) + r, _ = MessageContext(context.TODO(), request) if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id || r.Errors[0] != "application token is invalid" || r.ErrorParameters["token"] != "invalid" { t.Error("Handling of no token without validation") } - r, e = Message(request) + r, _ = Message(request) if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id || r.Errors[0] != "application token is invalid" || r.ErrorParameters["token"] != "invalid" { t.Error("Handling of no token without validation") @@ -176,14 +176,14 @@ func TestPushoverMessage(t *testing.T) { // Handling of no user request.Token = "testtoken" - r, e = MessageContext(context.TODO(), request) + r, _ = MessageContext(context.TODO(), request) if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id || r.Errors[0] != "user identifier is not a valid user, group, or subscribed user key" || r.ErrorParameters["user"] != "invalid" { t.Error("Handling of no user without validation") } - r, e = Message(request) + r, _ = Message(request) if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id || r.Errors[0] != "user identifier is not a valid user, group, or subscribed user key" || r.ErrorParameters["user"] != "invalid" { @@ -192,7 +192,7 @@ func TestPushoverMessage(t *testing.T) { // Valid submission request.User = "testuser" - r, e = Message(request) + r, _ = Message(request) if r.HTTPStatusCode != http.StatusOK || r.APIStatus != 1 || r.Request != id || len(r.Errors) > 0 || len(r.ErrorParameters) > 0 { t.Error("Valid submit data") @@ -200,28 +200,28 @@ func TestPushoverMessage(t *testing.T) { // Invalid API Status in response request.User = "failstatus" - r, e = Message(request) + _, e = Message(request) if _, ok := e.(*ErrInvalidResponse); !ok { t.Error("Invalid API status in response") } // Invalid request ID in response request.User = "failrequest" - r, e = Message(request) + _, e = Message(request) if _, ok := e.(*ErrInvalidResponse); !ok { t.Error("Invalid request ID in response") } // Invalid json response request.User = "failjson" - r, e = Message(request) + _, e = Message(request) if _, ok := e.(*ErrInvalidResponse); !ok { t.Error("Invalid response JSON") } // Invalid body request.User = "failbody" - r, e = Message(request) + _, e = Message(request) if _, ok := e.(*ErrInvalidResponse); !ok { t.Error("Invalid response body") } @@ -237,7 +237,7 @@ func TestPushoverMessage(t *testing.T) { request.Device = "device" request.Priority = "0" request.Timestamp = "timestamp" - r, e = Message(request) + r, _ = Message(request) if r.HTTPStatusCode != http.StatusOK || r.APIStatus != 1 || r.Request != id || len(r.Errors) > 0 || len(r.ErrorParameters) > 0 { t.Error("All fields submitted") @@ -245,7 +245,7 @@ func TestPushoverMessage(t *testing.T) { // Priority of 2 yields additional "receipt" parameter request.Priority = "2" - r, e = Message(request) + r, _ = Message(request) if r.HTTPStatusCode != http.StatusOK || r.APIStatus != 1 || r.Request != id || len(r.Errors) > 0 || len(r.ErrorParameters) > 0 { t.Error("Priority 2") @@ -254,7 +254,7 @@ func TestPushoverMessage(t *testing.T) { // Context cancellation ctx, cancel := context.WithTimeout(context.Background(), 0*time.Millisecond) - r, e = MessageContext(ctx, request) + _, e = MessageContext(ctx, request) if e != context.DeadlineExceeded { t.Error("Context deadline exceeded") } @@ -262,7 +262,7 @@ func TestPushoverMessage(t *testing.T) { // Image attachment request.ImageReader = strings.NewReader("image data") - r, e = Message(request) + r, _ = Message(request) if r.HTTPStatusCode != http.StatusOK || r.APIStatus != 1 || r.Request != id || len(r.Errors) > 0 || len(r.ErrorParameters) > 0 { t.Error("Image attachment") @@ -270,7 +270,7 @@ func TestPushoverMessage(t *testing.T) { // Test http.PostForm() returning error apiServer.Close() - r, e = Message(request) + _, e = Message(request) if e == nil { t.Error("No API server") } diff --git a/scripts/pushover-test.sh b/scripts/pushover-test.sh new file mode 100755 index 0000000..052dfe3 --- /dev/null +++ b/scripts/pushover-test.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +go build -o pushover ./cmd/... +./pushover --help diff --git a/validate.go b/validate.go index ea2cc7d..e85de8e 100644 --- a/validate.go +++ b/validate.go @@ -83,11 +83,11 @@ type ValidateResponse struct { // Validate API. This function will check a user or group token // to determine if it is valid. // -// resp, err := pushover.ValidateContext(context.Background(), -// pushover.ValidateRequest{ -// Token: token, -// User: user, -// }) +// resp, err := pushover.ValidateContext(context.Background(), +// pushover.ValidateRequest{ +// Token: token, +// User: user, +// }) func ValidateContext(ctx context.Context, request ValidateRequest) (*ValidateResponse, error) { if len(request.PushoverURL) == 0 { request.PushoverURL = validateURL @@ -173,10 +173,10 @@ func ValidateContext(ctx context.Context, request ValidateRequest) (*ValidateRes // Validate API This function will check a user or group token // to determine if it is valid. // -// resp, err := pushover.Validate(pushover.ValidateRequest{ -// Token: token, -// User: user, -// }) +// resp, err := pushover.Validate(pushover.ValidateRequest{ +// Token: token, +// User: user, +// }) func Validate(request ValidateRequest) (*ValidateResponse, error) { return ValidateContext(context.Background(), request) } diff --git a/validate_test.go b/validate_test.go index 609c7ea..0ace83e 100644 --- a/validate_test.go +++ b/validate_test.go @@ -23,10 +23,10 @@ Token Invaild //const id = "deadbeef-dead-beef-dead-deadbeefdead" func validateServerHandler(w http.ResponseWriter, r *http.Request) { - r.ParseForm() + _ = r.ParseForm() // Check token - value, _ := r.Form["token"] + value := r.Form["token"] if len(value) == 0 || len(value[0]) == 0 { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, `{"token":"invalid","errors":["application token is invalid"],"status":0,"request":"%s"}`, id) @@ -34,7 +34,7 @@ func validateServerHandler(w http.ResponseWriter, r *http.Request) { } // Check user - user, _ := r.Form["user"] + user := r.Form["user"] if len(user) == 0 || len(user[0]) == 0 { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, `{"user":"invalid","errors":["user key is invalid"],"status":0,"request":"%s"}`, id) @@ -88,13 +88,13 @@ func TestPushoverValidate(t *testing.T) { } // Handling of no token - r, e = ValidateContext(context.TODO(), request) + r, _ = ValidateContext(context.TODO(), request) if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id || r.Errors[0] != "application token is invalid" || r.ErrorParameters["token"] != "invalid" { t.Error("Handling of no token without validation") } - r, e = Validate(request) + r, _ = Validate(request) if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id || r.Errors[0] != "application token is invalid" || r.ErrorParameters["token"] != "invalid" { t.Error("Handling of no token without validation") @@ -102,14 +102,14 @@ func TestPushoverValidate(t *testing.T) { // Handling of no user request.Token = "testtoken" - r, e = ValidateContext(context.TODO(), request) + r, _ = ValidateContext(context.TODO(), request) if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id || r.Errors[0] != "user key is invalid" || r.ErrorParameters["user"] != "invalid" { t.Error("Handling of no user without validation") } - r, e = Validate(request) + r, _ = Validate(request) if r.HTTPStatusCode != http.StatusBadRequest || r.APIStatus != 0 || r.Request != id || r.Errors[0] != "user key is invalid" || r.ErrorParameters["user"] != "invalid" { @@ -127,7 +127,7 @@ func TestPushoverValidate(t *testing.T) { // Context cancellation ctx, cancel := context.WithTimeout(context.Background(), 0*time.Millisecond) - r, e = ValidateContext(ctx, request) + _, e = ValidateContext(ctx, request) if e != context.DeadlineExceeded { t.Error("Context deadline exceeded") } @@ -135,21 +135,21 @@ func TestPushoverValidate(t *testing.T) { // Invalid API Status in response request.User = "failstatus" - r, e = Validate(request) + _, e = Validate(request) if _, ok := e.(*ErrInvalidResponse); !ok { t.Error("Invalid API status in response") } // Invalid request ID in response request.User = "failrequest" - r, e = Validate(request) + _, e = Validate(request) if _, ok := e.(*ErrInvalidResponse); !ok { t.Error("Invalid request ID in response") } // Invalid json response request.User = "failjson" - r, e = Validate(request) + _, e = Validate(request) if _, ok := e.(*ErrInvalidResponse); !ok { t.Error("Invalid response JSON") } @@ -165,14 +165,14 @@ func TestPushoverValidate(t *testing.T) { // Invalid body request.User = "failbody" - r, e = Validate(request) + _, e = Validate(request) if _, ok := e.(*ErrInvalidResponse); !ok { t.Error("Invalid response body") } // Test http.PostForm() returning error apiServer.Close() - r, e = Validate(request) + _, e = Validate(request) if e == nil { t.Error("No API server") }