sometimes it happens

This commit is contained in:
Mark Bates 2019-10-22 14:50:48 -04:00
parent 80292eb459
commit da5e6120c4
5 changed files with 84 additions and 2 deletions

View File

@ -90,7 +90,7 @@ func New() (*packCmd, error) {
c := &packCmd{} c := &packCmd{}
c.subs = []command{ c.subs = []command{
&serveCmd{}, &statCmd{}, &infoCmd{}, &pathCmd{}, &serveCmd{}, &statCmd{}, &infoCmd{}, &pathCmd{}, &parseCmd{},
} }
sort.Slice(c.subs, func(a, b int) bool { sort.Slice(c.subs, func(a, b int) bool {
return c.subs[a].Name() <= c.subs[b].Name() return c.subs[a].Name() <= c.subs[b].Name()

63
cmd/pkger/cmds/parse.go Normal file
View File

@ -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)
}

View File

@ -55,5 +55,4 @@ func (c *pathCmd) Exec(args []string) error {
enc := json.NewEncoder(os.Stdout) enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ") enc.SetIndent("", " ")
return enc.Encode(paths) return enc.Encode(paths)
return nil
} }

View File

@ -1,6 +1,7 @@
package parser package parser
import ( import (
"encoding/json"
"go/token" "go/token"
"os" "os"
"path/filepath" "path/filepath"
@ -17,6 +18,15 @@ type OpenDecl struct {
value string 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) { func (d OpenDecl) File() (*File, error) {
if d.file == nil { if d.file == nil {
return nil, os.ErrNotExist return nil, os.ErrNotExist

View File

@ -1,6 +1,7 @@
package parser package parser
import ( import (
"encoding/json"
"go/token" "go/token"
"os" "os"
"path/filepath" "path/filepath"
@ -18,6 +19,15 @@ type WalkDecl struct {
value string 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) { func (d WalkDecl) File() (*File, error) {
if d.file == nil { if d.file == nil {
return nil, os.ErrNotExist return nil, os.ErrNotExist