Revert "its bright tonight"

This reverts commit 908d029a0a.
This commit is contained in:
Mark Bates 2019-08-29 21:06:58 -04:00
parent fe08bafeef
commit 89a072f8d2
11 changed files with 78 additions and 198 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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