feat: flags from gofmt

main
sudo pacman -Syu 2022-11-24 23:11:15 +07:00
parent 878278b0d2
commit 90e43ed163
No known key found for this signature in database
GPG Key ID: D6CB5C6C567C47B0
5 changed files with 153 additions and 0 deletions

7
cmd/gofimports/main.go Normal file
View File

@ -0,0 +1,7 @@
package main
import "github.com/haunt98/gofimports/internal/cli"
func main() {
cli.NewApp().Run()
}

15
go.mod
View File

@ -1,3 +1,18 @@
module github.com/haunt98/gofimports module github.com/haunt98/gofimports
go 1.19 go 1.19
require (
github.com/make-go-great/color-go v0.4.1
github.com/urfave/cli/v2 v2.23.5
)
require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
)

21
go.sum Normal file
View File

@ -0,0 +1,21 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/make-go-great/color-go v0.4.1 h1:4HIat5r1oQneu5BSDUjHKN/9x4Ay2qEEkswdOgk/VRY=
github.com/make-go-great/color-go v0.4.1/go.mod h1:G5G8IJ3PUo+vSQ+UvnfaswF8O/piVIhFJKv1mQjBGpI=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.23.5 h1:xbrU7tAYviSpqeR3X4nEFWUdB/uDZ6DE+HxmRU7Xtyw=
github.com/urfave/cli/v2 v2.23.5/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
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-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

38
internal/cli/action.go Normal file
View File

@ -0,0 +1,38 @@
package cli
import (
"fmt"
"github.com/urfave/cli/v2"
)
type action struct {
flags struct {
list bool
write bool
diff bool
}
}
func (a *action) RunHelp(c *cli.Context) error {
return cli.ShowAppHelp(c)
}
func (a *action) getFlags(c *cli.Context) {
a.flags.list = c.Bool(flagListName)
a.flags.write = c.Bool(flagWriteName)
a.flags.diff = c.Bool(flagDiffName)
}
func (a *action) Run(c *cli.Context) error {
a.getFlags(c)
if !a.flags.write &&
!a.flags.diff {
a.RunHelp(c)
fmt.Println("Error: -w or -d must required one")
return nil
}
return nil
}

72
internal/cli/app.go Normal file
View File

@ -0,0 +1,72 @@
package cli
import (
"os"
"github.com/make-go-great/color-go"
"github.com/urfave/cli/v2"
)
const (
name = "gofimports"
usage = "goimports with my opinionated preferences"
// Inspiration from gofmt flags
flagListName = "list"
flagListUsage = "list files will be changed"
flagWriteName = "write"
flagWriteUsage = "actually write changes to (source) files"
// If both write and diff is exist
// Choose diff
flagDiffName = "diff"
flagDiffUsage = "show diff instead of writing changes"
)
var (
flagListAliases = []string{"l"}
flagWriteAliases = []string{"w"}
flagDiffAliases = []string{"d"}
)
type App struct {
cliApp *cli.App
}
func NewApp() *App {
a := &action{}
cliApp := &cli.App{
Name: name,
Usage: usage,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: flagListName,
Usage: flagListUsage,
Aliases: flagListAliases,
},
&cli.BoolFlag{
Name: flagWriteName,
Usage: flagWriteUsage,
Aliases: flagWriteAliases,
},
&cli.BoolFlag{
Name: flagDiffName,
Usage: flagDiffUsage,
Aliases: flagDiffAliases,
},
},
Action: a.Run,
}
return &App{
cliApp: cliApp,
}
}
func (a *App) Run() {
if err := a.cliApp.Run(os.Args); err != nil {
color.PrintAppError(name, err.Error())
}
}