From 89a072f8d2d24eeb848860d39679d7ddeb942434 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Thu, 29 Aug 2019 21:06:58 -0400 Subject: [PATCH] Revert "its bright tonight" This reverts commit 908d029a0a926c34d6cb039987a896e02dfbd1b2. --- cmd/pkger/cmd/command.go | 30 ---------- cmd/pkger/cmd/io.go | 30 ---------- cmd/pkger/cmd/main.go | 37 ------------ cmd/pkger/cmd/options.go | 18 ------ cmd/pkger/cmd/usage.go | 16 ----- cmd/pkger/{cmd => }/info.go | 4 +- cmd/pkger/main.go | 110 ++++++++++++++++++++++------------- cmd/pkger/{cmd => }/pack.go | 21 +------ cmd/pkger/{cmd => }/read.go | 4 +- cmd/pkger/{cmd => }/serve.go | 4 +- cmd/pkger/{cmd => }/walk.go | 2 +- 11 files changed, 78 insertions(+), 198 deletions(-) delete mode 100644 cmd/pkger/cmd/command.go delete mode 100644 cmd/pkger/cmd/io.go delete mode 100644 cmd/pkger/cmd/main.go delete mode 100644 cmd/pkger/cmd/options.go delete mode 100644 cmd/pkger/cmd/usage.go rename cmd/pkger/{cmd => }/info.go (90%) rename cmd/pkger/{cmd => }/pack.go (82%) rename cmd/pkger/{cmd => }/read.go (92%) rename cmd/pkger/{cmd => }/serve.go (91%) rename cmd/pkger/{cmd => }/walk.go (94%) diff --git a/cmd/pkger/cmd/command.go b/cmd/pkger/cmd/command.go deleted file mode 100644 index a37c10b..0000000 --- a/cmd/pkger/cmd/command.go +++ /dev/null @@ -1,30 +0,0 @@ -package cmd - -import ( - "flag" - "fmt" -) - -type command interface { - Name() string - execer - flagger -} - -type execer interface { - Exec([]string) error -} - -type flagger interface { - Flags() *flag.FlagSet -} -type arrayFlags []string - -func (i arrayFlags) String() string { - return fmt.Sprintf("%s", []string(i)) -} - -func (i *arrayFlags) Set(value string) error { - *i = append(*i, value) - return nil -} diff --git a/cmd/pkger/cmd/io.go b/cmd/pkger/cmd/io.go deleted file mode 100644 index f895fc3..0000000 --- a/cmd/pkger/cmd/io.go +++ /dev/null @@ -1,30 +0,0 @@ -package cmd - -import ( - "io" - "os" -) - -type IO interface { - In() io.Reader - Out() io.Writer - Err() io.Writer -} - -type stdio struct{} - -func (stdio) In() io.Reader { - return os.Stdin -} - -func (stdio) Out() io.Writer { - return os.Stdout -} - -func (stdio) Err() io.Writer { - return os.Stderr -} - -func StdIO() IO { - return stdio{} -} diff --git a/cmd/pkger/cmd/main.go b/cmd/pkger/cmd/main.go deleted file mode 100644 index fa6a5da..0000000 --- a/cmd/pkger/cmd/main.go +++ /dev/null @@ -1,37 +0,0 @@ -package cmd - -import ( - "fmt" -) - -// section: main -func Main(args []string, opts *Options) error { - if len(args) == 0 { - return fmt.Errorf("you must pass in an argument") - } - - opts.Flags.Parse(args) - args = opts.Flags.Args() - - if opts.Help { - return Usage(opts.Out(), opts.Flags) - } - - // s := args[0] - // - // u, err := url.Parse(s) - // if err != nil { - // return err - // } - // - // switch u.Scheme { - // case "file": - // return File(args, opts) - // case "http", "https": - // return HTTP(args, opts) - // } - - return fmt.Errorf("don't know how to handle %s", args) -} - -// section: main diff --git a/cmd/pkger/cmd/options.go b/cmd/pkger/cmd/options.go deleted file mode 100644 index 0363699..0000000 --- a/cmd/pkger/cmd/options.go +++ /dev/null @@ -1,18 +0,0 @@ -package cmd - -import "flag" - -type Options struct { - IO - Flags *flag.FlagSet - Help bool -} - -func NewOptions(gio IO) *Options { - g := &Options{ - IO: gio, - Flags: flag.NewFlagSet("pkger", flag.ExitOnError), - } - g.Flags.BoolVar(&g.Help, "h", false, "print help") - return g -} diff --git a/cmd/pkger/cmd/usage.go b/cmd/pkger/cmd/usage.go deleted file mode 100644 index 9d64cab..0000000 --- a/cmd/pkger/cmd/usage.go +++ /dev/null @@ -1,16 +0,0 @@ -package cmd - -import ( - "flag" - "fmt" - "io" -) - -func Usage(w io.Writer, f *flag.FlagSet) error { - fmt.Fprintf(w, "Usage:\n\n") - fmt.Fprintf(w, "%s [flags] [args...]\n", f.Name()) - f.VisitAll(func(fl *flag.Flag) { - fmt.Fprintf(w, "\t-%s\t%s (%q)\n", fl.Name, fl.Usage, fl.DefValue) - }) - return nil -} diff --git a/cmd/pkger/cmd/info.go b/cmd/pkger/info.go similarity index 90% rename from cmd/pkger/cmd/info.go rename to cmd/pkger/info.go index 378ed5f..a7ca793 100644 --- a/cmd/pkger/cmd/info.go +++ b/cmd/pkger/info.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "flag" @@ -17,7 +17,7 @@ func (s *infoCmd) Name() string { func (f *infoCmd) Flags() *flag.FlagSet { if f.FlagSet == nil { - f.FlagSet = flag.NewFlagSet("info", flag.ExitOnError) + f.FlagSet = flag.NewFlagSet("pkger info", flag.ExitOnError) } return f.FlagSet } diff --git a/cmd/pkger/main.go b/cmd/pkger/main.go index 9f7590c..18bebbd 100644 --- a/cmd/pkger/main.go +++ b/cmd/pkger/main.go @@ -1,55 +1,83 @@ package main import ( + "flag" + "fmt" "log" "os" - - "github.com/markbates/pkger/cmd/pkger/cmd" + "os/exec" + "sort" + "strings" ) -// type command interface { -// Name() string -// execer -// flagger -// } -// -// type execer interface { -// Exec([]string) error -// } -// -// type flagger interface { -// Flags() *flag.FlagSet -// } -// type arrayFlags []string -// -// func (i arrayFlags) String() string { -// return fmt.Sprintf("%s", []string(i)) -// } -// -// func (i *arrayFlags) Set(value string) error { -// *i = append(*i, value) -// return nil -// } +type command interface { + Name() string + execer + flagger +} + +type execer interface { + Exec([]string) error +} + +type flagger interface { + Flags() *flag.FlagSet +} +type arrayFlags []string + +func (i arrayFlags) String() string { + return fmt.Sprintf("%s", []string(i)) +} + +func (i *arrayFlags) Set(value string) error { + *i = append(*i, value) + return nil +} func main() { - args := os.Args[1:] - if err := cmd.Main(args, cmd.NewOptions(cmd.StdIO())); err != nil { + + defer func() { + c := exec.Command("go", "mod", "tidy") + c.Run() + }() + + root := &packCmd{} + cmds := []command{ + root, &readCmd{}, &serveCmd{}, &infoCmd{}, + } + sort.Slice(cmds, func(a, b int) bool { + return cmds[a].Name() <= cmds[b].Name() + }) + root.Flags().Usage = func() { + for _, c := range cmds { + fg := c.Flags() + fmt.Fprintf(os.Stderr, "%s:\n", fg.Name()) + fg.PrintDefaults() + } + } + + root.Parse(os.Args[1:]) + args := root.Args() + var ex command = root + + if len(args) > 0 { + k := args[0] + for _, c := range cmds { + if k == strings.TrimPrefix(c.Name(), "pkger ") { + ex = c + args = args[1:] + break + } + } + } + + flg := ex.Flags() + flg.Parse(args) + args = flg.Args() + + if err := ex.Exec(args); err != nil { log.Fatal(err) } - // - // defer func() { - // c := exec.Command("go", "mod", "tidy") - // c.Run() - // }() - // - // root := &packCmd{ - // subCmds: []command{ - // &readCmd{}, &serveCmd{}, &infoCmd{}, - // }, - // } - // if err := root.Exec(os.Args[1:]); err != nil { - // log.Fatal(err) - // } } // does not computee diff --git a/cmd/pkger/cmd/pack.go b/cmd/pkger/pack.go similarity index 82% rename from cmd/pkger/cmd/pack.go rename to cmd/pkger/pack.go index 5d65196..cb50726 100644 --- a/cmd/pkger/cmd/pack.go +++ b/cmd/pkger/pack.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "flag" @@ -13,8 +13,7 @@ const outName = "pkged.go" type packCmd struct { *flag.FlagSet - list bool - subCmds []command + list bool } func (e *packCmd) Name() string { @@ -22,21 +21,6 @@ func (e *packCmd) Name() string { } func (e *packCmd) Exec(args []string) error { - if len(args) > 0 { - a := args[0] - for _, c := range e.subCmds { - if a == c.Name() { - return c.Exec(args[1:]) - } - } - } - return e.pack(args) -} - -func (e *packCmd) pack(args []string) error { - e.Flags().Parse(args) - args = e.Args() - info, err := pkger.Stat() if err != nil { return err @@ -67,7 +51,6 @@ func (e *packCmd) pack(args []string) error { } func (e *packCmd) Flags() *flag.FlagSet { - if e.FlagSet == nil { e.FlagSet = flag.NewFlagSet("pkger", flag.ExitOnError) e.BoolVar(&e.list, "list", false, "prints a list of files/dirs to be packaged") diff --git a/cmd/pkger/cmd/read.go b/cmd/pkger/read.go similarity index 92% rename from cmd/pkger/cmd/read.go rename to cmd/pkger/read.go index 7d1bbfe..dbc1359 100644 --- a/cmd/pkger/cmd/read.go +++ b/cmd/pkger/read.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "encoding/json" @@ -21,7 +21,7 @@ func (s *readCmd) Name() string { func (r *readCmd) Flags() *flag.FlagSet { if r.FlagSet == nil { - r.FlagSet = flag.NewFlagSet("read", flag.ExitOnError) + r.FlagSet = flag.NewFlagSet("pkger read", flag.ExitOnError) r.FlagSet.BoolVar(&r.JSON, "json", false, "print as JSON") } return r.FlagSet diff --git a/cmd/pkger/cmd/serve.go b/cmd/pkger/serve.go similarity index 91% rename from cmd/pkger/cmd/serve.go rename to cmd/pkger/serve.go index e330780..9a518be 100644 --- a/cmd/pkger/cmd/serve.go +++ b/cmd/pkger/serve.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "flag" @@ -19,7 +19,7 @@ func (s *serveCmd) Name() string { func (f *serveCmd) Flags() *flag.FlagSet { if f.FlagSet == nil { - f.FlagSet = flag.NewFlagSet("serve", flag.ExitOnError) + f.FlagSet = flag.NewFlagSet("pkger serve", flag.ExitOnError) f.Var(&f.excludes, "exclude", "slice of regexp patterns to exclude") } return f.FlagSet diff --git a/cmd/pkger/cmd/walk.go b/cmd/pkger/walk.go similarity index 94% rename from cmd/pkger/cmd/walk.go rename to cmd/pkger/walk.go index 2d73087..59c642c 100644 --- a/cmd/pkger/cmd/walk.go +++ b/cmd/pkger/walk.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "fmt"