Skip to content

Commit

Permalink
refact - issue #8 - use cobra module
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicola Strappazzon C committed Feb 15, 2024
1 parent bd3926b commit 799e589
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 158 deletions.
52 changes: 0 additions & 52 deletions command/help/main.go

This file was deleted.

47 changes: 0 additions & 47 deletions command/main.go

This file was deleted.

3 changes: 0 additions & 3 deletions command/version/main.go

This file was deleted.

60 changes: 7 additions & 53 deletions flags/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package flags

import (
"flag"
"os"

"skeely/command/help"
"skeely/common"
)

Expand All @@ -14,10 +12,8 @@ type Flags struct {
GitHubPullRequest int
GitHubRepository string
GitHubToken string
Help bool
Ignore string
Path string
Version bool
}

var instance *Flags
Expand All @@ -30,53 +26,11 @@ func GetInstance() *Flags {
}

func (f *Flags) Load() {
fFiles := flag.String("files", "", "")
fGitHubComment := flag.Bool("github-comment", false, "")
fGitHubPullRequest := flag.Int("github-pull-request", 0, "")
fGitHubRepository := flag.String("github-repository", "", "")
fGitHubToken := flag.String("github-token", "", "")
fHelp := flag.Bool("help", false, "")
fIgnore := flag.String("ignore", "", "")
flag.Usage = func() { help.Show(1) }
fPath := flag.String("path", "", "")
fVersion := flag.Bool("version", false, "")
flag.Parse()

f.Files = *fFiles
f.GitHubComment = *fGitHubComment
f.GitHubPullRequest = *fGitHubPullRequest
f.GitHubRepository = *fGitHubRepository
f.GitHubToken = *fGitHubToken
f.Help = *fHelp
f.Ignore = *fIgnore
f.Path = *fPath
f.Version = *fVersion

if len(f.Files) == 0 && len(os.Getenv("INPUT_FILES")) > 0 {
f.Files = os.Getenv("INPUT_FILES")
}

if len(f.Ignore) == 0 && len(os.Getenv("INPUT_IGNORE")) > 0 {
f.Ignore = os.Getenv("INPUT_IGNORE")
}

if len(f.Path) == 0 && len(os.Getenv("INPUT_PATH")) > 0 {
f.Path = os.Getenv("")
}

if f.GitHubComment == false && common.StringToBool(os.Getenv("INPUT_COMMENT")) == true {
f.GitHubComment = true
}

if f.GitHubPullRequest == 0 && common.StringToInt(os.Getenv("INPUT_PULLREQUEST")) > 0 {
f.GitHubPullRequest = common.StringToInt(os.Getenv("INPUT_PULLREQUEST"))
}

if len(f.GitHubRepository) == 0 && len(os.Getenv("INPUT_REPOSITORY")) > 0 {
f.GitHubRepository = os.Getenv("INPUT_REPOSITORY")
}

if len(f.GitHubToken) == 0 && len(os.Getenv("INPUT_TOKEN")) > 0 {
f.GitHubToken = os.Getenv("INPUT_TOKEN")
}
f.Files = os.Getenv("INPUT_FILES")
f.GitHubComment = common.StringToBool(os.Getenv("INPUT_COMMENT"))
f.GitHubPullRequest = common.StringToInt(os.Getenv("INPUT_PULLREQUEST"))
f.GitHubRepository = os.Getenv("INPUT_REPOSITORY")
f.GitHubToken = os.Getenv("INPUT_TOKEN")
f.Ignore = os.Getenv("INPUT_IGNORE")
f.Path = os.Getenv("INPUT_PATH")
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ go 1.21

require gopkg.in/yaml.v3 v3.0.1

require gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
66 changes: 64 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,71 @@
package main

import (
"skeely/command"
"fmt"
"os"

"skeely/common/github"
"skeely/common/terminal"
"skeely/config"
"skeely/flags"
"skeely/linter"
"skeely/version"

"github.com/spf13/cobra"
)

func main() {
command.Run()
var f = flags.GetInstance()
var rootCmd = &cobra.Command{
Use: "skeely [COMMANDS] [OPTIONS]",
Long: `skeely is a schema linter for MySQL, this tool help to identifying
some common and uncommon mistakes on data model.
For more help, plese visit: https://github.com/debeando/skeely`,
Example: `
# Lint directory
$ skeely --path=assets/examples/
# Lint specific file
$ skeely --files=assets/examples/case01.sql
# Lint specific file and ignore codes
$ skeely --files=assets/examples/case01.sql --ignore=103,104,305,406
# Lint and push summary as comment into GitHub Pull Request.
$ skeely --path=assets/examples/case01.sql \
--github-comment \
--github-token=${{github.token}} \
--github-repository=$GITHUB_REPOSITORY \
--github-pull-request=${{github.event.pull_request.number}}
`,
Run: func(cmd *cobra.Command, args []string) {
gh := github.GitHub{}
c := config.GetInstance()

if err := c.Load(); err != nil {
fmt.Println(err)
os.Exit(2)
}

msgPlugins := linter.Run()

gh.Comment(msgPlugins)
gh.Push()

terminal.Print(msgPlugins)
},
}

f.Load()

rootCmd.Flags().BoolVarP(&f.GitHubComment, "github-comment", "", false, "Send summary as comment into GitHub.")
rootCmd.Flags().IntVar(&f.GitHubPullRequest, "github-pull-request", 0, "Pull request number.")
rootCmd.Flags().StringVar(&f.Files, "files", "", "List of files to lint, separated by space.")
rootCmd.Flags().StringVar(&f.GitHubRepository, "github-repository", "", "Repository path on github.")
rootCmd.Flags().StringVar(&f.GitHubToken, "github-token", "", "Token to auth in github.")
rootCmd.Flags().StringVar(&f.Ignore, "ignore", "", "List of error codes separated by comma to ignore.")
rootCmd.Flags().StringVar(&f.Path, "path", "", "Path of the directory to start to find *.sql to lint.")
rootCmd.AddCommand(version.NewCommand())
rootCmd.Execute()
}
23 changes: 23 additions & 0 deletions version/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package version

import (
"fmt"

"github.com/spf13/cobra"
)

// Version is a const to have the latest version number for this code.
const VERSION string = "0.0.0-beta.3"

func NewCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "version",
Short: "Print version numbers",
Long: `All software has versions. This is skale`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(VERSION)
},
}

return cmd
}

0 comments on commit 799e589

Please sign in to comment.