2019-04-12 09:15:28 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
2020-01-15 18:36:05 +00:00
|
|
|
"src.lwithers.me.uk/go/htpack/cmd/htpacker/packer"
|
2020-02-15 11:19:17 +00:00
|
|
|
"src.lwithers.me.uk/go/htpack/packed"
|
2019-04-12 09:15:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var packCmd = &cobra.Command{
|
|
|
|
Use: "pack",
|
|
|
|
Short: "creates a packfile from a YAML spec or set of files/dirs",
|
2022-07-06 13:26:00 +01:00
|
|
|
Long: `When given a YAML spec file (a template for which can be generated
|
|
|
|
with the "yaml" command), files will be packed exactly as per the spec. The
|
|
|
|
--content-type flag cannot be used and no extra files can be specified.
|
|
|
|
|
|
|
|
When given a list of files and directories to pack, the content type for each
|
|
|
|
file will be automatically detected. It is possible to override the content
|
|
|
|
type by specifying one or more --content-type flags. These take an argument in
|
|
|
|
the form "pattern:content/type". The pattern is matched using common glob
|
|
|
|
(* = wildcard), very similar to .gitignore. If the pattern contains any
|
|
|
|
directory names, these must match the final components of the file to pack's
|
|
|
|
path. If the pattern starts with a "/", then the full path must be matched
|
|
|
|
exactly.
|
|
|
|
`,
|
|
|
|
|
2019-04-12 09:15:28 +01:00
|
|
|
RunE: func(c *cobra.Command, args []string) error {
|
|
|
|
// convert "out" to an absolute path, so that it will still
|
|
|
|
// work after chdir
|
|
|
|
out, err := c.Flags().GetString("out")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
out, err = filepath.Abs(out)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if "spec" is present, convert to an absolute path
|
|
|
|
spec, err := c.Flags().GetString("spec")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if spec != "" {
|
|
|
|
spec, err = filepath.Abs(spec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// chdir if required
|
|
|
|
chdir, err := c.Flags().GetString("chdir")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if chdir != "" {
|
|
|
|
if err = os.Chdir(chdir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 13:26:00 +01:00
|
|
|
// parse content-type globs
|
|
|
|
ctGlobList, err := c.Flags().GetStringArray("content-type")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctGlobs, err := parseGlobs(ctGlobList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-12 09:15:28 +01:00
|
|
|
// if "spec" is not present, then we expect a list of input
|
|
|
|
// files, and we'll build a spec from them
|
|
|
|
if spec == "" {
|
|
|
|
if len(args) == 0 {
|
|
|
|
return errors.New("need --yaml, " +
|
|
|
|
"or one or more filenames")
|
|
|
|
}
|
2022-07-06 13:26:00 +01:00
|
|
|
err = PackFiles2(c, args, ctGlobs, out)
|
2019-04-12 09:15:28 +01:00
|
|
|
} else {
|
|
|
|
if len(args) != 0 {
|
|
|
|
return errors.New("cannot specify files " +
|
|
|
|
"when using --yaml")
|
|
|
|
}
|
2022-07-06 13:26:00 +01:00
|
|
|
if ctGlobs != nil {
|
|
|
|
return errors.New("cannot specify --content-type " +
|
|
|
|
"when using --yaml")
|
|
|
|
}
|
2019-04-12 09:15:28 +01:00
|
|
|
err = PackSpec(c, spec, out)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
packCmd.Flags().StringP("out", "O", "",
|
|
|
|
"Output filename")
|
|
|
|
packCmd.MarkFlagRequired("out")
|
|
|
|
packCmd.Flags().StringP("spec", "y", "",
|
|
|
|
"YAML specification file (if not present, just pack files)")
|
|
|
|
packCmd.Flags().StringP("chdir", "C", "",
|
|
|
|
"Change to directory before searching for input files")
|
2022-07-06 13:26:00 +01:00
|
|
|
packCmd.Flags().StringArrayP("content-type", "", nil,
|
|
|
|
"Override content type for pattern, e.g. \"*.foo=bar/baz\" (like .gitignore)")
|
2019-04-12 09:15:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func PackFiles(c *cobra.Command, args []string, out string) error {
|
2022-07-06 13:26:00 +01:00
|
|
|
return PackFiles2(c, args, nil, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PackFiles2(c *cobra.Command, args []string, ctGlobs ctGlobList, out string) error {
|
2019-04-12 09:15:28 +01:00
|
|
|
ftp, err := filesFromList(args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-06 13:26:00 +01:00
|
|
|
ctGlobs.ApplyContentTypes(ftp)
|
2020-02-15 11:19:17 +00:00
|
|
|
|
|
|
|
return doPack(ftp, out)
|
2019-04-12 09:15:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func PackSpec(c *cobra.Command, spec, out string) error {
|
|
|
|
raw, err := ioutil.ReadFile(spec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ftp packer.FilesToPack
|
|
|
|
if err := yaml.UnmarshalStrict(raw, &ftp); err != nil {
|
|
|
|
return fmt.Errorf("parsing YAML spec %s: %v", spec, err)
|
|
|
|
}
|
|
|
|
|
2020-02-15 11:19:17 +00:00
|
|
|
return doPack(ftp, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doPack(ftp packer.FilesToPack, out string) error {
|
|
|
|
prog := mpbProgress(ftp)
|
|
|
|
err := packer.Pack2(ftp, out, prog)
|
|
|
|
prog.Complete()
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
fin, err := os.Open(out)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fin.Close()
|
|
|
|
|
|
|
|
_, dir, err := packed.Load(fin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
inspectSummary(dir)
|
|
|
|
}
|
|
|
|
return err
|
2019-04-12 09:15:28 +01:00
|
|
|
}
|