From 48c2362aa8dbcd7b93f51d5e43dd631261f99e43 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Tue, 22 Oct 2019 13:40:06 -0400 Subject: [PATCH] if i had a mountain --- cmd/pkger/cmds/pack.go | 2 +- cmd/pkger/cmds/path.go | 59 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 cmd/pkger/cmds/path.go diff --git a/cmd/pkger/cmds/pack.go b/cmd/pkger/cmds/pack.go index 8ef6880..67e9a15 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{}, + &serveCmd{}, &statCmd{}, &infoCmd{}, &pathCmd{}, } sort.Slice(c.subs, func(a, b int) bool { return c.subs[a].Name() <= c.subs[b].Name() diff --git a/cmd/pkger/cmds/path.go b/cmd/pkger/cmds/path.go new file mode 100644 index 0000000..702102e --- /dev/null +++ b/cmd/pkger/cmds/path.go @@ -0,0 +1,59 @@ +package cmds + +import ( + "encoding/json" + "flag" + "fmt" + "os" + + "github.com/markbates/pkger" + "github.com/markbates/pkger/here" +) + +type pathCmd struct { + *flag.FlagSet + json bool + help bool +} + +func (s *pathCmd) Name() string { + return s.Flags().Name() +} + +func (c *pathCmd) Flags() *flag.FlagSet { + if c.FlagSet == nil { + c.FlagSet = flag.NewFlagSet("path", 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 *pathCmd) Exec(args []string) error { + + c.Parse(args) + + if c.help { + c.Usage() + return nil + } + + args = c.Args() + if len(args) == 0 { + return fmt.Errorf("you specify at least one path") + } + + paths := map[string]here.Path{} + for _, a := range args { + pt, err := pkger.Parse(a) + if err != nil { + return err + } + paths[a] = pt + } + + enc := json.NewEncoder(os.Stdout) + enc.SetIndent("", " ") + return enc.Encode(paths) + return nil +}