Skip to content

Commit

Permalink
fix main.go command bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tamayika committed Nov 23, 2018
1 parent 2842151 commit 6050e8d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
11 changes: 9 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ before:
hooks:
# you may remove this if you don't use vgo
- go mod download
# you may remove this if you don't need go generate
- go generate ./...
builds:
- env:
- CGO_ENABLED=0
- GO111MODULE=on
goos:
- linux
- darwin
- windows
goarch:
- 386
- amd64
- arm
- arm64
archive:
replacements:
darwin: Darwin
Expand Down
28 changes: 16 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,9 @@ import (
"github.com/tamayika/gaq/pkg/gaq/query"
)

func main() {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("Cannot read data from stdin. %v", err)
}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", string(data), parser.ParseComments)
if err != nil {
log.Fatalf("Cannot parse source. %v", err)
}
node := gaq.MustParseNode(f)
var version = "dev"

func main() {
rootCmd := &cobra.Command{
Use: "gaq <Query>",
Short: "gaq is the cli tool to query ast node. STDIN needed as go code.",
Expand All @@ -34,8 +25,20 @@ Typical usage is
cat <go file path> | gaq <Query>
Please see details at https://github.com/tamayika/gaq`,
Args: cobra.ExactArgs(1),
Args: cobra.ExactArgs(1),
Version: version,
Run: func(cmd *cobra.Command, args []string) {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("Cannot read data from stdin. %v", err)
}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", string(data), parser.ParseComments)
if err != nil {
log.Fatalf("Cannot parse source. %v", err)
}
node := gaq.MustParseNode(f)

q := query.MustParse(args[0])
nodes := node.QuerySelectorAll(q)
for _, node := range nodes {
Expand All @@ -45,6 +48,7 @@ Please see details at https://github.com/tamayika/gaq`,
}
},
}
rootCmd.SetVersionTemplate(`{{printf "%s" .Version}}`)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down

0 comments on commit 6050e8d

Please sign in to comment.