its bright tonight

This commit is contained in:
Mark Bates 2019-08-29 17:08:22 -04:00
parent 01cf733caa
commit 908d029a0a
11 changed files with 198 additions and 78 deletions

30
cmd/pkger/cmd/command.go Normal file
View File

@ -0,0 +1,30 @@
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
}

View File

@ -1,4 +1,4 @@
package main package cmd
import ( import (
"flag" "flag"
@ -17,7 +17,7 @@ func (s *infoCmd) Name() string {
func (f *infoCmd) Flags() *flag.FlagSet { func (f *infoCmd) Flags() *flag.FlagSet {
if f.FlagSet == nil { if f.FlagSet == nil {
f.FlagSet = flag.NewFlagSet("pkger info", flag.ExitOnError) f.FlagSet = flag.NewFlagSet("info", flag.ExitOnError)
} }
return f.FlagSet return f.FlagSet
} }

30
cmd/pkger/cmd/io.go Normal file
View File

@ -0,0 +1,30 @@
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{}
}

37
cmd/pkger/cmd/main.go Normal file
View File

@ -0,0 +1,37 @@
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

18
cmd/pkger/cmd/options.go Normal file
View File

@ -0,0 +1,18 @@
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
}

View File

@ -1,4 +1,4 @@
package main package cmd
import ( import (
"flag" "flag"
@ -13,7 +13,8 @@ const outName = "pkged.go"
type packCmd struct { type packCmd struct {
*flag.FlagSet *flag.FlagSet
list bool list bool
subCmds []command
} }
func (e *packCmd) Name() string { func (e *packCmd) Name() string {
@ -21,6 +22,21 @@ func (e *packCmd) Name() string {
} }
func (e *packCmd) Exec(args []string) error { 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() info, err := pkger.Stat()
if err != nil { if err != nil {
return err return err
@ -51,6 +67,7 @@ func (e *packCmd) Exec(args []string) error {
} }
func (e *packCmd) Flags() *flag.FlagSet { func (e *packCmd) Flags() *flag.FlagSet {
if e.FlagSet == nil { if e.FlagSet == nil {
e.FlagSet = flag.NewFlagSet("pkger", flag.ExitOnError) e.FlagSet = flag.NewFlagSet("pkger", flag.ExitOnError)
e.BoolVar(&e.list, "list", false, "prints a list of files/dirs to be packaged") e.BoolVar(&e.list, "list", false, "prints a list of files/dirs to be packaged")

View File

@ -1,4 +1,4 @@
package main package cmd
import ( import (
"encoding/json" "encoding/json"
@ -21,7 +21,7 @@ func (s *readCmd) Name() string {
func (r *readCmd) Flags() *flag.FlagSet { func (r *readCmd) Flags() *flag.FlagSet {
if r.FlagSet == nil { if r.FlagSet == nil {
r.FlagSet = flag.NewFlagSet("pkger read", flag.ExitOnError) r.FlagSet = flag.NewFlagSet("read", flag.ExitOnError)
r.FlagSet.BoolVar(&r.JSON, "json", false, "print as JSON") r.FlagSet.BoolVar(&r.JSON, "json", false, "print as JSON")
} }
return r.FlagSet return r.FlagSet

View File

@ -1,4 +1,4 @@
package main package cmd
import ( import (
"flag" "flag"
@ -19,7 +19,7 @@ func (s *serveCmd) Name() string {
func (f *serveCmd) Flags() *flag.FlagSet { func (f *serveCmd) Flags() *flag.FlagSet {
if f.FlagSet == nil { if f.FlagSet == nil {
f.FlagSet = flag.NewFlagSet("pkger serve", flag.ExitOnError) f.FlagSet = flag.NewFlagSet("serve", flag.ExitOnError)
f.Var(&f.excludes, "exclude", "slice of regexp patterns to exclude") f.Var(&f.excludes, "exclude", "slice of regexp patterns to exclude")
} }
return f.FlagSet return f.FlagSet

16
cmd/pkger/cmd/usage.go Normal file
View File

@ -0,0 +1,16 @@
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
}

View File

@ -1,4 +1,4 @@
package main package cmd
import ( import (
"fmt" "fmt"

View File

@ -1,83 +1,55 @@
package main package main
import ( import (
"flag"
"fmt"
"log" "log"
"os" "os"
"os/exec"
"sort" "github.com/markbates/pkger/cmd/pkger/cmd"
"strings"
) )
type command interface { // type command interface {
Name() string // Name() string
execer // execer
flagger // flagger
} // }
//
type execer interface { // type execer interface {
Exec([]string) error // Exec([]string) error
} // }
//
type flagger interface { // type flagger interface {
Flags() *flag.FlagSet // Flags() *flag.FlagSet
} // }
type arrayFlags []string // type arrayFlags []string
//
func (i arrayFlags) String() string { // func (i arrayFlags) String() string {
return fmt.Sprintf("%s", []string(i)) // return fmt.Sprintf("%s", []string(i))
} // }
//
func (i *arrayFlags) Set(value string) error { // func (i *arrayFlags) Set(value string) error {
*i = append(*i, value) // *i = append(*i, value)
return nil // return nil
} // }
func main() { func main() {
args := os.Args[1:]
defer func() { if err := cmd.Main(args, cmd.NewOptions(cmd.StdIO())); err != nil {
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) 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 // does not computee