pkger/cmd/pkger/pack.go

148 lines
2.6 KiB
Go
Raw Normal View History

package main
2019-08-01 21:37:01 +03:00
import (
2019-08-05 00:13:27 +03:00
"flag"
"fmt"
2019-08-01 21:37:01 +03:00
"os"
2019-08-30 05:30:00 +03:00
"sort"
2019-08-01 21:37:01 +03:00
"github.com/markbates/pkger"
"github.com/markbates/pkger/parser"
2019-09-20 18:16:57 +03:00
"github.com/markbates/pkger/pkging"
2019-09-21 19:51:29 +03:00
"github.com/markbates/pkger/stuffing"
2019-08-01 21:37:01 +03:00
)
const outName = "pkged.go"
type packCmd struct {
2019-08-05 00:13:27 +03:00
*flag.FlagSet
2019-08-30 05:30:00 +03:00
help bool
list bool
2019-08-30 05:30:00 +03:00
subs []command
2019-08-05 00:13:27 +03:00
}
func (e *packCmd) Name() string {
return e.Flags().Name()
}
2019-08-05 00:13:27 +03:00
func (e *packCmd) Exec(args []string) error {
2019-09-20 18:16:57 +03:00
info, err := pkger.Current()
2019-08-01 21:37:01 +03:00
if err != nil {
return err
}
2019-09-20 18:16:57 +03:00
res, err := parser.Parse(info)
2019-08-01 21:37:01 +03:00
if err != nil {
return err
}
if e.list {
2019-08-05 00:13:27 +03:00
fmt.Println(res.Path)
for _, p := range res.Paths {
fmt.Printf(" > %s\n", p)
}
return nil
}
fp := info.FilePath(outName)
os.RemoveAll(fp)
2019-08-01 21:37:01 +03:00
if err := Package(fp, res.Paths); err != nil {
return err
}
return nil
}
2019-08-30 05:30:00 +03:00
func (e *packCmd) Route(args []string) error {
e.Parse(args)
if e.help {
e.Usage()
return nil
}
args = e.Args()
if len(args) == 0 {
return e.Exec(args)
}
k := args[0]
for _, c := range e.subs {
if k == c.Name() {
args = args[1:]
for _, a := range args {
if a == "-h" {
Usage(os.Stderr, c.Flags())()
return nil
}
}
return c.Exec(args)
}
}
return e.Exec(args)
}
func New() (*packCmd, error) {
c := &packCmd{}
c.subs = []command{
&readCmd{}, &serveCmd{}, &infoCmd{},
}
sort.Slice(c.subs, func(a, b int) bool {
return c.subs[a].Name() <= c.subs[b].Name()
})
c.FlagSet = flag.NewFlagSet("pkger", flag.ExitOnError)
c.BoolVar(&c.list, "list", false, "prints a list of files/dirs to be packaged")
c.BoolVar(&c.help, "h", false, "prints help information")
c.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage:\n\n")
Usage(os.Stderr, c.FlagSet)()
for _, s := range c.subs {
Usage(os.Stderr, s.Flags())()
}
}
return c, nil
}
func (e *packCmd) Flags() *flag.FlagSet {
if e.FlagSet == nil {
2019-08-30 05:30:00 +03:00
e.FlagSet = flag.NewFlagSet("", flag.ExitOnError)
e.BoolVar(&e.list, "list", false, "prints a list of files/dirs to be packaged")
}
2019-08-30 05:30:00 +03:00
e.Usage = Usage(os.Stderr, e.FlagSet)
return e.FlagSet
}
2019-09-20 18:16:57 +03:00
func Package(out string, paths []pkging.Path) error {
2019-08-02 07:22:17 +03:00
os.RemoveAll(out)
2019-08-01 21:37:01 +03:00
2019-08-05 00:13:27 +03:00
f, err := os.Create(out)
if err != nil {
2019-08-01 21:37:01 +03:00
return err
}
2019-09-21 19:51:29 +03:00
defer f.Close()
2019-08-01 21:37:01 +03:00
2019-09-20 18:16:57 +03:00
c, err := pkger.Current()
2019-08-02 07:22:17 +03:00
if err != nil {
return err
}
2019-08-05 00:13:27 +03:00
fmt.Fprintf(f, "package %s\n\n", c.Name)
fmt.Fprintf(f, "import \"github.com/markbates/pkger\"\n\n")
2019-09-21 19:51:29 +03:00
fmt.Fprintf(f, "import \"github.com/markbates/pkger/pkging/mem\"\n\n")
2019-08-01 21:37:01 +03:00
2019-09-21 19:51:29 +03:00
fmt.Fprintf(f, "var _ = pkger.Apply(mem.UnmarshalEmbed([]byte(`")
2019-08-01 21:37:01 +03:00
2019-09-21 19:51:29 +03:00
if err := stuffing.Stuff(f, c, paths); err != nil {
return err
}
fmt.Fprintf(f, "`)))\n")
2019-08-05 00:13:27 +03:00
2019-08-01 21:37:01 +03:00
return nil
}