From da5e6120c449402e49d15f305d6db919785d4bf2 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Tue, 22 Oct 2019 14:50:48 -0400 Subject: [PATCH] sometimes it happens --- cmd/pkger/cmds/pack.go | 2 +- cmd/pkger/cmds/parse.go | 63 +++++++++++++++++++++++++++++++++++++++++ cmd/pkger/cmds/path.go | 1 - parser/open.go | 10 +++++++ parser/walk.go | 10 +++++++ 5 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 cmd/pkger/cmds/parse.go diff --git a/cmd/pkger/cmds/pack.go b/cmd/pkger/cmds/pack.go index 67e9a15..ca5ae0b 100644 --- a/cmd/pkger/cmds/pack.go +++ b/cmd/pkger/cmds/pack.go @@ -90,7 +90,7 @@ func New() (*packCmd, error) { c := &packCmd{} c.subs = []command{ - &serveCmd{}, &statCmd{}, &infoCmd{}, &pathCmd{}, + &serveCmd{}, &statCmd{}, &infoCmd{}, &pathCmd{}, &parseCmd{}, } sort.Slice(c.subs, func(a, b int) bool { return c.subs[a].Name() <= c.subs[b].Name() diff --git a/cmd/pkger/cmds/parse.go b/cmd/pkger/cmds/parse.go new file mode 100644 index 0000000..9ff43af --- /dev/null +++ b/cmd/pkger/cmds/parse.go @@ -0,0 +1,63 @@ +package cmds + +import ( + "encoding/json" + "flag" + "os" + + "github.com/markbates/pkger/here" + "github.com/markbates/pkger/parser" +) + +type parseCmd struct { + *flag.FlagSet + json bool + help bool +} + +func (s *parseCmd) Name() string { + return s.Flags().Name() +} + +func (c *parseCmd) Flags() *flag.FlagSet { + if c.FlagSet == nil { + c.FlagSet = flag.NewFlagSet("parse", flag.ExitOnError) + // c.BoolVar(&c.json, "json", false, "outputs as json") + c.BoolVar(&c.help, "h", false, "prints help information") + } + return c.FlagSet +} + +func (c *parseCmd) Exec(args []string) error { + + c.Parse(args) + + if c.help { + c.Usage() + return nil + } + + args = c.Args() + if len(args) == 0 { + args = append(args, ".") + } + + m := map[string]parser.Decls{} + + for _, a := range args { + info, err := here.Package(a) + if err != nil { + return err + } + + decls, err := parser.Parse(info) + if err != nil { + return err + } + m[a] = decls + } + + enc := json.NewEncoder(os.Stdout) + enc.SetIndent("", " ") + return enc.Encode(m) +} diff --git a/cmd/pkger/cmds/path.go b/cmd/pkger/cmds/path.go index 702102e..e185fff 100644 --- a/cmd/pkger/cmds/path.go +++ b/cmd/pkger/cmds/path.go @@ -55,5 +55,4 @@ func (c *pathCmd) Exec(args []string) error { enc := json.NewEncoder(os.Stdout) enc.SetIndent("", " ") return enc.Encode(paths) - return nil } diff --git a/parser/open.go b/parser/open.go index 53b20cf..90cab67 100644 --- a/parser/open.go +++ b/parser/open.go @@ -1,6 +1,7 @@ package parser import ( + "encoding/json" "go/token" "os" "path/filepath" @@ -17,6 +18,15 @@ type OpenDecl struct { value string } +func (d OpenDecl) MarshalJSON() ([]byte, error) { + return json.Marshal(map[string]interface{}{ + "type": "pkger.Open", + "file": d.file, + "pos": d.pos, + "value": d.value, + }) +} + func (d OpenDecl) File() (*File, error) { if d.file == nil { return nil, os.ErrNotExist diff --git a/parser/walk.go b/parser/walk.go index 8ee84e6..84614a7 100644 --- a/parser/walk.go +++ b/parser/walk.go @@ -1,6 +1,7 @@ package parser import ( + "encoding/json" "go/token" "os" "path/filepath" @@ -18,6 +19,15 @@ type WalkDecl struct { value string } +func (d WalkDecl) MarshalJSON() ([]byte, error) { + return json.Marshal(map[string]interface{}{ + "type": "pkger.Walk", + "file": d.file, + "pos": d.pos, + "value": d.value, + }) +} + func (d WalkDecl) File() (*File, error) { if d.file == nil { return nil, os.ErrNotExist