From 87db8a7240bafcd2b4e5848cf6af670186d824b9 Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Wed, 25 Nov 2020 01:04:23 +0100 Subject: [PATCH] refactor: cline cli beta migration --- Makefile | 22 ++++++++-- cmd/cmd.go | 107 ++++++++++++++++++++----------------------------- cmd/version.go | 54 ++----------------------- go.mod | 2 +- go.sum | 15 +------ 5 files changed, 69 insertions(+), 131 deletions(-) diff --git a/Makefile b/Makefile index 6067c73..ee20798 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ PKG_TARGET=linux PKG_BIN=./bin/enve PKG_TAG=$(shell git tag -l --contains HEAD) +BUILD_TIME ?= $(shell date -u '+%Y-%m-%dT%H:%m:%S') export GO111MODULE := on # enable consistent Go 1.12/1.13 GOPROXY behavior. @@ -35,8 +36,12 @@ dev.release: ########### Utility tasks ############# ####################################### -test: lint - @go test -v -timeout 30s -race -coverprofile=coverage.txt -covermode=atomic ./... +test: + @go version + @golint -set_exit_status ./... + @go vet ./... + @go test $$(go list ./...) \ + -v -timeout 30s -race -coverprofile=coverage.txt -covermode=atomic .PHONY: test coverage: @@ -57,10 +62,19 @@ lint: @./bin/misspell -error **/* .PHONY: lint -dev.release: +dev_release: @go version @goreleaser release --snapshot --rm-dist -.PHONY: dev.release +.PHONY: dev_release + +build: + @go version + @go build -v \ + -ldflags "-s -w \ + -X 'github.com/joseluisq/enve/cmd.versionNumber=0.0.0' \ + -X 'github.com/joseluisq/enve/cmd.buildTime=$(BUILD_TIME)'" \ + -a -o bin/enve main.go +.PHONY: build ####################################### diff --git a/cmd/cmd.go b/cmd/cmd.go index 72d1aee..be204d6 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -9,7 +9,8 @@ import ( "strings" "github.com/joho/godotenv" - "github.com/urfave/cli/v2" + + cli "github.com/joseluisq/cline" ) // Environment defines JSON/XML data structure @@ -22,31 +23,28 @@ type Environment struct { // Execute adds all child commands to the root command and sets flags appropriately. func Execute() { - app := &cli.App{ - Name: "enve", - Usage: "run a program in a modified environment using .env files", - Description: "Set all environment variables of one .env file and run a `command`.", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "file", - Aliases: []string{"f"}, - Value: ".env", - Usage: "load environment variables from a file path (optional)", - }, - &cli.StringFlag{ - Name: "output", - Aliases: []string{"o"}, - Value: "text", - Usage: "output environment variables using text, json or xml format", - }, - VersionFlag(), + app := cli.New() + app.Name = "enve" + app.Summary = "run a program in a modified environment using .env files" + app.Version = versionNumber + app.BuildTime = buildTime + app.Flags = []cli.Flag{ + cli.FlagString{ + Name: "file", + Aliases: []string{"f"}, + Value: ".env", + Summary: "load environment variables from a file path (optional)", + }, + cli.FlagString{ + Name: "output", + Aliases: []string{"o"}, + Value: "text", + Summary: "output environment variables using text, json or xml format", }, - Action: onCommand, } + app.Handler = onCommand - err := app.Run(os.Args) - - if err != nil { + if err := app.Run(os.Args); err != nil { fmt.Println(err) os.Exit(1) } @@ -62,75 +60,60 @@ func fileExists(filename string) bool { return !info.IsDir() } -func onCommand(ctx *cli.Context) error { - // 1. Version flag - v := ctx.Bool("version") - - if v { - return VersionAction(ctx) - } - +func onCommand(ctx *cli.AppContext) error { // 2. File flag - f := ctx.String("file") - + f := ctx.Flags.String("file") if f != "" { if exist := fileExists(f); exist { err := godotenv.Load(f) - if err != nil { return err } } } - // 3. Output flag - output := ctx.String("output") - - if ctx.NArg() == 0 { - switch output { - case "json": - return jsonPrintAction(ctx) - case "xml": - return xmlPrintAction(ctx) - default: - return textPrintAction(ctx) - } - } + tArgs := ctx.TailArgs // 4. Execute the given command - if ctx.NArg() > 0 { - return execCmdAction(ctx) + if len(tArgs) > 0 { + return execCmdAction(tArgs) + } + + output := ctx.Flags.String("output") + + // 3. Output flag + switch output { + case "json": + return jsonPrintAction() + case "xml": + return xmlPrintAction() + case "text": + return textPrintAction() } return nil } // execCmdAction executes a command along with its env variables -func execCmdAction(ctx *cli.Context) (err error) { - args := ctx.Args().Slice() - cmdIn := args[0] - +func execCmdAction(tArgs []string) (err error) { + cmdIn := tArgs[0] _, err = exec.LookPath(cmdIn) - if err != nil { return fmt.Errorf("executable \"%s\" was not found\n%s", cmdIn, err) } - cmd := exec.Command(cmdIn, args[1:]...) - + cmd := exec.Command(cmdIn, tArgs[1:]...) cmd.Stderr = os.Stderr cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout - return cmd.Run() } // textPrintAction prints all environment variables in plain text -func textPrintAction(ctx *cli.Context) (err error) { +func textPrintAction() (err error) { for _, s := range os.Environ() { fmt.Println(s) } - return nil } @@ -164,7 +147,7 @@ func parseJSONFromEnviron() (jsonu Environment, err error) { } // jsonPrintAction prints all environment variables in JSON format -func jsonPrintAction(ctx *cli.Context) error { +func jsonPrintAction() error { jsonu, err := parseJSONFromEnviron() if err != nil { return err @@ -176,12 +159,11 @@ func jsonPrintAction(ctx *cli.Context) error { } fmt.Println(string(jsonb)) - return nil } // xmlPrintAction prints all environment variables in XML format -func xmlPrintAction(ctx *cli.Context) error { +func xmlPrintAction() error { jsonu, err := parseJSONFromEnviron() if err != nil { return err @@ -193,6 +175,5 @@ func xmlPrintAction(ctx *cli.Context) error { } fmt.Println("" + string(xmlb)) - return nil } diff --git a/cmd/version.go b/cmd/version.go index 08a5156..f19d29e 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,13 +1,7 @@ package cmd import ( - "fmt" - "io" - "os" - "runtime" - "text/template" - - "github.com/urfave/cli/v2" + cli "github.com/joseluisq/cline" ) // application version values @@ -16,51 +10,11 @@ var ( buildTime string ) -var versionTemplate = `Version: {{.Version}} -Go version: {{.GoVersion}} -Built: {{.BuildTime}} -OS/Arch: {{.Os}}/{{.Arch}}` - // VersionFlag builds a new Version flag -func VersionFlag() *cli.BoolFlag { - return &cli.BoolFlag{ +func VersionFlag() *cli.FlagBool { + return &cli.FlagBool{ Name: "version", Aliases: []string{"v"}, - Usage: "shows the current version", + Summary: "shows the current version", } } - -// VersionAction defines the version action function -func VersionAction(c *cli.Context) (err error) { - if err = getVersionTemplate(os.Stdout); err != nil { - return err - } - - fmt.Print("\n") - return nil -} - -// getVersionTemplate write the version template -func getVersionTemplate(wr io.Writer) error { - tmpl, err := template.New("").Parse(versionTemplate) - - if err != nil { - return err - } - - v := struct { - Version string - GoVersion string - BuildTime string - Os string - Arch string - }{ - Version: versionNumber, - GoVersion: runtime.Version(), - BuildTime: buildTime, - Os: runtime.GOOS, - Arch: runtime.GOARCH, - } - - return tmpl.Execute(wr, v) -} diff --git a/go.mod b/go.mod index e334d78..81e8257 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.15 require ( github.com/joho/godotenv v1.3.0 - github.com/urfave/cli/v2 v2.2.0 + github.com/joseluisq/cline v0.1.0-beta.5 ) diff --git a/go.sum b/go.sum index 14d2547..3d8caaf 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,4 @@ -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -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/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= -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/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4= -github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= -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= +github.com/joseluisq/cline v0.1.0-beta.5 h1:D0+8gsN3gFeQ4tRlxi7haVcN02A6qqFgrpUfmkZlAhQ= +github.com/joseluisq/cline v0.1.0-beta.5/go.mod h1:OHx3HRPOMpaxrkgR6pNdPCdcOh55WhP2nT7OiSFLLkA=