Fix binary and add command

Signed-off-by: jolheiser <john.olheiser@gmail.com>
pull/1/head
jolheiser 2020-02-18 23:39:54 -06:00
parent 023c95c5bb
commit 62ad64516c
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
9 changed files with 63 additions and 20 deletions

View File

@ -2,7 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"gitea.com/gpm/cli/modules/config" "gitea.com/gpm/gpm/modules/config"
"gitea.com/jolheiser/beaver" "gitea.com/jolheiser/beaver"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -25,7 +25,7 @@ var Add = cli.Command{
func doAdd(ctx *cli.Context) error { func doAdd(ctx *cli.Context) error {
goGetQuestion := &survey.Input{ goGetQuestion := &survey.Input{
Message: "Package go-get URL", Message: "Package go-get import",
} }
var goGetAnswer string var goGetAnswer string
@ -51,8 +51,8 @@ func doAdd(ctx *cli.Context) error {
} }
pkg := config.Package{ pkg := config.Package{
Name: nameAnswer, Name: nameAnswer,
URL: goGetAnswer, Import: goGetAnswer,
} }
if !ctx.Bool("force") { if !ctx.Bool("force") {
for idx, p := range config.Packages { for idx, p := range config.Packages {
@ -75,6 +75,7 @@ func doAdd(ctx *cli.Context) error {
break break
} }
} }
config.Packages = append(config.Packages, pkg)
} else { } else {
config.Packages = append(config.Packages, pkg) config.Packages = append(config.Packages, pkg)
} }

View File

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

34
cmd/config.go 100644
View File

@ -0,0 +1,34 @@
package cmd
import (
"gitea.com/gpm/gpm/modules/config"
"gitea.com/jolheiser/beaver"
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
)
var Config = cli.Command{
Name: "config",
Aliases: []string{"cfg"},
Action: doConfig,
}
func doConfig(ctx *cli.Context) error {
urlQuestion := &survey.Input{
Message: "gpm URL",
Default: "gpm.jolheiser.com",
}
var urlAnswer string
if err := survey.AskOne(urlQuestion, &urlAnswer); err != nil {
return err
}
config.GPMURL = urlAnswer
if err := config.Save(); err != nil {
return err
}
beaver.Info("gpm URL saved!")
return nil
}

View File

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

View File

@ -3,7 +3,7 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gitea.com/gpm/cli/modules/config" "gitea.com/gpm/gpm/modules/config"
"gitea.com/jolheiser/beaver" "gitea.com/jolheiser/beaver"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -54,6 +54,8 @@ func doGet(ctx *cli.Context) error {
} }
url = u url = u
} }
beaver.Infof("getting `%s`...", pkg)
if err := goGet(url); err != nil { if err := goGet(url); err != nil {
beaver.Error(err) beaver.Error(err)
} }

View File

@ -2,7 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"gitea.com/gpm/cli/modules/config" "gitea.com/gpm/gpm/modules/config"
"gitea.com/jolheiser/beaver" "gitea.com/jolheiser/beaver"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -28,7 +28,7 @@ func doRemove(ctx *cli.Context) error {
for idx, p := range config.Packages { for idx, p := range config.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.URL), Message: fmt.Sprintf("Are you sure you want to remove %s (%s) ?", p.Name, p.Import),
Default: false, Default: false,
} }
var answer bool var answer bool

2
go.mod
View File

@ -1,4 +1,4 @@
module gitea.com/gpm/cli module gitea.com/gpm/gpm
go 1.13 go 1.13

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"gitea.com/gpm/cli/cmd" "gitea.com/gpm/gpm/cmd"
"gitea.com/jolheiser/beaver" "gitea.com/jolheiser/beaver"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"os" "os"
@ -22,6 +22,7 @@ func main() {
&cmd.Remove, &cmd.Remove,
&cmd.Get, &cmd.Get,
&cmd.Export, &cmd.Export,
&cmd.Config,
} }
app.Flags = cmd.Flags app.Flags = cmd.Flags
app.EnableBashCompletion = true app.EnableBashCompletion = true

View File

@ -1,6 +1,7 @@
package config package config
import ( import (
"encoding/json"
"fmt" "fmt"
"gitea.com/jolheiser/beaver" "gitea.com/jolheiser/beaver"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
@ -21,13 +22,13 @@ var (
) )
type config struct { type config struct {
GPMURL string `toml:"gpm-url"` GPMURL string `toml:"gpm-url" json:"gpm_url"`
Packages []Package `toml:"package"` Packages []Package `toml:"package" json:"packages"`
} }
type Package struct { type Package struct {
Name string `toml:"name"` Name string `toml:"name" json:"name"`
URL string `toml:"url"` Import string `toml:"import" json:"import"`
} }
// Load on init so that CLI contexts are correctly populated // Load on init so that CLI contexts are correctly populated
@ -82,10 +83,15 @@ func Save() error {
return nil return nil
} }
func Export() (string, error) {
data, err := json.Marshal(cfg)
return string(data), err
}
func PackageMap() map[string]string { func PackageMap() map[string]string {
pkgs := make(map[string]string) pkgs := make(map[string]string)
for _, pkg := range Packages { for _, pkg := range Packages {
pkgs[pkg.Name] = pkg.URL pkgs[pkg.Name] = pkg.Import
} }
return pkgs return pkgs
} }