stat / info

This commit is contained in:
Mark Bates 2019-10-17 17:03:04 -04:00
parent 372454529c
commit 372c9371cf
7 changed files with 1130 additions and 33 deletions

View File

@ -9,6 +9,9 @@ import (
"github.com/markbates/pkger/pkging/pkgutil" "github.com/markbates/pkger/pkging/pkgutil"
) )
// Apply will wrap the current implementation
// of pkger.Pkger with the new pkg. This allows
// for layering of pkging.Pkger implementations.
func Apply(pkg pkging.Pkger, err error) error { func Apply(pkg pkging.Pkger, err error) error {
gil.Lock() gil.Lock()
defer gil.Unlock() defer gil.Unlock()

View File

@ -1,11 +1,11 @@
package main package main
import ( import (
"encoding/json"
"flag" "flag"
"fmt" "os"
"github.com/markbates/pkger" "github.com/markbates/pkger"
"github.com/markbates/pkger/pkging"
) )
type infoCmd struct { type infoCmd struct {
@ -16,41 +16,29 @@ func (s *infoCmd) Name() string {
return s.Flags().Name() return s.Flags().Name()
} }
func (f *infoCmd) Flags() *flag.FlagSet { func (r *infoCmd) Flags() *flag.FlagSet {
if f.FlagSet == nil { if r.FlagSet == nil {
f.FlagSet = flag.NewFlagSet("info", flag.ExitOnError) r.FlagSet = flag.NewFlagSet("info", flag.ExitOnError)
} }
return f.FlagSet return r.FlagSet
} }
func (f *infoCmd) Exec(args []string) error { func (r *infoCmd) Exec(args []string) error {
if len(args) == 0 { if len(args) == 0 {
args = []string{"."} args = []string{"."}
} }
for _, a := range args { for _, a := range args {
f, err := pkger.Open(a)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat() fi, err := pkger.Info(a)
if err != nil { if err != nil {
return err return err
} }
if fi.IsDir() { enc := json.NewEncoder(os.Stdout)
files, err := f.Readdir(-1) enc.SetIndent("", " ")
if err != nil { if err := enc.Encode(fi); err != nil {
return err return err
}
for _, ff := range files {
fmt.Println(pkging.NewFileInfo(ff))
}
continue
} }
fmt.Println(pkging.NewFileInfo(fi))
} }
return nil return nil

View File

@ -90,7 +90,7 @@ func New() (*packCmd, error) {
c := &packCmd{} c := &packCmd{}
c.subs = []command{ c.subs = []command{
&readCmd{}, &serveCmd{}, &infoCmd{}, &readCmd{}, &serveCmd{}, &statCmd{}, &infoCmd{},
} }
sort.Slice(c.subs, func(a, b int) bool { sort.Slice(c.subs, func(a, b int) bool {
return c.subs[a].Name() <= c.subs[b].Name() return c.subs[a].Name() <= c.subs[b].Name()

57
cmd/pkger/stat.go Normal file
View File

@ -0,0 +1,57 @@
package main
import (
"flag"
"fmt"
"github.com/markbates/pkger"
"github.com/markbates/pkger/pkging"
)
type statCmd struct {
*flag.FlagSet
}
func (s *statCmd) Name() string {
return s.Flags().Name()
}
func (f *statCmd) Flags() *flag.FlagSet {
if f.FlagSet == nil {
f.FlagSet = flag.NewFlagSet("stat", flag.ExitOnError)
}
return f.FlagSet
}
func (f *statCmd) Exec(args []string) error {
if len(args) == 0 {
args = []string{"."}
}
for _, a := range args {
f, err := pkger.Open(a)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
if fi.IsDir() {
files, err := f.Readdir(-1)
if err != nil {
return err
}
for _, ff := range files {
fmt.Println(pkging.NewFileInfo(ff))
}
continue
}
fmt.Println(pkging.NewFileInfo(fi))
}
return nil
}

View File

@ -18,16 +18,16 @@ type Info struct {
Dir string Dir string
ImportPath string ImportPath string
Name string Name string
Imports []string // Imports []string
Module Module Module Module
} }
func (fi Info) MarshalJSON() ([]byte, error) { func (fi Info) MarshalJSON() ([]byte, error) {
mm := map[string]interface{}{ mm := map[string]interface{}{
"ImportPath": fi.ImportPath, "ImportPath": fi.ImportPath,
"Name": fi.Name, "Name": fi.Name,
"Imports": fi.Imports, // "Imports": fi.Imports,
"Module": fi.Module, "Module": fi.Module,
} }
hep := hepa.New() hep := hepa.New()

View File

@ -2,6 +2,9 @@ module github.com/markbates/pkger/examples/app
go 1.13 go 1.13
require github.com/markbates/pkger v0.0.0 require (
github.com/gobuffalo/buffalo v0.14.11 // indirect
github.com/markbates/pkger v0.0.0
)
replace github.com/markbates/pkger => ../../ replace github.com/markbates/pkger => ../../../

File diff suppressed because it is too large Load Diff