Browse Source

Refactor (#9)

Reviewed-on: #9
Co-authored-by: jolheiser <john.olheiser@gmail.com>
Co-committed-by: jolheiser <john.olheiser@gmail.com>
main latest
jolheiser 1 month ago
parent
commit
2da056a909
  1. 23
      .golangci.yml
  2. 59
      .woodpecker.yml
  3. 66
      DOCS.md
  4. 14
      Earthfile
  5. 41
      Makefile
  6. 2
      README.md
  7. 4
      cmd/add.go
  8. 7
      cmd/cmd.go
  9. 8
      cmd/get.go
  10. 4
      cmd/remove.go
  11. 8
      cmd/search.go
  12. 6
      cmd/server.go
  13. 4
      cmd/update.go
  14. 4
      docs.go
  15. 7
      go.mod
  16. 49
      go.sum
  17. 8
      main.go
  18. 53
      router/router.go
  19. 5
      router/router_test.go
  20. 8
      tools.go

23
.golangci.yml

@ -1,23 +0,0 @@
linters:
enable:
- deadcode
- dogsled
- dupl
- errcheck
- funlen
- gocognit
- goconst
- gocritic
- gocyclo
- gofmt
- golint
- gosimple
- govet
- misspell
- prealloc
- staticcheck
- structcheck
- typecheck
- unparam
- unused
- varcheck

59
.woodpecker.yml

@ -0,0 +1,59 @@
clone:
git:
image: woodpeckerci/plugin-git:next
pipeline:
compliance:
image: golang:1.17
commands:
- go test -race ./...
- go vet ./...
- go run github.com/rs/zerolog/cmd/lint go.jolheiser.com/gpm
- go build
when:
event: pull_request
build:
image: golang:1.17
commands:
- GOOS="windows" go build
- GOOS="linux" go build
when:
event: [ push, tag ]
branch: main
release-main:
image: jolheiser/drone-gitea-main:latest
secrets:
- source: gitea_token
target: plugin_token
base: https://git.jojodev.com
files:
- "gpm"
- "gpm.exe"
when:
event: push
branch: main
release-tag:
image: plugins/gitea-release:1
secrets:
- source: gitea_token
target: plugin_api_key
base_url: https://git.jojodev.com
files:
- "gpm"
- "gpm.exe"
when:
event: tag
tag: v*
prune:
image: jolheiser/drone-gitea-prune
secrets:
- source: gitea_token
target: plugin_token
base: https://git.jojodev.com
when:
event: tag
tag: v*

66
DOCS.md

@ -0,0 +1,66 @@
# NAME
gpm - Go Package Manager
# SYNOPSIS
gpm
```
[--database|-d]=[value]
[--server|-s]=[value]
[--token|-t]=[value]
```
**Usage**:
```
gpm [GLOBAL OPTIONS] command [COMMAND OPTIONS] [ARGUMENTS...]
```
# GLOBAL OPTIONS
**--database, -d**="": path to gpm database for server (default: `${USERCONFIG}/gpm.db` or `${BINPATH}/gpm.db`)
**--server, -s**="": gpm server to use (default: https://gpm.jolheiser.com)
**--token, -t**="": gpm auth token to use
# COMMANDS
## add, a
Add a package
**--force, -f**: Overwrite existing package without prompt
**--local, -l**: local mode
## get, g
Get package(s)
## list, ls, l
List local packages
## remove, rm
Remove package(s)
## search, s
Search packages
## server, web
Start the gpm server
**--port, -p**="": Port to run the gpm server on (default: 3333)
## update, u
Update a package
**--local, -l**: local mode

14
Earthfile

@ -1,14 +0,0 @@
# To lint, install Earthly and run `earth +lint`
# This ensures the usage of the same version of golangci-lint
FROM golangci/golangci-lint:v1.37
WORKDIR /gpm
lint-cli:
COPY . .
RUN golangci-lint run
lint-lib:
COPY ./go-gpm .
RUN golangci-lint run

41
Makefile

@ -1,41 +0,0 @@
GO ?= go
VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')
.PHONY: build
build:
$(GO) build -ldflags '-s -w -X "go.jolheiser.com/gpm/router.Version=$(VERSION)"'
.PHONY: lint
lint:
earth +lint-cli
earth +lint-lib
.PHONY: fmt
fmt: fmt-cli fmt-lib
.PHONY: test
test: test-cli test-lib
.PHONY: fmt-cli
fmt-cli:
$(GO) fmt ./...
.PHONY: test-cli
test-cli:
$(GO) test -race ./...
.PHONY: fmt-lib
fmt-lib:
@cd go-gpm && $(GO) fmt ./...
.PHONY: test-lib
test-lib:
@cd go-gpm && $(GO) test -race ./...
.PHONY: docker-build
docker-build:
docker build -f docker/Dockerfile -t jolheiser/gpm .
.PHONY: docker-push
docker-push: docker-build
docker push jolheiser/gpm

2
README.md

@ -18,7 +18,7 @@ Using either a GPM server or local config, I can instead `gpm get cli` which fin
* `list` - List local packages
* `config` - Change local configuration
* `get` - Get a list of packages
* e.g. `gpm get beaver survey bbolt cli chi` to get all the modules needed for gpm itself (assuming the map resolves to the same packages)
* e.g. `gpm get zerolog survey bbolt cli chi` to get all the modules needed for gpm itself (assuming the map resolves to the same packages)
* `server` - Start a gpm server

4
cmd/add.go

@ -2,6 +2,7 @@ package cmd
import (
"context"
"github.com/rs/zerolog/log"
"regexp"
"strings"
@ -11,7 +12,6 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
)
var Add = cli.Command{
@ -85,6 +85,6 @@ func doAdd(_ *cli.Context) error {
}
}
beaver.Infof("Added %s", yellow.Format(nameAnswer))
log.Info().Msgf("Added %q", nameAnswer)
return nil
}

7
cmd/cmd.go

@ -12,11 +12,8 @@ import (
"go.jolheiser.com/gpm/router"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver/color"
)
var yellow = color.FgYellow
func New() *cli.App {
app := cli.NewApp()
app.Name = "gpm"
@ -53,7 +50,7 @@ func New() *cli.App {
Aliases: []string{"d"},
Usage: "path to gpm database for server",
Value: dbPath(),
DefaultText: "`${HOME}/gpm.db` or `${BINPATH}/gpm.db`",
DefaultText: "`${USERCONFIG}/gpm.db` or `${BINPATH}/gpm.db`",
EnvVars: []string{"GPM_DATABASE"},
Destination: &flags.Database,
},
@ -63,7 +60,7 @@ func New() *cli.App {
func dbPath() string {
fn := "gpm.db"
home, err := os.UserHomeDir()
home, err := os.UserConfigDir()
if err != nil {
bin, err := os.Executable()
if err != nil {

8
cmd/get.go

@ -2,6 +2,7 @@ package cmd
import (
"context"
"github.com/rs/zerolog/log"
"os"
"os/exec"
"strings"
@ -11,7 +12,6 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
)
var Get = cli.Command{
@ -40,13 +40,13 @@ func doGet(ctx *cli.Context) error {
for _, p := range pkgs {
pkg, err := client.Get(context.Background(), p)
if err != nil {
beaver.Error(err)
log.Err(err).Msg("")
continue
}
beaver.Infof("getting `%s`...", pkg)
log.Info().Msgf("getting %q...", pkg.Import)
if err := goGet(pkg.Import); err != nil {
beaver.Error(err)
log.Err(err).Msg("")
}
}

4
cmd/remove.go

@ -2,6 +2,7 @@ package cmd
import (
"context"
"github.com/rs/zerolog/log"
"go.jolheiser.com/gpm/cmd/flags"
"go.jolheiser.com/gpm/database"
@ -9,7 +10,6 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
)
var Remove = cli.Command{
@ -63,6 +63,6 @@ func doRemove(_ *cli.Context) error {
}
}
beaver.Infof("Removed %s", yellow.Format(pkgName))
log.Info().Msgf("Removed %q", pkgName)
return nil
}

8
cmd/search.go

@ -1,11 +1,11 @@
package cmd
import (
"github.com/rs/zerolog/log"
"go.jolheiser.com/gpm/go-gpm"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
)
var Search = cli.Command{
@ -41,12 +41,12 @@ func doSearch(_ *cli.Context) error {
for _, name := range a {
pkg, ok := pkgMap[name]
if !ok {
beaver.Errorf("could not find package for `%s`", name)
log.Error().Msgf("could not find package for %q", name)
continue
}
beaver.Infof("getting `%s`...", name)
log.Info().Msgf("getting %q...", name)
if err := goGet(pkg.Import); err != nil {
beaver.Error(err)
log.Err(err).Msg("")
}
}

6
cmd/server.go

@ -3,6 +3,7 @@ package cmd
import (
"errors"
"fmt"
"github.com/rs/zerolog/log"
"net/http"
"go.jolheiser.com/gpm/cmd/flags"
@ -10,7 +11,6 @@ import (
"go.jolheiser.com/gpm/router"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
)
var Server = cli.Command{
@ -37,10 +37,10 @@ func doServer(_ *cli.Context) error {
db, err := database.Load(flags.Database)
if err != nil {
beaver.Fatalf("could not load database at %s: %v", flags.Database, err)
log.Fatal().Msgf("could not load database at %q: %v", flags.Database, err)
}
beaver.Infof("Running gpm server at http://localhost:%d", flags.Port)
log.Info().Msgf("Running gpm server at http://localhost:%d", flags.Port)
if err := http.ListenAndServe(fmt.Sprintf(":%d", flags.Port), router.New(flags.Token, db)); err != nil {
return err
}

4
cmd/update.go

@ -2,6 +2,7 @@ package cmd
import (
"context"
"github.com/rs/zerolog/log"
"go.jolheiser.com/gpm/cmd/flags"
"go.jolheiser.com/gpm/database"
@ -9,7 +10,6 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
)
var Update = cli.Command{
@ -81,6 +81,6 @@ func doUpdate(_ *cli.Context) error {
}
}
beaver.Infof("Updated %s", yellow.Format(pkgName))
log.Info().Msgf("Updated %q", pkgName)
return nil
}

4
docs.go

@ -1,4 +1,5 @@
//+build docs
//go:build generate
// +build generate
package main
@ -9,6 +10,7 @@ import (
"go.jolheiser.com/gpm/cmd"
)
//go:generate go run docs.go
func main() {
app := cmd.New()

7
go.mod

@ -6,14 +6,11 @@ require (
github.com/AlecAivazis/survey/v2 v2.2.7
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/go-chi/chi v1.5.2
github.com/go-chi/render v1.0.1
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/rs/zerolog v1.26.0
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/urfave/cli/v2 v2.3.0
go.etcd.io/bbolt v1.3.5
go.jolheiser.com/beaver v1.1.1
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073 // indirect
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect
golang.org/x/text v0.3.5 // indirect
)

49
go.sum

@ -1,9 +1,9 @@
github.com/AlecAivazis/survey/v2 v2.2.7 h1:5NbxkF4RSKmpywYdcRgUmos1o+roJY8duCLZXbVjoig=
github.com/AlecAivazis/survey/v2 v2.2.7/go.mod h1:9DYvHgXtiXm6nCn+jXnOXLKbH+Yo9u8fAS/SduGdoPk=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8 h1:xzYJEypr/85nBpB11F9br+3HUrpgb+fcm5iADzXXYEw=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61PbNXchhh0oikYAH+4Pcfw5LKv21+Jnpr6r6Pc=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
@ -11,6 +11,9 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi v1.5.2 h1:YcLIBANL4OTaAOcTdp//sskGa0yGACQMCtbnr7YEn0Q=
github.com/go-chi/chi v1.5.2/go.mod h1:REp24E+25iKvxgeTfHmdUoL5x15kBiDBlnIl5bCwe2k=
github.com/go-chi/render v1.0.1 h1:4/5tis2cKaNdnv9zFLfXzcquC9HbeZgCnxGnKrltBS8=
github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1OvJns=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 h1:WlZsjVhE8Af9IcZDGgJGQpNflI3+MJSBhsgT5PCtzBQ=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
@ -26,48 +29,56 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.26.0 h1:ORM4ibhEZeTeQlCojCK2kPz1ogAY4bGs4tD+SaAdGaE=
github.com/rs/zerolog v1.26.0/go.mod h1:yBiM87lvSqX8h0Ww4sdzNSkVYZ8dL2xjZJG1lAuGZEo=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/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/stretchr/testify v1.2.1 h1:52QO5WkIUcHGIR7EnGagH88x1bUzqGXTC5/1bDTUQ7U=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0=
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
go.jolheiser.com/beaver v1.1.1 h1:py8Zj3tjT52dUzsvnu97aiLj1fBJjDJiK6kHjKJejMQ=
go.jolheiser.com/beaver v1.1.1/go.mod h1:7X4F5+XOGSC3LejTShoBdqtRCnPWcnRgmYGmG3EKW8g=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190530182044-ad28b68e88f1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073 h1:8qxJSnu+7dRq6upnbntrmriWByIakBuct5OM/MdQC1M=
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf h1:MZ2shdL+ZM/XzY3ZGOnh4Nlpnxz5GSOhOmtHo3iPU6M=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e h1:FDhOuMEY4JVRztM/gsbk+IKUQ8kj74bxZrgw87eMMVc=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.7 h1:6j8CgantCy3yc8JGBqkDLMKWqZ0RDU2g1HVgacojGWQ=
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/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.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

8
main.go

@ -1,15 +1,17 @@
package main
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"os"
"go.jolheiser.com/gpm/cmd"
"go.jolheiser.com/beaver"
)
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
if err := cmd.New().Run(os.Args); err != nil {
beaver.Error(err)
log.Err(err).Msg("")
}
}

53
router/router.go

@ -1,8 +1,8 @@
package router
import (
"encoding/json"
"io"
"github.com/go-chi/render"
"github.com/rs/zerolog/log"
"net/http"
"time"
@ -11,7 +11,6 @@ import (
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"go.jolheiser.com/beaver"
)
var Version = "develop"
@ -31,41 +30,35 @@ func New(token string, db *database.Database) *chi.Mux {
return r
}
func handleHome(db *database.Database) func(res http.ResponseWriter, _ *http.Request) {
return func(res http.ResponseWriter, _ *http.Request) {
func handleHome(db *database.Database) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
pkgs, err := db.Packages()
if err != nil {
beaver.Error(err)
log.Err(err).Msg("")
return
}
status, err := json.Marshal(gpm.Info{
render.JSON(w, r, gpm.Info{
Version: Version,
NumPackages: len(pkgs),
Packages: pkgs,
})
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
_, _ = res.Write([]byte("{}"))
return
}
_, _ = res.Write(status)
}
}
func getPackage(db *database.Database) func(http.ResponseWriter, *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
name := chi.URLParam(req, "name")
return func(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
pkg, err := db.PackageJSON(name)
pkg, err := db.Package(name)
if err != nil {
res.WriteHeader(http.StatusNotFound)
_, _ = res.Write([]byte("{}"))
w.WriteHeader(http.StatusNotFound)
render.JSON(w, r, struct{}{})
return
}
_, _ = res.Write(pkg)
render.JSON(w, r, pkg)
}
}
@ -76,18 +69,12 @@ func addUpdatePackage(db *database.Database, token string) func(http.ResponseWri
return
}
data, err := io.ReadAll(req.Body)
if err != nil {
res.WriteHeader(http.StatusBadRequest)
return
}
defer req.Body.Close()
var pkg gpm.Package
if err := json.Unmarshal(data, &pkg); err != nil {
if err := render.DecodeJSON(req.Body, &pkg); err != nil {
res.WriteHeader(http.StatusBadRequest)
return
}
defer req.Body.Close()
exists, err := db.PackageJSON(pkg.Name)
if err != nil {
@ -128,18 +115,12 @@ func removePackage(db *database.Database, token string) func(http.ResponseWriter
return
}
data, err := io.ReadAll(req.Body)
if err != nil {
res.WriteHeader(http.StatusBadRequest)
return
}
defer req.Body.Close()
var pkg gpm.Package
if err := json.Unmarshal(data, &pkg); err != nil {
if err := render.DecodeJSON(req.Body, &pkg); err != nil {
res.WriteHeader(http.StatusBadRequest)
return
}
defer req.Body.Close()
if err := db.RemovePackage(pkg.Name); err != nil {
res.WriteHeader(http.StatusInternalServerError)

5
router/router_test.go

@ -2,6 +2,7 @@ package router
import (
"context"
"github.com/rs/zerolog/log"
"net/http/httptest"
"os"
"path/filepath"
@ -9,8 +10,6 @@ import (
"go.jolheiser.com/gpm/database"
"go.jolheiser.com/gpm/go-gpm"
"go.jolheiser.com/beaver"
)
var (
@ -29,7 +28,7 @@ func TestMain(m *testing.M) {
db, err := database.Load(dbPath)
if err != nil {
beaver.Fatalf("could not load database at %s: %v", dbPath, err)
log.Fatal().Msgf("could not load database at %q: %v", dbPath, err)
}
server = httptest.NewServer(New(token, db))

8
tools.go

@ -0,0 +1,8 @@
//go:build tools
// +build tools
package main
import (
_ "github.com/rs/zerolog/cmd/lint"
)
Loading…
Cancel
Save