Skip to content

Commit

Permalink
feat(influx): add support for providing directories of pkgs to pkg co…
Browse files Browse the repository at this point in the history
…mmand

closes: #16657
  • Loading branch information
jsteenb2 committed Feb 6, 2020
1 parent e472b01 commit 617a9b9
Showing 1 changed file with 85 additions and 21 deletions.
106 changes: 85 additions & 21 deletions cmd/influx/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type cmdPkgBuilder struct {
disableTableBorders bool
org organization
quiet bool
recurse bool

applyOpts struct {
envRefs []string
Expand Down Expand Up @@ -96,6 +97,7 @@ func (b *cmdPkgBuilder) cmdPkgApply() *cobra.Command {
cmd.Short = "Apply a pkg to create resources"

b.org.register(cmd, false)
b.registerFileFlags(cmd)
cmd.Flags().StringVarP(&b.encoding, "encoding", "e", "", "Encoding for the input stream. If a file is provided will gather encoding type from file extension. If extension provided will override.")
cmd.Flags().BoolVarP(&b.quiet, "quiet", "q", false, "Disable output printing")
cmd.Flags().StringVar(&b.applyOpts.force, "force", "", `TTY input, if package will have destructive changes, proceed if set "true"`)
Expand All @@ -104,8 +106,6 @@ func (b *cmdPkgBuilder) cmdPkgApply() *cobra.Command {
cmd.Flags().BoolVar(&b.disableTableBorders, "disable-table-borders", false, "Disable table borders")

b.applyOpts.secrets = []string{}
cmd.Flags().StringSliceVarP(&b.files, "file", "f", nil, "Path to package file")
cmd.MarkFlagFilename("file", "yaml", "yml", "json", "jsonnet")
cmd.Flags().StringSliceVar(&b.applyOpts.secrets, "secret", nil, "Secrets to provide alongside the package; format should --secret=SECRET_KEY=SECRET_VALUE --secret=SECRET_KEY_2=SECRET_VALUE_2")
cmd.Flags().StringSliceVar(&b.applyOpts.envRefs, "env-ref", nil, "Environment references to provide alongside the package; format should --env-ref=REF_KEY=REF_VALUE --env-ref=REF_KEY_2=REF_VALUE_2")

Expand Down Expand Up @@ -137,9 +137,9 @@ func (b *cmdPkgBuilder) pkgApplyRunEFn(cmd *cobra.Command, args []string) error
isTTY bool
)
if b.applyOpts.url != "" {
pkg, err = pkger.Parse(b.applyEncoding(), pkger.FromHTTPRequest(b.applyOpts.url))
pkg, err = pkger.Parse(b.applyEncoding(""), pkger.FromHTTPRequest(b.applyOpts.url))
} else {
pkg, isTTY, err = b.readPkgStdInOrFile(b.files)
pkg, isTTY, err = b.readPkgStdInOrFile(b.files, b.recurse)
}
if err != nil {
return err
Expand Down Expand Up @@ -300,7 +300,7 @@ func (b *cmdPkgBuilder) pkgExportAllRunEFn(cmd *cobra.Command, args []string) er

func (b *cmdPkgBuilder) cmdPkgSummary() *cobra.Command {
runE := func(cmd *cobra.Command, args []string) error {
pkg, _, err := b.readPkgStdInOrFile(b.files)
pkg, _, err := b.readPkgStdInOrFile(b.files, b.recurse)
if err != nil {
return err
}
Expand All @@ -312,7 +312,7 @@ func (b *cmdPkgBuilder) cmdPkgSummary() *cobra.Command {
cmd := b.newCmd("summary", runE)
cmd.Short = "Summarize the provided package"

cmd.Flags().StringSliceVarP(&b.files, "file", "f", nil, "input file for pkg; if none provided will use TTY input")
b.registerFileFlags(cmd)
cmd.Flags().BoolVarP(&b.disableColor, "disable-color", "c", false, "Disable color in output")
cmd.Flags().BoolVar(&b.disableTableBorders, "disable-table-borders", false, "Disable table borders")

Expand All @@ -321,7 +321,7 @@ func (b *cmdPkgBuilder) cmdPkgSummary() *cobra.Command {

func (b *cmdPkgBuilder) cmdPkgValidate() *cobra.Command {
runE := func(cmd *cobra.Command, args []string) error {
pkg, _, err := b.readPkgStdInOrFile(b.files)
pkg, _, err := b.readPkgStdInOrFile(b.files, b.recurse)
if err != nil {
return err
}
Expand All @@ -331,12 +331,18 @@ func (b *cmdPkgBuilder) cmdPkgValidate() *cobra.Command {
cmd := b.newCmd("validate", runE)
cmd.Short = "Validate the provided package"

b.registerFileFlags(cmd)
cmd.Flags().StringVarP(&b.encoding, "encoding", "e", "", "Encoding for the input stream. If a file is provided will gather encoding type from file extension. If extension provided will override.")
cmd.Flags().StringSliceVarP(&b.files, "file", "f", nil, "input file for pkg; if none provided will use TTY input")

return cmd
}

func (b *cmdPkgBuilder) registerFileFlags(cmd *cobra.Command) {
cmd.Flags().StringSliceVarP(&b.files, "file", "f", nil, "Path to package file")
cmd.MarkFlagFilename("file", "yaml", "yml", "json", "jsonnet")
cmd.Flags().BoolVarP(&b.recurse, "recurse", "R", false, "Process the directory used in -f, --file recursively. Useful when you want to manage related manifests organized within the same directory.")
}

func (b *cmdPkgBuilder) writePkg(w io.Writer, pkgSVC pkger.SVC, outPath string, opts ...pkger.CreatePkgSetFn) error {
pkg, err := pkgSVC.CreatePkg(context.Background(), opts...)
if err != nil {
Expand All @@ -356,17 +362,33 @@ func (b *cmdPkgBuilder) writePkg(w io.Writer, pkgSVC pkger.SVC, outPath string,
return ioutil.WriteFile(outPath, buf.Bytes(), os.ModePerm)
}

func (b *cmdPkgBuilder) readPkgStdInOrFile(files []string) (*pkger.Pkg, bool, error) {
if len(files) > 0 {
var rawPkgs []*pkger.Pkg
for _, file := range files {
pkg, err := pkger.Parse(b.applyEncoding(), pkger.FromFile(file), pkger.ValidSkipParseError())
if err != nil {
return nil, false, err
}
rawPkgs = append(rawPkgs, pkg)
func (b *cmdPkgBuilder) readPkgFromFiles(filePaths []string, recurse bool) (*pkger.Pkg, error) {
mFiles := make(map[string]struct{})
for _, f := range filePaths {
files, err := readFilesFromPath(f, recurse)
if err != nil {
return nil, err
}
for _, ff := range files {
mFiles[ff] = struct{}{}
}
pkg, err := pkger.Combine(rawPkgs...)
}

var rawPkgs []*pkger.Pkg
for f := range mFiles {
pkg, err := pkger.Parse(b.applyEncoding(f), pkger.FromFile(f), pkger.ValidSkipParseError())
if err != nil {
return nil, err
}
rawPkgs = append(rawPkgs, pkg)
}

return pkger.Combine(rawPkgs...)
}

func (b *cmdPkgBuilder) readPkgStdInOrFile(files []string, recurse bool) (*pkger.Pkg, bool, error) {
if len(files) > 0 {
pkg, err := b.readPkgFromFiles(files, recurse)
return pkg, false, err
}

Expand All @@ -376,7 +398,7 @@ func (b *cmdPkgBuilder) readPkgStdInOrFile(files []string) (*pkger.Pkg, bool, er
isTTY = true
}

pkg, err := pkger.Parse(b.applyEncoding(), pkger.FromReader(b.in))
pkg, err := pkger.Parse(b.applyEncoding(""), pkger.FromReader(b.in))
return pkg, isTTY, err
}

Expand Down Expand Up @@ -421,9 +443,9 @@ func (b *cmdPkgBuilder) getInput(msg, defaultVal string) string {
return getInput(ui, msg, defaultVal)
}

func (b *cmdPkgBuilder) applyEncoding() pkger.Encoding {
func (b *cmdPkgBuilder) applyEncoding(file string) pkger.Encoding {
urlBase := path.Ext(b.applyOpts.url)
ext := filepath.Ext(b.file)
ext := filepath.Ext(file)
switch {
case ext == ".json" || b.encoding == "json" || urlBase == ".json":
return pkger.EncodingJSON
Expand Down Expand Up @@ -940,6 +962,48 @@ func formatDuration(d time.Duration) string {
return d.String()
}

func readFilesFromPath(filePath string, recurse bool) ([]string, error) {
info, err := os.Stat(filePath)
if err != nil {
return nil, err
}
if !info.IsDir() {
return []string{filePath}, nil
}

dirFiles, err := ioutil.ReadDir(filePath)
if err != nil {
return nil, err
}

mFiles := make(map[string]struct{})
assign := func(ss ...string) {
for _, s := range ss {
mFiles[s] = struct{}{}
}
}
for _, f := range dirFiles {
fileP := filepath.Join(filePath, f.Name())
if f.IsDir() {
if recurse {
rFiles, err := readFilesFromPath(fileP, recurse)
if err != nil {
return nil, err
}
assign(rFiles...)
}
continue
}
assign(fileP)
}

var files []string
for f := range mFiles {
files = append(files, f)
}
return files, nil
}

func mapKeys(provided, kvPairs []string) map[string]string {
out := make(map[string]string)
for _, k := range provided {
Expand Down

0 comments on commit 617a9b9

Please sign in to comment.