Browse Source

Initial commit

Signed-off-by: jolheiser <john.olheiser@gmail.com>
pull/1/head
jolheiser 2 years ago
commit
023c95c5bb
Signed by: jolheiser GPG Key ID: B853ADA5DA7BBF7A
  1. 5
      .gitignore
  2. 86
      Makefile
  3. 88
      cmd/add.go
  4. 15
      cmd/cmd.go
  5. 25
      cmd/export.go
  6. 99
      cmd/get.go
  7. 55
      cmd/remove.go
  8. 11
      go.mod
  9. 49
      go.sum
  10. 32
      main.go
  11. 91
      modules/config/config.go

5
.gitignore

@ -0,0 +1,5 @@
# GoLand
.idea/
# gpm
/gpm*

86
Makefile

@ -0,0 +1,86 @@
DIST := dist
GO ?= go
SHASUM ?= shasum -a 256
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 "main.Version=$(LONG_VERSION)"
.PHONY: build
build:
$(GO) build -ldflags '-s -w $(LDFLAGS)'
.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
fmt:
$(GO) fmt ./...
.PHONY: test
test:
$(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;

88
cmd/add.go

@ -0,0 +1,88 @@
package cmd
import (
"fmt"
"gitea.com/gpm/cli/modules/config"
"gitea.com/jolheiser/beaver"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"strings"
)
var Add = cli.Command{
Name: "add",
Usage: "Add a gpm package",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "Overwrite if the package already exists",
},
},
Action: doAdd,
}
func doAdd(ctx *cli.Context) error {
goGetQuestion := &survey.Input{
Message: "Package go-get URL",
}
var goGetAnswer string
if err := survey.AskOne(goGetQuestion, &goGetAnswer); err != nil {
return err
}
goGet := strings.Split(goGetAnswer, "/")
defaultName := goGet[len(goGet)-1]
// Check if go-get is actually pointing to a versioned module
if strings.HasPrefix(defaultName, "v") {
defaultName = goGet[len(goGet)-2]
}
nameQuestion := &survey.Input{
Message: "Package name for gpm",
Default: defaultName,
}
var nameAnswer string
if err := survey.AskOne(nameQuestion, &nameAnswer); err != nil {
return err
}
pkg := config.Package{
Name: nameAnswer,
URL: goGetAnswer,
}
if !ctx.Bool("force") {
for idx, p := range config.Packages {
if strings.EqualFold(p.Name, nameAnswer) {
forceQuestion := &survey.Confirm{
Message: "This package already exists, do you want to overwrite?",
Default: false,
}
var force bool
if err := survey.AskOne(forceQuestion, &force); err != nil {
return err
}
if !force {
return fmt.Errorf("leaving package `%s` as-is", nameAnswer)
}
config.Packages[idx] = pkg
break
}
}
} else {
config.Packages = append(config.Packages, pkg)
}
if err := config.Save(); err != nil {
return err
}
beaver.Infof("Added `%s` to local gpm.", nameAnswer)
return nil
}

15
cmd/cmd.go

@ -0,0 +1,15 @@
package cmd
import (
"gitea.com/gpm/cli/modules/config"
"github.com/urfave/cli/v2"
)
var Flags = []cli.Flag{
&cli.StringFlag{
Name: "url",
Aliases: []string{"u"},
Usage: "gpm URL to use",
Value: config.GPMURL,
},
}

25
cmd/export.go

@ -0,0 +1,25 @@
package cmd
import (
"encoding/json"
"fmt"
"gitea.com/gpm/cli/modules/config"
"github.com/urfave/cli/v2"
)
var Export = cli.Command{
Name: "export",
Usage: "Export local gpm packages",
Action: doExport,
}
func doExport(ctx *cli.Context) error {
data, err := json.Marshal(config.Packages)
if err != nil {
return err
}
fmt.Println(string(data))
return nil
}

99
cmd/get.go

@ -0,0 +1,99 @@
package cmd
import (
"encoding/json"
"fmt"
"gitea.com/gpm/cli/modules/config"
"gitea.com/jolheiser/beaver"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
)
var Get = cli.Command{
Name: "get",
Usage: "Get gpm package(s)",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "ignore-local",
Usage: "Ignore local packages",
},
},
Action: doGet,
}
func doGet(ctx *cli.Context) error {
pkgs := ctx.Args().Slice()
if len(pkgs) == 0 {
pkgsQuestion := &survey.Multiline{
Message: "Enter packages to get, one for each line",
}
var pkgsAnswer string
if err := survey.AskOne(pkgsQuestion, &pkgsAnswer); err != nil {
return err
}
pkgs = strings.Split(pkgsAnswer, "\n")
}
local := config.PackageMap()
for _, pkg := range pkgs {
var url string
if u, ok := local[pkg]; ok {
url = u
} else {
u, err := queryServer(ctx.String("url"), pkg)
if err != nil {
beaver.Error(err)
continue
}
url = u
}
if err := goGet(url); err != nil {
beaver.Error(err)
}
}
return nil
}
type response struct {
URL string `json:"url"`
}
func queryServer(server, name string) (string, error) {
endpoint := fmt.Sprintf("%s/%s", server, name)
resp, err := http.Get(endpoint)
if err != nil {
return "", nil
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("could not find server package for `%s`", name)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
defer resp.Body.Close()
var data response
if err := json.Unmarshal(body, &data); err != nil {
return "", err
}
return data.URL, nil
}
func goGet(url string) error {
cmd := exec.Command("go", "get", url)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

55
cmd/remove.go

@ -0,0 +1,55 @@
package cmd
import (
"fmt"
"gitea.com/gpm/cli/modules/config"
"gitea.com/jolheiser/beaver"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"strings"
)
var Remove = cli.Command{
Name: "remove",
Usage: "Remove a gpm package",
Action: doRemove,
}
func doRemove(ctx *cli.Context) error {
pkgQuestion := &survey.Input{
Message: "Package name",
}
var pkgAnswer string
if err := survey.AskOne(pkgQuestion, &pkgAnswer); err != nil {
return err
}
for idx, p := range config.Packages {
if strings.EqualFold(p.Name, pkgAnswer) {
confirm := &survey.Confirm{
Message: fmt.Sprintf("Are you sure you want to remove %s (%s) ?", p.Name, p.URL),
Default: false,
}
var answer bool
if err := survey.AskOne(confirm, &answer); err != nil {
return err
}
if answer {
config.Packages = append(config.Packages[:idx], config.Packages[idx+1:]...)
if err := config.Save(); err != nil {
return err
}
beaver.Infof("Removed `%s` from local gpm.", p.Name)
break
}
beaver.Infof("Did not remove `%s` from local gpm.", p.Name)
break
}
}
return nil
}

11
go.mod

@ -0,0 +1,11 @@
module gitea.com/gpm/cli
go 1.13
require (
gitea.com/jolheiser/beaver v1.0.0
github.com/AlecAivazis/survey/v2 v2.0.5
github.com/BurntSushi/toml v0.3.1
github.com/mitchellh/go-homedir v1.1.0
github.com/urfave/cli/v2 v2.1.1
)

49
go.sum

@ -0,0 +1,49 @@
gitea.com/jolheiser/beaver v1.0.0 h1:IfNMhp7+DUaM0kaNwho4RWfuebCsa8A/kxtZBngFjHk=
gitea.com/jolheiser/beaver v1.0.0/go.mod h1:2mUGl6ZGKY/Y9u36iR4bqOPrHhr4C22cxkR8ei2G06I=
github.com/AlecAivazis/survey/v2 v2.0.5 h1:xpZp+Q55wi5C7Iaze+40onHnEkex1jSc34CltJjOoPM=
github.com/AlecAivazis/survey/v2 v2.0.5/go.mod h1:WYBhg6f0y/fNYUuesWQc0PKbJcEliGcYHB9sNT3Bg74=
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/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/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/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=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
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/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-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
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/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
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/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/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.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k=
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
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/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
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-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 h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
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=

32
main.go

@ -0,0 +1,32 @@
package main
import (
"gitea.com/gpm/cli/cmd"
"gitea.com/jolheiser/beaver"
"github.com/urfave/cli/v2"
"os"
)
var Version = "develop"
func main() {
// config loads on init
app := cli.NewApp()
app.Name = "gpm"
app.Usage = "Go Package Manager"
app.Version = Version
app.Commands = []*cli.Command{
&cmd.Add,
&cmd.Remove,
&cmd.Get,
&cmd.Export,
}
app.Flags = cmd.Flags
app.EnableBashCompletion = true
err := app.Run(os.Args)
if err != nil {
beaver.Error(err)
}
}

91
modules/config/config.go

@ -0,0 +1,91 @@
package config
import (
"fmt"
"gitea.com/jolheiser/beaver"
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
"os"
"path"
"strings"
)
var (
configPath string
cfg *config
// Config items
GPMURL string
Packages []Package
)
type config struct {
GPMURL string `toml:"gpm-url"`
Packages []Package `toml:"package"`
}
type Package struct {
Name string `toml:"name"`
URL string `toml:"url"`
}
// 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 Sip home: %v", err)
}
if _, err := os.Create(configPath); err != nil {
beaver.Fatalf("could not create Sip config: %v", err)
}
}
if _, err := toml.DecodeFile(configPath, &cfg); err != nil {
beaver.Fatalf("could not decode Sip 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 PackageMap() map[string]string {
pkgs := make(map[string]string)
for _, pkg := range Packages {
pkgs[pkg.Name] = pkg.URL
}
return pkgs
}
Loading…
Cancel
Save