Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

Commit

Permalink
Revert "its bright tonight"
Browse files Browse the repository at this point in the history
This reverts commit 908d029.
  • Loading branch information
markbates committed Aug 30, 2019
1 parent fe08baf commit 89a072f
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 198 deletions.
30 changes: 0 additions & 30 deletions cmd/pkger/cmd/command.go

This file was deleted.

30 changes: 0 additions & 30 deletions cmd/pkger/cmd/io.go

This file was deleted.

37 changes: 0 additions & 37 deletions cmd/pkger/cmd/main.go

This file was deleted.

18 changes: 0 additions & 18 deletions cmd/pkger/cmd/options.go

This file was deleted.

16 changes: 0 additions & 16 deletions cmd/pkger/cmd/usage.go

This file was deleted.

4 changes: 2 additions & 2 deletions cmd/pkger/cmd/info.go → cmd/pkger/info.go
@@ -1,4 +1,4 @@
package cmd
package main

import (
"flag"
Expand All @@ -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
}
Expand Down
110 changes: 69 additions & 41 deletions 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
21 changes: 2 additions & 19 deletions cmd/pkger/cmd/pack.go → cmd/pkger/pack.go
@@ -1,4 +1,4 @@
package cmd
package main

import (
"flag"
Expand All @@ -13,30 +13,14 @@ const outName = "pkged.go"

type packCmd struct {
*flag.FlagSet
list bool
subCmds []command
list bool
}

func (e *packCmd) Name() string {
return e.Flags().Name()
}

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
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions cmd/pkger/cmd/read.go → cmd/pkger/read.go
@@ -1,4 +1,4 @@
package cmd
package main

import (
"encoding/json"
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/pkger/cmd/serve.go → cmd/pkger/serve.go
@@ -1,4 +1,4 @@
package cmd
package main

import (
"flag"
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/pkger/cmd/walk.go → cmd/pkger/walk.go
@@ -1,4 +1,4 @@
package cmd
package main

import (
"fmt"
Expand Down

0 comments on commit 89a072f

Please sign in to comment.