From f29fa535bed1d8a5de93246fefa9ffb81e9f0a9d Mon Sep 17 00:00:00 2001 From: Tran Hau Date: Thu, 21 Jan 2021 14:32:21 +0700 Subject: [PATCH] feat: skeleton cli app --- go.mod | 5 +++++ go.sum | 22 ++++++++++++++++++++ license.go | 1 + main.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 go.sum create mode 100644 license.go create mode 100644 main.go diff --git a/go.mod b/go.mod index cd166e2..b36db73 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,8 @@ module github.com/haunt98/license go 1.15 + +require ( + github.com/fatih/color v1.10.0 + github.com/urfave/cli/v2 v2.3.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4586904 --- /dev/null +++ b/go.sum @@ -0,0 +1,22 @@ +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/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= +github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= +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.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +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.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M= +github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/license.go b/license.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/license.go @@ -0,0 +1 @@ +package main diff --git a/main.go b/main.go new file mode 100644 index 0000000..4aa49d7 --- /dev/null +++ b/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + "os" + + "github.com/fatih/color" + "github.com/urfave/cli/v2" +) + +const ( + appName = "license" + + nameFlag = "name" +) + +var fmtErr = color.New(color.FgRed) + +func main() { + a := action{} + + app := &cli.App{ + Name: appName, + Usage: "generate LICENSE", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: nameFlag, + Aliases: []string{"n"}, + Usage: "which `LICENSE`", + }, + }, + Action: a.Run, + } + + if err := app.Run(os.Args); err != nil { + // Highlight error + fmtErr.Printf("[%s error]: ", appName) + fmt.Printf("%s\n", err.Error()) + } +} + +type action struct { + flags struct { + name string + } +} + +func (a *action) Run(c *cli.Context) error { + // Show help if there is nothing + if c.NArg() == 0 && c.NumFlags() == 0 { + return cli.ShowAppHelp(c) + } + + a.getFlags(c) + + return nil +} + +func (a *action) getFlags(c *cli.Context) { + a.flags.name = c.String(nameFlag) +}