diff --git a/cmd/pkger/cmds/list.go b/cmd/pkger/cmds/list.go new file mode 100644 index 0000000..4474e10 --- /dev/null +++ b/cmd/pkger/cmds/list.go @@ -0,0 +1,103 @@ +package cmds + +import ( + "bytes" + "encoding/json" + "flag" + "fmt" + "os" + + "github.com/markbates/pkger" + "github.com/markbates/pkger/internal/takeon/github.com/markbates/hepa" + "github.com/markbates/pkger/internal/takeon/github.com/markbates/hepa/filters" + "github.com/markbates/pkger/parser" +) + +type listCmd struct { + *flag.FlagSet + help bool + json bool + subs []command +} + +func (e *listCmd) Name() string { + return e.Flags().Name() +} + +func (e *listCmd) Exec(args []string) error { + e.Parse(args) + + if e.help { + e.Usage() + return nil + } + + args = e.Args() + + info, err := pkger.Current() + if err != nil { + return err + } + + fp := info.FilePath(outName) + os.RemoveAll(fp) + + decls, err := parser.Parse(info) + if err != nil { + return err + } + + jay := struct { + ImportPath string `json:"import_path"` + Files []*parser.File `json:"files"` + }{ + ImportPath: info.ImportPath, + } + + for _, decl := range decls { + if fl, ok := decl.(parser.Filer); ok { + files, err := fl.Files() + if err != nil { + return err + } + jay.Files = append(jay.Files, files...) + } + + } + + if e.json { + bb := &bytes.Buffer{} + + enc := json.NewEncoder(bb) + enc.SetIndent("", " ") + if err := enc.Encode(jay); err != nil { + return err + } + + hep := hepa.New() + hep = hepa.With(hep, filters.Home()) + hep = hepa.With(hep, filters.Golang()) + + b, err := hep.Filter(bb.Bytes()) + if err != nil { + return err + } + _, err = os.Stdout.Write(b) + return err + } + + fmt.Println(jay.ImportPath) + for _, f := range jay.Files { + fmt.Println(" >", f.Path) + } + return nil +} + +func (e *listCmd) Flags() *flag.FlagSet { + if e.FlagSet == nil { + e.FlagSet = flag.NewFlagSet("list", flag.ExitOnError) + e.BoolVar(&e.json, "json", false, "prints in JSON format") + } + e.Usage = Usage(os.Stderr, e.FlagSet) + return e.FlagSet +} diff --git a/cmd/pkger/cmds/pack.go b/cmd/pkger/cmds/pack.go index ca5ae0b..009500f 100644 --- a/cmd/pkger/cmds/pack.go +++ b/cmd/pkger/cmds/pack.go @@ -17,7 +17,6 @@ const outName = "pkged.go" type packCmd struct { *flag.FlagSet help bool - list bool subs []command } @@ -34,21 +33,12 @@ func (e *packCmd) Exec(args []string) error { fp := info.FilePath(outName) os.RemoveAll(fp) - res, err := parser.Parse(info) + decls, err := parser.Parse(info) if err != nil { return err } - if e.list { - fmt.Println(info.ImportPath) - - for _, p := range res { - fmt.Printf(" > %s\n", p) - } - return nil - } - - if err := Package(fp, res); err != nil { + if err := Package(fp, decls); err != nil { return err } @@ -90,14 +80,13 @@ func New() (*packCmd, error) { c := &packCmd{} c.subs = []command{ - &serveCmd{}, &statCmd{}, &infoCmd{}, &pathCmd{}, &parseCmd{}, + &serveCmd{}, &statCmd{}, &infoCmd{}, &pathCmd{}, &parseCmd{}, &listCmd{}, } sort.Slice(c.subs, func(a, b int) bool { return c.subs[a].Name() <= c.subs[b].Name() }) c.FlagSet = flag.NewFlagSet("pkger", flag.ExitOnError) - c.BoolVar(&c.list, "list", false, "prints a list of files/dirs to be packaged") c.BoolVar(&c.help, "h", false, "prints help information") c.Usage = func() { fmt.Fprintf(os.Stderr, "Usage:\n\n") @@ -112,7 +101,6 @@ func New() (*packCmd, error) { func (e *packCmd) Flags() *flag.FlagSet { if e.FlagSet == nil { e.FlagSet = flag.NewFlagSet("", flag.ExitOnError) - e.BoolVar(&e.list, "list", false, "prints a list of files/dirs to be packaged") } e.Usage = Usage(os.Stderr, e.FlagSet) return e.FlagSet @@ -134,10 +122,6 @@ func Package(out string, decls parser.Decls) error { fmt.Fprintf(f, "package %s\n\n", c.Name) fmt.Fprintf(f, "import \"github.com/markbates/pkger\"\n\n") fmt.Fprintf(f, "import \"github.com/markbates/pkger/pkging/mem\"\n\n") - fmt.Fprintf(f, "// packing:\n") - // for _, p := range paths { - // fmt.Fprintf(f, "// %s\n", p) - // } fmt.Fprintf(f, "\nvar _ = pkger.Apply(mem.UnmarshalEmbed([]byte(`") if err := pkgutil.Stuff(f, c, decls); err != nil { diff --git a/here/info.go b/here/info.go index 37a301b..d848e84 100644 --- a/here/info.go +++ b/here/info.go @@ -16,35 +16,27 @@ type Info struct { Dir string ImportPath string Name string - // Imports []string - Module Module + Module Module } func (fi Info) MarshalJSON() ([]byte, error) { mm := map[string]interface{}{ "ImportPath": fi.ImportPath, "Name": fi.Name, - // "Imports": fi.Imports, - "Module": fi.Module, + "Module": fi.Module, + "Dir": fi.Dir, + } + + b, err := json.Marshal(mm) + if err != nil { + return nil, err } hep := hepa.New() hep = hepa.With(hep, filters.Home()) hep = hepa.With(hep, filters.Golang()) - cm := map[string]string{ - "Dir": fi.Dir, - } - - for k, v := range cm { - b, err := hep.Filter([]byte(v)) - if err != nil { - return nil, err - } - mm[k] = string(b) - } - - return json.Marshal(mm) + return hep.Filter(b) } func (i Info) FilePath(paths ...string) string {