Code improvements (#2)

Code improvements

Signed-off-by: jolheiser <john.olheiser@gmail.com>

Co-authored-by: jolheiser <john.olheiser@gmail.com>
Reviewed-on: https://gitea.com/jolheiser/gpm/pulls/2
pull/4/head
John Olheiser 2020-10-07 04:11:36 +00:00
parent 932d05f5ab
commit 0577fb4dbc
19 changed files with 476 additions and 350 deletions

10
Earthfile 100644
View File

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

View File

@ -1,32 +1,9 @@
DIST := dist
GO ?= go GO ?= go
SHASUM ?= shasum -a 256 VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')
ifneq ($(DRONE_TAG),)
VERSION ?= $(subst v,,$(DRONE_TAG))
LONG_VERSION ?= $(VERSION)
else
ifneq ($(DRONE_BRANCH),)
VERSION ?= $(subst release/v,,$(DRONE_BRANCH))
else
VERSION ?= master
endif
LONG_VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')
endif
LDFLAGS := $(LDFLAGS) -X "go.jolheiser.com/gpm/modules/config.Version=$(LONG_VERSION)"
.PHONY: build .PHONY: build
build: build:
$(GO) build -ldflags '-s -w $(LDFLAGS)' $(GO) build -ldflags '-s -w -X "go.jolheiser.com/gpm/modules/config.Version=$(VERSION)"'
.PHONY: lint
lint:
@hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
export BINARY="golangci-lint"; \
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.1; \
fi
golangci-lint run --timeout 5m
.PHONY: fmt .PHONY: fmt
fmt: fmt:
@ -35,52 +12,3 @@ fmt:
.PHONY: test .PHONY: test
test: test:
$(GO) test -race ./... $(GO) test -race ./...
.PHONY: release
release: release-dirs check-xgo release-windows release-linux release-darwin release-copy release-compress release-check
.PHONY: check-xgo
check-xgo:
@hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
$(GO) get -u src.techknowlogick.com/xgo; \
fi
.PHONY: release-dirs
release-dirs:
mkdir -p $(DIST)/binaries $(DIST)/release
.PHONY: release-windows
release-windows:
xgo -dest $(DIST)/binaries -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'windows/*' -out gpm-$(VERSION) .
ifeq ($(CI),drone)
cp /build/* $(DIST)/binaries
endif
.PHONY: release-linux
release-linux:
xgo -dest $(DIST)/binaries -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'linux/amd64,linux/386,linux/arm-5,linux/arm-6,linux/arm64,linux/mips64le,linux/mips,linux/mipsle' -out gpm-$(VERSION) .
ifeq ($(CI),drone)
cp /build/* $(DIST)/binaries
endif
.PHONY: release-darwin
release-darwin:
xgo -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '$(LDFLAGS)' -targets 'darwin/*' -out gpm-$(VERSION) .
ifeq ($(CI),drone)
cp /build/* $(DIST)/binaries
endif
.PHONY: release-copy
release-copy:
cd $(DIST); for file in `find /build -type f -name "*"`; do cp $${file} ./release/; done;
.PHONY: release-check
release-check:
cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "checksumming $${file}" && $(SHASUM) `echo $${file} | sed 's/^..//'` > $${file}.sha256; done;
.PHONY: release-compress
release-compress:
@hash gxz > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
$(GO) get -u github.com/ulikunitz/xz/cmd/gxz; \
fi
cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "compressing $${file}" && gxz -k -9 $${file}; done;

View File

@ -1,12 +1,14 @@
package cmd package cmd
import ( import (
"regexp"
"strings"
"go.jolheiser.com/gpm/config"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go.jolheiser.com/beaver" "go.jolheiser.com/beaver"
"go.jolheiser.com/gpm/modules/config"
"regexp"
"strings"
) )
var Add = cli.Command{ var Add = cli.Command{
@ -25,6 +27,10 @@ var Add = cli.Command{
var vPattern = regexp.MustCompile(`v\d+$`) var vPattern = regexp.MustCompile(`v\d+$`)
func doAdd(ctx *cli.Context) error { func doAdd(ctx *cli.Context) error {
cfg, err := config.Load()
if err != nil {
return err
}
goGetQuestion := &survey.Input{ goGetQuestion := &survey.Input{
Message: "Package go-get import", Message: "Package go-get import",
@ -56,9 +62,9 @@ func doAdd(ctx *cli.Context) error {
Name: nameAnswer, Name: nameAnswer,
Import: goGetAnswer, Import: goGetAnswer,
} }
config.AddPackages(ctx.Bool("force"), pkg) cfg.AddPackages(ctx.Bool("force"), pkg)
if err := config.Save(); err != nil { if err := cfg.Save(); err != nil {
return err return err
} }

View File

@ -1,15 +1,18 @@
package cmd package cmd
import ( import (
"go.jolheiser.com/gpm/config"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go.jolheiser.com/gpm/modules/config"
) )
var Flags = []cli.Flag{ func NewFlags(cfg *config.Config) []cli.Flag {
&cli.StringFlag{ return []cli.Flag{
Name: "url", &cli.StringFlag{
Aliases: []string{"u"}, Name: "url",
Usage: "gpm server to use", Aliases: []string{"u"},
Value: config.GPMURL, Usage: "gpm server to use",
}, Value: cfg.GPMURL,
},
}
} }

View File

@ -1,10 +1,11 @@
package cmd package cmd
import ( import (
"go.jolheiser.com/gpm/config"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go.jolheiser.com/beaver" "go.jolheiser.com/beaver"
"go.jolheiser.com/gpm/modules/config"
) )
var Config = cli.Command{ var Config = cli.Command{
@ -14,10 +15,15 @@ var Config = cli.Command{
Action: doConfig, Action: doConfig,
} }
func doConfig(ctx *cli.Context) error { func doConfig(_ *cli.Context) error {
cfg, err := config.Load()
if err != nil {
return err
}
urlQuestion := &survey.Input{ urlQuestion := &survey.Input{
Message: "gpm URL", Message: "gpm URL",
Default: config.GPMURL, Default: cfg.GPMURL,
} }
var urlAnswer string var urlAnswer string
@ -25,8 +31,8 @@ func doConfig(ctx *cli.Context) error {
return err return err
} }
config.GPMURL = urlAnswer cfg.GPMURL = urlAnswer
if err := config.Save(); err != nil { if err := cfg.Save(); err != nil {
return err return err
} }

View File

@ -2,8 +2,10 @@ package cmd
import ( import (
"fmt" "fmt"
"go.jolheiser.com/gpm/config"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go.jolheiser.com/gpm/modules/config"
) )
var Export = cli.Command{ var Export = cli.Command{
@ -12,9 +14,13 @@ var Export = cli.Command{
Action: doExport, Action: doExport,
} }
func doExport(ctx *cli.Context) error { func doExport(_ *cli.Context) error {
cfg, err := config.Load()
if err != nil {
return err
}
export, err := config.Export() export, err := cfg.Export()
if err != nil { if err != nil {
return err return err
} }

View File

@ -3,15 +3,17 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
"go.jolheiser.com/gpm/modules/config"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"go.jolheiser.com/gpm/config"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
) )
var Get = cli.Command{ var Get = cli.Command{
@ -31,6 +33,11 @@ var Get = cli.Command{
} }
func doGet(ctx *cli.Context) error { func doGet(ctx *cli.Context) error {
cfg, err := config.Load()
if err != nil {
return err
}
pkgs := ctx.Args().Slice() pkgs := ctx.Args().Slice()
if len(pkgs) == 0 { if len(pkgs) == 0 {
pkgsQuestion := &survey.Multiline{ pkgsQuestion := &survey.Multiline{
@ -45,7 +52,7 @@ func doGet(ctx *cli.Context) error {
pkgs = strings.Split(pkgsAnswer, "\n") pkgs = strings.Split(pkgsAnswer, "\n")
} }
local := config.PackageMap() local := cfg.Packages.Map()
for _, pkg := range pkgs { for _, pkg := range pkgs {
var url string var url string
if u, ok := local[pkg]; ok && !ctx.Bool("ignore-local") { if u, ok := local[pkg]; ok && !ctx.Bool("ignore-local") {

View File

@ -3,12 +3,14 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
"go.jolheiser.com/gpm/modules/config"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
"go.jolheiser.com/gpm/config"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
) )
var Import = cli.Command{ var Import = cli.Command{
@ -25,6 +27,11 @@ var Import = cli.Command{
} }
func doImport(ctx *cli.Context) error { func doImport(ctx *cli.Context) error {
cfg, err := config.Load()
if err != nil {
return err
}
if ctx.NArg() == 0 { if ctx.NArg() == 0 {
return errors.New("must point to either a JSON file or gpm server export endpoint") return errors.New("must point to either a JSON file or gpm server export endpoint")
} }
@ -38,7 +45,6 @@ func doImport(ctx *cli.Context) error {
} }
var data []byte var data []byte
var err error
if isJSON { if isJSON {
data, err = ioutil.ReadFile(arg) data, err = ioutil.ReadFile(arg)
if err != nil { if err != nil {
@ -55,16 +61,15 @@ func doImport(ctx *cli.Context) error {
return err return err
} }
defer resp.Body.Close() defer resp.Body.Close()
} }
var cfg config.Config var importPkgs []config.Package
if err := json.Unmarshal(data, &cfg); err != nil { if err := json.Unmarshal(data, &importPkgs); err != nil {
return err return err
} }
config.AddPackages(ctx.Bool("force"), cfg.Packages...) cfg.AddPackages(ctx.Bool("force"), importPkgs...)
if err := config.Save(); err != nil { if err := cfg.Save(); err != nil {
return err return err
} }

View File

@ -1,9 +1,10 @@
package cmd package cmd
import ( import (
"go.jolheiser.com/gpm/config"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go.jolheiser.com/beaver" "go.jolheiser.com/beaver"
"go.jolheiser.com/gpm/modules/config"
) )
var List = cli.Command{ var List = cli.Command{
@ -13,8 +14,13 @@ var List = cli.Command{
Action: doList, Action: doList,
} }
func doList(ctx *cli.Context) error { func doList(_ *cli.Context) error {
for _, pkg := range config.Packages { cfg, err := config.Load()
if err != nil {
return err
}
for _, pkg := range cfg.Packages {
beaver.Infof("%s -> %s", pkg.Name, pkg.Import) beaver.Infof("%s -> %s", pkg.Name, pkg.Import)
} }
return nil return nil

View File

@ -2,21 +2,28 @@ package cmd
import ( import (
"fmt" "fmt"
"strings"
"go.jolheiser.com/gpm/config"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go.jolheiser.com/beaver" "go.jolheiser.com/beaver"
"go.jolheiser.com/gpm/modules/config"
"strings"
) )
var Remove = cli.Command{ var Remove = cli.Command{
Name: "remove", Name: "remove",
Aliases: []string{"rm"}, Aliases: []string{"rm"},
Usage: "Remove a package", Usage: "Remove package(s)",
Action: doRemove, Action: doRemove,
} }
func doRemove(ctx *cli.Context) error { func doRemove(_ *cli.Context) error {
cfg, err := config.Load()
if err != nil {
return err
}
pkgQuestion := &survey.Input{ pkgQuestion := &survey.Input{
Message: "Package name", Message: "Package name",
} }
@ -26,7 +33,7 @@ func doRemove(ctx *cli.Context) error {
return err return err
} }
for idx, p := range config.Packages { for idx, p := range cfg.Packages {
if strings.EqualFold(p.Name, pkgAnswer) { if strings.EqualFold(p.Name, pkgAnswer) {
confirm := &survey.Confirm{ confirm := &survey.Confirm{
Message: fmt.Sprintf("Are you sure you want to remove %s (%s) ?", p.Name, p.Import), Message: fmt.Sprintf("Are you sure you want to remove %s (%s) ?", p.Name, p.Import),
@ -39,8 +46,8 @@ func doRemove(ctx *cli.Context) error {
} }
if answer { if answer {
config.Packages = append(config.Packages[:idx], config.Packages[idx+1:]...) cfg.Packages = append(cfg.Packages[:idx], cfg.Packages[idx+1:]...)
if err := config.Save(); err != nil { if err := cfg.Save(); err != nil {
return err return err
} }
beaver.Infof("Removed `%s` from local gpm.", p.Name) beaver.Infof("Removed `%s` from local gpm.", p.Name)

89
cmd/search.go 100644
View File

@ -0,0 +1,89 @@
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"go.jolheiser.com/gpm/config"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
)
var Search = cli.Command{
Name: "search",
Aliases: []string{"s"},
Usage: "Search packages",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "local",
Usage: "Search locally",
},
},
Action: doSearch,
}
func doSearch(ctx *cli.Context) error {
cfg, err := config.Load()
if err != nil {
return err
}
packageMap := cfg.Packages.Map()
packageSlice := cfg.Packages.Slice()
if !ctx.Bool("local") {
export, err := queryExport(ctx.String("url"))
if err != nil {
return err
}
packageMap = export.Map()
packageSlice = export.Slice()
}
q := &survey.MultiSelect{
Message: "Select packages",
Options: packageSlice,
}
var a []string
if err := survey.AskOne(q, &a); err != nil {
return err
}
for _, name := range a {
pkg, ok := packageMap[name]
if !ok {
beaver.Errorf("could not find package for `%s`", name)
continue
}
beaver.Infof("getting `%s`...", name)
if err := goGet(pkg.Import); err != nil {
beaver.Error(err)
}
}
return nil
}
func queryExport(server string) (config.Packages, error) {
resp, err := http.Get(fmt.Sprintf("%s/export", server))
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var importPkgs config.Packages
if err := json.Unmarshal(data, &importPkgs); err != nil {
return nil, err
}
return importPkgs, nil
}

View File

@ -2,10 +2,13 @@ package cmd
import ( import (
"fmt" "fmt"
"net/http"
"go.jolheiser.com/gpm/config"
"go.jolheiser.com/gpm/router"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go.jolheiser.com/beaver" "go.jolheiser.com/beaver"
"go.jolheiser.com/gpm/modules/router"
"net/http"
) )
var Server = cli.Command{ var Server = cli.Command{
@ -23,8 +26,13 @@ var Server = cli.Command{
} }
func doServer(ctx *cli.Context) error { func doServer(ctx *cli.Context) error {
cfg, err := config.Load()
if err != nil {
return err
}
beaver.Infof("Running gpm server at http://localhost:%s", ctx.String("port")) beaver.Infof("Running gpm server at http://localhost:%s", ctx.String("port"))
if err := http.ListenAndServe(fmt.Sprintf(":%s", ctx.String("port")), router.Init()); err != nil { if err := http.ListenAndServe(fmt.Sprintf(":%s", ctx.String("port")), router.New(cfg)); err != nil {
return err return err
} }
return nil return nil

146
config/config.go 100644
View File

@ -0,0 +1,146 @@
package config
import (
"encoding/json"
"fmt"
"os"
"path"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/pelletier/go-toml"
"go.jolheiser.com/beaver"
)
var Version = "develop"
type Config struct {
path string
GPMURL string `toml:"gpm-url" json:"gpm_url"`
Packages Packages `toml:"package" json:"packages"`
}
type Package struct {
Name string `toml:"name" json:"name"`
Import string `toml:"import" json:"import"`
}
type Packages []Package
func Load() (*Config, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("could not get user home dir: %v", err)
}
home = path.Join(home, ".gpm")
homeEnv := os.Getenv("GPM_HOME")
if homeEnv != "" {
home = homeEnv
}
configPath := path.Join(home, "gpm.toml")
configEnv := os.Getenv("GPM_CONFIG")
if configEnv != "" {
configPath = configEnv
}
if _, err := os.Stat(configPath); os.IsNotExist(err) {
if err := os.MkdirAll(path.Dir(configPath), os.ModePerm); err != nil {
return nil, fmt.Errorf("could not create gpm home: %v", err)
}
if _, err := os.Create(configPath); err != nil {
return nil, fmt.Errorf("could not create gpm config: %v", err)
}
}
var cfg Config
tree, err := toml.LoadFile(configPath)
if err != nil {
return nil, fmt.Errorf("could not decode gpm config: %v", err)
}
if err = tree.Unmarshal(&cfg); err != nil {
return nil, fmt.Errorf("could not unmarshal config: %v", err)
}
dupe := make(map[string]bool)
for _, pkg := range cfg.Packages {
name := strings.ToLower(pkg.Name)
if ok := dupe[name]; ok {
return nil, fmt.Errorf("duplicate package for %s", pkg.Name)
}
dupe[name] = true
}
cfg.path = configPath
return &cfg, nil
}
func (c *Config) Save() error {
fi, err := os.Create(c.path)
if err != nil {
return err
}
defer fi.Close()
if err := toml.NewEncoder(fi).Encode(c); err != nil {
return err
}
return nil
}
func (c *Config) Export() (string, error) {
data, err := json.Marshal(c.Packages)
return string(data), err
}
func (p Packages) Slice() []string {
pkgs := make([]string, len(p))
for idx, pkg := range p {
pkgs[idx] = fmt.Sprintf("%s (%s)", pkg.Name, pkg.Import)
}
return pkgs
}
func (p Packages) Map() map[string]Package {
pkgs := make(map[string]Package)
for _, pkg := range p {
pkgs[pkg.Name] = pkg
}
return pkgs
}
func (c *Config) AddPackages(force bool, pkgs ...Package) {
for _, pkg := range pkgs {
for idx, p := range c.Packages {
if strings.EqualFold(p.Name, pkg.Name) {
if force {
c.Packages[idx] = pkg
break
}
forceQuestion := &survey.Confirm{
Message: fmt.Sprintf("Package `%s` (%s) already exists. Overwrite with `%s`?", p.Name, p.Import, p.Import),
Default: false,
}
var forceAnswer bool
if err := survey.AskOne(forceQuestion, &forceAnswer); err != nil {
beaver.Error(err)
break
}
if !forceAnswer {
beaver.Errorf("leaving package `%s` as-is", pkg.Name)
break
}
c.Packages[idx] = pkg
break
}
}
c.Packages = append(c.Packages, pkg)
}
}

19
go.mod
View File

@ -1,12 +1,17 @@
module go.jolheiser.com/gpm module go.jolheiser.com/gpm
go 1.13 go 1.15
require ( require (
github.com/AlecAivazis/survey/v2 v2.0.5 github.com/AlecAivazis/survey/v2 v2.1.1
github.com/BurntSushi/toml v0.3.1 github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/go-chi/chi v4.0.3+incompatible github.com/go-chi/chi v4.1.2+incompatible
github.com/mitchellh/go-homedir v1.1.0 github.com/mattn/go-colorable v0.1.8 // indirect
github.com/urfave/cli/v2 v2.1.1 github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
go.jolheiser.com/beaver v1.0.1 github.com/pelletier/go-toml v1.8.1
github.com/urfave/cli/v2 v2.2.0
go.jolheiser.com/beaver v1.0.2
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 // indirect
golang.org/x/sys v0.0.0-20201006155630-ac719f4daadf // indirect
golang.org/x/text v0.3.3 // indirect
) )

37
go.sum
View File

@ -1,15 +1,17 @@
github.com/AlecAivazis/survey/v2 v2.0.5 h1:xpZp+Q55wi5C7Iaze+40onHnEkex1jSc34CltJjOoPM= github.com/AlecAivazis/survey/v2 v2.1.1 h1:LEMbHE0pLj75faaVEKClEX1TM4AJmmnOh9eimREzLWI=
github.com/AlecAivazis/survey/v2 v2.0.5/go.mod h1:WYBhg6f0y/fNYUuesWQc0PKbJcEliGcYHB9sNT3Bg74= github.com/AlecAivazis/survey/v2 v2.1.1/go.mod h1:9FJRdMdDm8rnT+zHVbvQT2RTSTLq0Ttd6q3Vl2fahjk=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 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 h1:xzYJEypr/85nBpB11F9br+3HUrpgb+fcm5iADzXXYEw=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61PbNXchhh0oikYAH+4Pcfw5LKv21+Jnpr6r6Pc= github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61PbNXchhh0oikYAH+4Pcfw5LKv21+Jnpr6r6Pc=
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 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/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=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 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/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi v4.0.3+incompatible h1:gakN3pDJnzZN5jqFV2TEdF66rTfKeITyR8qu6ekICEY= github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec=
github.com/go-chi/chi v4.0.3+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 h1:WlZsjVhE8Af9IcZDGgJGQpNflI3+MJSBhsgT5PCtzBQ= 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/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
@ -18,12 +20,18 @@ github.com/kr/pty v1.1.4 h1:5Myjjh3JY/NaAi4IsUbHADytDyl1VE1Y9PXDlL+P/VQ=
github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU= github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE= github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM=
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 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/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 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
@ -32,13 +40,15 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5I
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= 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 h1:52QO5WkIUcHGIR7EnGagH88x1bUzqGXTC5/1bDTUQ7U=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k= github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4=
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
go.jolheiser.com/beaver v1.0.1 h1:gt3aGEr5Bj4ZjDF1g8t8OYOGRCRXGaanGR9CmXUxez8= go.jolheiser.com/beaver v1.0.2 h1:KA2D6iO8MQhZi1nZYi/Chak/f1Cxfrs6b1XO623+Khk=
go.jolheiser.com/beaver v1.0.1/go.mod h1:7X4F5+XOGSC3LejTShoBdqtRCnPWcnRgmYGmG3EKW8g= go.jolheiser.com/beaver v1.0.2/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-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5 h1:8dUaAV7K4uHsF56JQWkprecIQKdPHtR9jCHF5nB8uzc= golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5 h1:8dUaAV7K4uHsF56JQWkprecIQKdPHtR9jCHF5nB8uzc=
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 h1:hb9wdF1z5waM+dSIICn1l0DkLVDT3hqhhQsDNUmHPRE=
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@ -47,7 +57,14 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190530182044-ad28b68e88f1/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 h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201006155630-ac719f4daadf h1:Bg47KQy0JhTHuf4sLiQwTMKwUMfSDwgSGatrxGR7nLM=
golang.org/x/sys v0.0.0-20201006155630-ac719f4daadf/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/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/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

21
main.go
View File

@ -1,16 +1,20 @@
package main package main
import ( import (
"os"
"go.jolheiser.com/gpm/cmd"
"go.jolheiser.com/gpm/config"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go.jolheiser.com/beaver" "go.jolheiser.com/beaver"
"go.jolheiser.com/gpm/cmd"
"go.jolheiser.com/gpm/modules/config"
"os"
) )
func main() { func main() {
cfg, err := config.Load()
// config loads on init if err != nil {
beaver.Fatal(err)
}
app := cli.NewApp() app := cli.NewApp()
app.Name = "gpm" app.Name = "gpm"
@ -25,11 +29,10 @@ func main() {
&cmd.Export, &cmd.Export,
&cmd.Config, &cmd.Config,
&cmd.Server, &cmd.Server,
&cmd.Search,
} }
app.Flags = cmd.Flags app.Flags = cmd.NewFlags(cfg)
app.EnableBashCompletion = true if err := app.Run(os.Args); err != nil {
err := app.Run(os.Args)
if err != nil {
beaver.Error(err) beaver.Error(err)
} }
} }

View File

@ -1,132 +0,0 @@
package config
import (
"encoding/json"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
"go.jolheiser.com/beaver"
"os"
"path"
"strings"
)
var (
configPath string
cfg *Config
Version = "develop"
// Config items
GPMURL string
Packages []Package
)
type Config struct {
GPMURL string `toml:"gpm-url" json:"gpm_url"`
Packages []Package `toml:"package" json:"packages"`
}
type Package struct {
Name string `toml:"name" json:"name"`
Import string `toml:"import" json:"import"`
}
// Load on init so that CLI contexts are correctly populated
func init() {
home, err := homedir.Dir()
if err != nil {
beaver.Fatalf("could not locate home directory: %v", err)
}
configPath = fmt.Sprintf("%s/.gpm/config.toml", home)
if _, err := os.Stat(configPath); os.IsNotExist(err) {
if err := os.MkdirAll(path.Dir(configPath), os.ModePerm); err != nil {
beaver.Fatalf("could not create gpm home: %v", err)
}
if _, err := os.Create(configPath); err != nil {
beaver.Fatalf("could not create gpm config: %v", err)
}
}
if _, err := toml.DecodeFile(configPath, &cfg); err != nil {
beaver.Fatalf("could not decode gpm config: %v", err)
}
dupe := make(map[string]bool)
for _, pkg := range cfg.Packages {
name := strings.ToLower(pkg.Name)
if ok := dupe[name]; ok {
beaver.Fatalf("duplicate package for %s", pkg.Name)
}
dupe[name] = true
}
GPMURL = cfg.GPMURL
Packages = cfg.Packages
}
func Save() error {
cfg.GPMURL = GPMURL
cfg.Packages = Packages
fi, err := os.Create(configPath)
if err != nil {
return err
}
defer fi.Close()
if err := toml.NewEncoder(fi).Encode(cfg); err != nil {
return err
}
return nil
}
func Export() (string, error) {
data, err := json.Marshal(Packages)
return string(data), err
}
func PackageMap() map[string]Package {
pkgs := make(map[string]Package)
for _, pkg := range Packages {
pkgs[pkg.Name] = pkg
}
return pkgs
}
func AddPackages(force bool, pkgs ...Package) {
for _, pkg := range pkgs {
for idx, p := range Packages {
if strings.EqualFold(p.Name, pkg.Name) {
if force {
Packages[idx] = pkg
break
}
forceQuestion := &survey.Confirm{
Message: fmt.Sprintf("Package `%s` (%s) already exists. Overwrite with `%s`?", p.Name, p.Import, p.Import),
Default: false,
}
var forceAnswer bool
if err := survey.AskOne(forceQuestion, &forceAnswer); err != nil {
beaver.Error(err)
break
}
if !forceAnswer {
beaver.Errorf("leaving package `%s` as-is", pkg.Name)
break
}
Packages[idx] = pkg
break
}
}
Packages = append(Packages, pkg)
}
}

View File

@ -1,70 +0,0 @@
package router
import (
"encoding/json"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"go.jolheiser.com/beaver"
"go.jolheiser.com/gpm/modules/config"
"net/http"
"time"
)
var cache map[string]config.Package
func Init() *chi.Mux {
r := chi.NewRouter()
r.Use(middleware.RedirectSlashes)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(30 * time.Second))
r.Get("/", handleHome)
r.Get("/export", handleExport)
r.Get("/package/{name}", handlePackage)
cache = config.PackageMap()
return r
}
func handleHome(res http.ResponseWriter, _ *http.Request) {
status, err := json.Marshal(map[string]interface{}{
"version": config.Version,
"packages": len(config.Packages),
})
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
_, _ = res.Write([]byte("{}"))
return
}
_, _ = res.Write(status)
}
func handleExport(res http.ResponseWriter, _ *http.Request) {
export, err := config.Export()
if err != nil {
beaver.Error(err)
return
}
_, _ = res.Write([]byte(export))
}
func handlePackage(res http.ResponseWriter, req *http.Request) {
name := chi.URLParam(req, "name")
if pkg, ok := cache[name]; ok {
data, err := json.Marshal(pkg)
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
_, _ = res.Write([]byte("{}"))
return
}
_, _ = res.Write(data)
return
}
res.WriteHeader(http.StatusNotFound)
_, _ = res.Write([]byte("{}"))
}

76
router/router.go 100644
View File

@ -0,0 +1,76 @@
package router
import (
"encoding/json"
"net/http"
"time"
"go.jolheiser.com/gpm/config"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"go.jolheiser.com/beaver"
)
var cache map[string]config.Package
func New(cfg *config.Config) *chi.Mux {
r := chi.NewRouter()
r.Use(middleware.RedirectSlashes)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(30 * time.Second))
r.Get("/", handleHome(cfg))
r.Get("/export", handleExport(cfg))
r.Get("/package/{name}", handlePackage)
cache = cfg.Packages.Map()
return r
}
func handleHome(cfg *config.Config) func(res http.ResponseWriter, _ *http.Request) {
return func(res http.ResponseWriter, _ *http.Request) {
status, err := json.Marshal(map[string]interface{}{
"version": config.Version,
"packages": len(cfg.Packages),
})
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
_, _ = res.Write([]byte("{}"))
return
}
_, _ = res.Write(status)
}
}
func handleExport(cfg *config.Config) func(res http.ResponseWriter, _ *http.Request) {
return func(res http.ResponseWriter, _ *http.Request) {
export, err := cfg.Export()
if err != nil {
beaver.Error(err)
return
}
_, _ = res.Write([]byte(export))
}
}
func handlePackage(res http.ResponseWriter, req *http.Request) {
name := chi.URLParam(req, "name")
if pkg, ok := cache[name]; ok {
data, err := json.Marshal(pkg)
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
_, _ = res.Write([]byte("{}"))
return
}
_, _ = res.Write(data)
return
}
res.WriteHeader(http.StatusNotFound)
_, _ = res.Write([]byte("{}"))
}