github.com/bingoohuang/pkger@v0.0.0-20210127185155-a71b9df4c4c7/cmd/pkger/cmds/path.go (about)

     1  package cmds
     2  
     3  import (
     4  	"encoding/json"
     5  	"flag"
     6  	"fmt"
     7  	"os"
     8  
     9  	"github.com/bingoohuang/pkger"
    10  	"github.com/bingoohuang/pkger/here"
    11  )
    12  
    13  type pathCmd struct {
    14  	*flag.FlagSet
    15  	json bool
    16  	help bool
    17  }
    18  
    19  func (s *pathCmd) Name() string {
    20  	return s.Flags().Name()
    21  }
    22  
    23  func (c *pathCmd) Flags() *flag.FlagSet {
    24  	if c.FlagSet == nil {
    25  		c.FlagSet = flag.NewFlagSet("path", flag.ExitOnError)
    26  		// c.BoolVar(&c.json, "json", false, "outputs as json")
    27  		c.BoolVar(&c.help, "h", false, "prints help information")
    28  	}
    29  	return c.FlagSet
    30  }
    31  
    32  func (c *pathCmd) Exec(args []string) error {
    33  
    34  	c.Parse(args)
    35  
    36  	if c.help {
    37  		c.Usage()
    38  		return nil
    39  	}
    40  
    41  	args = c.Args()
    42  	if len(args) == 0 {
    43  		return fmt.Errorf("you specify at least one path")
    44  	}
    45  
    46  	paths := map[string]here.Path{}
    47  	for _, a := range args {
    48  		pt, err := pkger.Parse(a)
    49  		if err != nil {
    50  			return err
    51  		}
    52  		paths[a] = pt
    53  	}
    54  
    55  	enc := json.NewEncoder(os.Stdout)
    56  	enc.SetIndent("", " ")
    57  	return enc.Encode(paths)
    58  }