Merge pull request #2326 from marianitadn/read-json-directories-as-object-streams

Add directory as option for createall command
This commit is contained in:
Brendan Burns 2014-11-17 09:50:57 -08:00
commit f05c4a69e8
2 changed files with 45 additions and 13 deletions

View File

@ -69,13 +69,16 @@ func DataToObjects(m meta.RESTMapper, t runtime.ObjectTyper, data []byte) (resul
func (f *Factory) NewCmdCreateAll(out io.Writer) *cobra.Command { func (f *Factory) NewCmdCreateAll(out io.Writer) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "createall -f filename", Use: "createall [-d directory] [-f filename]",
Short: "Create all resources specified in filename or stdin", Short: "Create all resources specified in a directory, filename or stdin",
Long: `Create all resources contained in JSON file specified in filename or stdin Long: `Create all resources contained in JSON file specified in a directory, filename or stdin
JSON and YAML formats are accepted. JSON and YAML formats are accepted.
Examples: Examples:
$ kubectl createall -d configs/
<creates all resources listed in JSON or YAML files, found recursively under the configs directory>
$ kubectl createall -f config.json $ kubectl createall -f config.json
<creates all resources listed in config.json> <creates all resources listed in config.json>
@ -87,23 +90,36 @@ Examples:
} }
filename := GetFlagString(cmd, "filename") filename := GetFlagString(cmd, "filename")
if len(filename) == 0 { directory := GetFlagString(cmd, "directory")
usageError(cmd, "Must pass a filename to update") if (len(filename) == 0 && len(directory) == 0) || (len(filename) != 0 && len(directory) != 0) {
usageError(cmd, "Must pass a directory or filename to update")
} }
files := []string{}
if len(filename) != 0 {
files = append(files, filename)
} else {
files = append(GetFilesFromDir(directory, ".json"), GetFilesFromDir(directory, ".yaml")...)
}
for _, filename := range files {
data, err := ReadConfigData(filename) data, err := ReadConfigData(filename)
checkErr(err) checkErr(err)
items, errs := DataToObjects(f.Mapper, f.Typer, data) items, errs := DataToObjects(f.Mapper, f.Typer, data)
applyErrs := config.CreateObjects(f.Typer, f.Mapper, clientFunc, items) applyErrs := config.CreateObjects(f.Typer, f.Mapper, clientFunc, items)
errs = append(errs, applyErrs...) errs = append(errs, applyErrs...)
if len(errs) > 0 { if len(errs) > 0 {
for _, e := range errs { for _, e := range errs {
glog.Error(e) glog.Error(e)
} }
} }
}
}, },
} }
cmd.Flags().StringP("directory", "d", "", "Directory of JSON or YAML files to use to update the resource")
cmd.Flags().StringP("filename", "f", "", "Filename or URL to file to use to update the resource") cmd.Flags().StringP("filename", "f", "", "Filename or URL to file to use to update the resource")
return cmd return cmd
} }

View File

@ -21,6 +21,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -92,6 +93,21 @@ func FirstNonEmptyString(args ...string) string {
return "" return ""
} }
// Return a list of file names of a certain type within a given directory.
func GetFilesFromDir(directory string, fileType string) []string {
files := []string{}
err := filepath.Walk(directory, func(path string, f os.FileInfo, err error) error {
if filepath.Ext(path) == fileType {
files = append(files, path)
}
return err
})
checkErr(err)
return files
}
// ReadConfigData reads the bytes from the specified filesytem or network // ReadConfigData reads the bytes from the specified filesytem or network
// location or from stdin if location == "-". // location or from stdin if location == "-".
func ReadConfigData(location string) ([]byte, error) { func ReadConfigData(location string) ([]byte, error) {