func startHoarder(ccmd *cobra.Command, args []string) error { // convert the log level logLvl := lumber.LvlInt(viper.GetString("log-level")) // configure the logger lumber.Prefix("[hoader]") lumber.Level(logLvl) // enable/start garbage collection if age config was changed if ccmd.Flag("clean-after").Changed { lumber.Debug("Starting garbage collector (data older than %vs)...\n", ccmd.Flag("clean-after").Value) // start garbage collector go collector.Start() } // set, and initialize, the backend driver if err := backends.Initialize(); err != nil { lumber.Error("Failed to initialize backend - %v", err) return err } // start the API if err := api.Start(); err != nil { lumber.Fatal("Failed to start API: ", err.Error()) return err } return nil }
func generate(pkgName, pkgPath string, c *cobra.Command) ([]string, error) { m := make(map[string]string) c.Flags().Visit(func(f *pflag.Flag) { if f.Name != "pkg-path" { m[f.Name] = f.Value.String() } }) if _, ok := m["out"]; !ok { m["out"] = c.Flag("out").DefValue } // turn "out" into an absolute path var err error m["out"], err = filepath.Abs(m["out"]) if err != nil { return nil, err } gen, err := meta.NewGenerator( pkgName+".Generate", []*codegen.ImportSpec{codegen.SimpleImport(pkgPath)}, m, ) if err != nil { return nil, err } return gen.Generate() }
// Complete turns a partially defined TopImagesOptions into a solvent structure // which can be validated and used for showing limits usage. func (o *TopImagesOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []string, out io.Writer) error { osClient, kClient, err := f.Clients() if err != nil { return err } namespace := cmd.Flag("namespace").Value.String() if len(namespace) == 0 { namespace = kapi.NamespaceAll } o.out = out allImages, err := osClient.Images().List(kapi.ListOptions{}) if err != nil { return err } o.Images = allImages allStreams, err := osClient.ImageStreams(namespace).List(kapi.ListOptions{}) if err != nil { return err } o.Streams = allStreams allPods, err := kClient.Pods(namespace).List(kapi.ListOptions{}) if err != nil { return err } o.Pods = allPods return nil }
// Restore loads the arguments from disk and prefills the Request func Restore(config *api.Config, cmd *cobra.Command) { data, err := ioutil.ReadFile(DefaultConfigPath) if err != nil { data, err = ioutil.ReadFile(".stifile") if err != nil { glog.V(1).Infof("Unable to restore %s: %v", DefaultConfigPath, err) return } glog.Infof("DEPRECATED: Use %s instead of .stifile", DefaultConfigPath) } c := Config{} if err := json.Unmarshal(data, &c); err != nil { glog.V(1).Infof("Unable to parse %s: %v", DefaultConfigPath, err) return } config.BuilderImage = c.BuilderImage config.Source = c.Source config.Tag = c.Tag for name, value := range c.Flags { // Do not change flags that user sets. Allow overriding of stored flags. if cmd.Flag(name).Changed { continue } cmd.Flags().Set(name, value) } }
func mustLoadConfig(cmd *cobra.Command) { home := os.Getenv("HOME") if home == "" { home = os.Getenv("USERPROFILE") } if home == "" { log.Fatalf("Unable to locate home directory, giving up\n") } configFile := filepath.Join(home, perUserDotFile) if raw, err := ioutil.ReadFile(configFile); err != nil { log.Fatalf("Unable to load config file; try running \"grind init\"\n") } else if err := json.Unmarshal(raw, &Config); err != nil { log.Printf("failed to parse %s: %v", configFile, err) log.Fatalf("you may wish to try deleting the file and running \"grind init\" again\n") } if cmd.Flag("api").Value.String() == "true" { Config.apiReport = true } if cmd.Flag("api-dump").Value.String() == "true" { Config.apiReport = true Config.apiDump = true } checkVersion() }
func connect(cmd *cobra.Command) (*http.Client, error) { url := cmd.Flag("registry").Value.String() // XXX construct client using TLS client := &http.Client{Transport: &http.Transport{}} resp, err := client.Get(url + "/v2/") if err != nil { return nil, err } defer resp.Body.Close() switch resp.StatusCode { case http.StatusUnauthorized: // error text in resp.Body return nil, errors.New("take action based on WWW-Authenticate") case http.StatusNotFound: return nil, errors.New("registry does not support v2 API") case http.StatusOK: break default: return nil, fmt.Errorf("bad status (%s/v2/) %d", url, resp.StatusCode) } // consume body so connection can be reused ioutil.ReadAll(resp.Body) ver := resp.Header.Get("Docker-Distribution-API-Version") if ver != "registry/2.0" { return nil, errors.New("registry does not support v2 API") } return client, nil }
func (o *LogsOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error { containerName := cmdutil.GetFlagString(cmd, "container") switch len(args) { case 0: return cmdutil.UsageError(cmd, "POD is required for logs") case 1: o.ResourceArg = args[0] case 2: if cmd.Flag("container").Changed { return cmdutil.UsageError(cmd, "only one of -c, [CONTAINER] arg is allowed") } o.ResourceArg = args[0] containerName = args[1] default: return cmdutil.UsageError(cmd, "logs POD [-c CONTAINER]") } var err error o.Namespace, _, err = f.DefaultNamespace() if err != nil { return err } logOptions := &api.PodLogOptions{ Container: containerName, Follow: cmdutil.GetFlagBool(cmd, "follow"), Previous: cmdutil.GetFlagBool(cmd, "previous"), Timestamps: cmdutil.GetFlagBool(cmd, "timestamps"), } if sinceTime := cmdutil.GetFlagString(cmd, "since-time"); len(sinceTime) > 0 { t, err := api.ParseRFC3339(sinceTime, unversioned.Now) if err != nil { return err } logOptions.SinceTime = &t } if limit := cmdutil.GetFlagInt64(cmd, "limit-bytes"); limit != 0 { logOptions.LimitBytes = &limit } if tail := cmdutil.GetFlagInt64(cmd, "tail"); tail != -1 { logOptions.TailLines = &tail } if sinceSeconds := cmdutil.GetFlagDuration(cmd, "since"); sinceSeconds != 0 { // round up to the nearest second sec := int64(math.Ceil(float64(sinceSeconds) / float64(time.Second))) logOptions.SinceSeconds = &sec } o.Options = logOptions o.Mapper, o.Typer = f.Object() o.Decoder = f.Decoder(true) o.ClientMapper = resource.ClientMapperFunc(f.ClientForMapping) o.LogsForObject = f.LogsForObject o.Out = out return nil }
func testFlag(cmd *cobra.Command, flagName string, defaultVal string, t *testing.T) { f := cmd.Flag(flagName) if f == nil { t.Fatalf("expected flag %s to be registered but found none", flagName) } if f.DefValue != defaultVal { t.Errorf("expected default value of %s for %s but found %s", defaultVal, flagName, f.DefValue) } }
func globalFlags(cmd *cobra.Command) { if verbose { logrus.SetLevel(logrus.DebugLevel) } if cmd.Flag("driver").Changed { driver = cmd.Flag("driver").Value.String() } else { driver = os.Getenv("DOCKER_GRAPHDRIVER") } }
func (c *CLI) globalFlags(cmd *cobra.Command) *flag.FlagSet { fs := &flag.FlagSet{} if cmd.HasParent() { fs.AddFlagSet(cmd.InheritedFlags()) if fs.Lookup("help") == nil && cmd.Flag("help") != nil { fs.AddFlag(cmd.Flag("help")) } } else { fs.AddFlagSet(cmd.PersistentFlags()) } return c.sansDriverFlags(c.sansAdditionalFlags(fs)) }
func runGen(c *cobra.Command) ([]string, error) { pkgPath := c.Flag("pkg-path").Value.String() pkgSrcPath, err := codegen.PackageSourcePath(pkgPath) if err != nil { return nil, fmt.Errorf("invalid plugin package import path: %s", err) } pkgName, err := codegen.PackageName(pkgSrcPath) if err != nil { return nil, fmt.Errorf("invalid plugin package import path: %s", err) } return generate(pkgName, pkgPath, c) }
func getIncludeExcludeArgs(cmd *cobra.Command) (include, exclude *string) { includeFlag := cmd.Flag("include") excludeFlag := cmd.Flag("exclude") if includeFlag.Changed { include = &includeArg } if excludeFlag.Changed { exclude = &excludeArg } return }
func delete(cmd *cobra.Command, args []string) { url := cmd.Flag("registry").Value.String() if len(args) != 1 { cmd.UsageFunc()(cmd) return } delimg := args[0] conn, err := connect(cmd) if err != nil { fmt.Fprintln(os.Stderr, err) return } images, err := catalog(conn, url) for _, name := range images { tags, err := tags(conn, url, name) if err != nil { fmt.Fprintln(os.Stderr, err) continue } for _, tag := range tags { if strings.Compare(delimg, name+":"+tag) == 0 { digest, _, err := manifest(conn, url, name, tag) if err != nil { fmt.Fprintf(os.Stderr, "manifest: %v\n", err) return } err = deleteImage(conn, url, name, digest) if err != nil { fmt.Fprintf(os.Stderr, "delete: %v\n", err) return } fmt.Printf("deleted %s:%s\n", name, tag) return } } } }
func ValidOperation(c *cobra.Command) error { numOps := 0 if c.Flag("schedulable").Changed { numOps++ } if evacuate { numOps++ } if listpods { numOps++ } if numOps == 0 { return errors.New("must provide a node operation. Supported operations: --schedulable, --evacuate and --list-pods") } else if numOps != 1 { return errors.New("must provide only one node operation at a time") } return nil }
func (o *LogsOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error { switch len(args) { case 0: return cmdutil.UsageError(cmd, "POD is required for log") case 1: o.PodName = args[0] case 2: if cmd.Flag("container").Changed { return cmdutil.UsageError(cmd, "only one of -c, [CONTAINER] arg is allowed") } o.PodName = args[0] o.ContainerName = args[1] default: return cmdutil.UsageError(cmd, "log POD [-c CONTAINER]") } var err error o.PodNamespace, _, err = f.DefaultNamespace() if err != nil { return err } o.Client, err = f.Client() if err != nil { return err } sinceTime := cmdutil.GetFlagString(cmd, "since-time") if len(sinceTime) > 0 { t, err := api.ParseRFC3339(sinceTime, unversioned.Now) if err != nil { return err } o.SinceTime = &t } return nil }
func runMe(cmd *cobra.Command, args []string) { if len(args) == 0 { fmt.Fprintf(os.Stderr, "ERROR: Please specify at least one P2 .XML clip file.") os.Exit(1) } for _, filename := range args { xmlBytes, err := ioutil.ReadFile(filename) if err != nil { log.Fatalf("Error! %v", err) } clip, err := clip.ReadXML(filename, xmlBytes) if err != nil { log.Fatalf("Error! %v", err) } if cmd.Flag("outputdir").Value.String() != "" { clip.OutputDir = cmd.Flag("outputdir").Value.String() } filter := clip.GetFilter() cmdline := clip.CommandLine(filter) for i := range cmdline { // Double up the backslashes. This shouldn't be necessary, but FFmpeg // seems to require it on Windows. cmdline[i] = strings.Replace(cmdline[i], "\\", "\\\\", -1) } ffCmd := exec.Command("ffmpeg", cmdline...) ffCmd.Stdin = os.Stdin ffCmd.Stdout = os.Stdout ffCmd.Stderr = os.Stderr err = ffCmd.Run() if err != nil { log.Fatalf("Got error running ffmpeg: %v", err) } } }
func (o *DebugOptions) Complete(cmd *cobra.Command, f *clientcmd.Factory, args []string, in io.Reader, out, errout io.Writer) error { if i := cmd.ArgsLenAtDash(); i != -1 && i < len(args) { o.Command = args[i:] args = args[:i] } resources, envArgs, ok := cmdutil.SplitEnvironmentFromResources(args) if !ok { return kcmdutil.UsageError(cmd, "all resources must be specified before environment changes: %s", strings.Join(args, " ")) } switch { case o.ForceTTY && o.NoStdin: return kcmdutil.UsageError(cmd, "you may not specify -I and -t together") case o.ForceTTY && o.DisableTTY: return kcmdutil.UsageError(cmd, "you may not specify -t and -T together") case o.ForceTTY: o.Attach.TTY = true case o.DisableTTY: o.Attach.TTY = false // don't default TTY to true if a command is passed case len(o.Command) > 0: o.Attach.TTY = false o.Attach.Stdin = false default: o.Attach.TTY = term.IsTerminal(in) glog.V(4).Infof("Defaulting TTY to %t", o.Attach.TTY) } if o.NoStdin { o.Attach.TTY = false o.Attach.Stdin = false } if o.Annotations == nil { o.Annotations = make(map[string]string) } if len(o.Command) == 0 { o.Command = []string{"/bin/sh"} } cmdNamespace, explicit, err := f.DefaultNamespace() if err != nil { return err } mapper, typer := f.Object(false) b := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder()). NamespaceParam(cmdNamespace).DefaultNamespace(). SingleResourceType(). ResourceNames("pods", resources...). Flatten() if len(o.Filename) > 0 { b.FilenameParam(explicit, false, o.Filename) } o.AddEnv, o.RemoveEnv, err = cmdutil.ParseEnv(envArgs, nil) if err != nil { return err } one := false infos, err := b.Do().IntoSingular(&one).Infos() if err != nil { return err } if !one { return fmt.Errorf("you must identify a resource with a pod template to debug") } template, err := f.ApproximatePodTemplateForObject(infos[0].Object) if err != nil && template == nil { return fmt.Errorf("cannot debug %s: %v", infos[0].Name, err) } if err != nil { glog.V(4).Infof("Unable to get exact template, but continuing with fallback: %v", err) } pod := &kapi.Pod{ ObjectMeta: template.ObjectMeta, Spec: template.Spec, } pod.Name, pod.Namespace = infos[0].Name, infos[0].Namespace o.Attach.Pod = pod o.AsNonRoot = !o.AsRoot && cmd.Flag("as-root").Changed if len(o.Attach.ContainerName) == 0 && len(pod.Spec.Containers) > 0 { glog.V(4).Infof("Defaulting container name to %s", pod.Spec.Containers[0].Name) o.Attach.ContainerName = pod.Spec.Containers[0].Name } o.Annotations[debugPodAnnotationSourceResource] = fmt.Sprintf("%s/%s", infos[0].Mapping.Resource, infos[0].Name) o.Annotations[debugPodAnnotationSourceContainer] = o.Attach.ContainerName output := kcmdutil.GetFlagString(cmd, "output") if len(output) != 0 { o.Print = func(pod *kapi.Pod, out io.Writer) error { return f.PrintObject(cmd, mapper, pod, out) } } config, err := f.ClientConfig() if err != nil { return err } o.Attach.Config = config _, kc, err := f.Clients() if err != nil { return err } o.Attach.Client = kc return nil }
// RunGet implements the generic Get command // TODO: convert all direct flag accessors to a struct and pass that instead of cmd func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *GetOptions) error { selector := cmdutil.GetFlagString(cmd, "selector") allNamespaces := cmdutil.GetFlagBool(cmd, "all-namespaces") mapper, typer := f.Object() cmdNamespace, enforceNamespace, err := f.DefaultNamespace() if err != nil { return err } if len(args) == 0 && len(options.Filenames) == 0 { fmt.Fprint(out, "You must specify the type of resource to get. ", valid_resources, ` * componentstatuses (aka 'cs') * endpoints (aka 'ep') `) return cmdutil.UsageError(cmd, "Required resource not specified.") } // always show resources when getting by name or filename argsHasNames, err := resource.HasNames(args) if err != nil { return err } if len(options.Filenames) > 0 || argsHasNames { cmd.Flag("show-all").Value.Set("true") } // handle watch separately since we cannot watch multiple resource types isWatch, isWatchOnly := cmdutil.GetFlagBool(cmd, "watch"), cmdutil.GetFlagBool(cmd, "watch-only") if isWatch || isWatchOnly { r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()). NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces). FilenameParam(enforceNamespace, options.Filenames...). SelectorParam(selector). ResourceTypeOrNameArgs(true, args...). SingleResourceType(). Latest(). Do() err := r.Err() if err != nil { return err } infos, err := r.Infos() if err != nil { return err } if len(infos) != 1 { return fmt.Errorf("watch is only supported on a single resource - %d resources were found", len(infos)) } info := infos[0] mapping := info.ResourceMapping() printer, err := f.PrinterForMapping(cmd, mapping, allNamespaces) if err != nil { return err } obj, err := r.Object() if err != nil { return err } rv, err := mapping.MetadataAccessor.ResourceVersion(obj) if err != nil { return err } // print the current object if !isWatchOnly { if err := printer.PrintObj(obj, out); err != nil { return fmt.Errorf("unable to output the provided object: %v", err) } } // print watched changes w, err := r.Watch(rv) if err != nil { return err } kubectl.WatchLoop(w, func(e watch.Event) error { return printer.PrintObj(e.Object, out) }) return nil } b := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()). NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces). FilenameParam(enforceNamespace, options.Filenames...). SelectorParam(selector). ResourceTypeOrNameArgs(true, args...). ContinueOnError(). Latest() printer, generic, err := cmdutil.PrinterForCommand(cmd) if err != nil { return err } if generic { clientConfig, err := f.ClientConfig() if err != nil { return err } defaultVersion := clientConfig.Version singular := false r := b.Flatten().Do() infos, err := r.IntoSingular(&singular).Infos() if err != nil { return err } // the outermost object will be converted to the output-version, but inner // objects can use their mappings version := cmdutil.OutputVersion(cmd, defaultVersion) obj, err := resource.AsVersionedObject(infos, !singular, version) if err != nil { return err } return printer.PrintObj(obj, out) } // use the default printer for each object printer = nil var lastMapping *meta.RESTMapping w := kubectl.GetNewTabWriter(out) defer w.Flush() return b.Flatten().Do().Visit(func(r *resource.Info, err error) error { if err != nil { return err } if printer == nil || lastMapping == nil || r.Mapping == nil || r.Mapping.Resource != lastMapping.Resource { printer, err = f.PrinterForMapping(cmd, r.Mapping, allNamespaces) if err != nil { return err } lastMapping = r.Mapping } if _, found := printer.(*kubectl.HumanReadablePrinter); found { return printer.PrintObj(r.Object, w) } return printer.PrintObj(r.Object, out) }) }
// RunGet implements the generic Get command // TODO: convert all direct flag accessors to a struct and pass that instead of cmd func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *GetOptions) error { selector := cmdutil.GetFlagString(cmd, "selector") allNamespaces := cmdutil.GetFlagBool(cmd, "all-namespaces") mapper, typer := f.Object() cmdNamespace, enforceNamespace, err := f.DefaultNamespace() if err != nil { return err } if len(args) == 0 && len(options.Filenames) == 0 { fmt.Fprint(out, "You must specify the type of resource to get. ", valid_resources) return cmdutil.UsageError(cmd, "Required resource not specified.") } // always show resources when getting by name or filename argsHasNames, err := resource.HasNames(args) if err != nil { return err } if len(options.Filenames) > 0 || argsHasNames { cmd.Flag("show-all").Value.Set("true") } export := cmdutil.GetFlagBool(cmd, "export") // handle watch separately since we cannot watch multiple resource types isWatch, isWatchOnly := cmdutil.GetFlagBool(cmd, "watch"), cmdutil.GetFlagBool(cmd, "watch-only") if isWatch || isWatchOnly { r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)). NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces). FilenameParam(enforceNamespace, options.Filenames...). SelectorParam(selector). ExportParam(export). ResourceTypeOrNameArgs(true, args...). SingleResourceType(). Latest(). Do() err := r.Err() if err != nil { return err } infos, err := r.Infos() if err != nil { return err } if len(infos) != 1 { return fmt.Errorf("watch is only supported on individual resources and resource collections - %d resources were found", len(infos)) } info := infos[0] mapping := info.ResourceMapping() printer, err := f.PrinterForMapping(cmd, mapping, allNamespaces) if err != nil { return err } obj, err := r.Object() if err != nil { return err } rv, err := mapping.MetadataAccessor.ResourceVersion(obj) if err != nil { return err } // print the current object if !isWatchOnly { if err := printer.PrintObj(obj, out); err != nil { return fmt.Errorf("unable to output the provided object: %v", err) } } // print watched changes w, err := r.Watch(rv) if err != nil { return err } kubectl.WatchLoop(w, func(e watch.Event) error { return printer.PrintObj(e.Object, out) }) return nil } b := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)). NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces). FilenameParam(enforceNamespace, options.Filenames...). SelectorParam(selector). ExportParam(export). ResourceTypeOrNameArgs(true, args...). ContinueOnError(). Latest() printer, generic, err := cmdutil.PrinterForCommand(cmd) if err != nil { return err } if generic { clientConfig, err := f.ClientConfig() if err != nil { return err } singular := false r := b.Flatten().Do() infos, err := r.IntoSingular(&singular).Infos() if err != nil { return err } // the outermost object will be converted to the output-version, but inner // objects can use their mappings version, err := cmdutil.OutputVersion(cmd, clientConfig.GroupVersion) if err != nil { return err } obj, err := resource.AsVersionedObject(infos, !singular, version.String(), f.JSONEncoder()) if err != nil { return err } return printer.PrintObj(obj, out) } infos, err := b.Flatten().Do().Infos() if err != nil { return err } objs := make([]runtime.Object, len(infos)) for ix := range infos { objs[ix] = infos[ix].Object } sorting, err := cmd.Flags().GetString("sort-by") var sorter *kubectl.RuntimeSort if err == nil && len(sorting) > 0 && len(objs) > 1 { // TODO: questionable if sorter, err = kubectl.SortObjects(f.Decoder(true), objs, sorting); err != nil { return err } } // use the default printer for each object printer = nil var lastMapping *meta.RESTMapping w := kubectl.GetNewTabWriter(out) defer w.Flush() for ix := range objs { var mapping *meta.RESTMapping if sorter != nil { mapping = infos[sorter.OriginalPosition(ix)].Mapping } else { mapping = infos[ix].Mapping } if printer == nil || lastMapping == nil || mapping == nil || mapping.Resource != lastMapping.Resource { printer, err = f.PrinterForMapping(cmd, mapping, allNamespaces) if err != nil { return err } lastMapping = mapping } if _, found := printer.(*kubectl.HumanReadablePrinter); found { if err := printer.PrintObj(objs[ix], w); err != nil { return err } continue } if err := printer.PrintObj(objs[ix], out); err != nil { return err } } return nil }
func list(cmd *cobra.Command, args []string) { url := cmd.Flag("registry").Value.String() var filter string if len(args) > 0 { filter = args[0] } conn, err := connect(cmd) if err != nil { fmt.Fprintln(os.Stderr, err) return } type Image struct { name string tags []string } imageset := make([]*Image, 0) images, err := catalog(conn, url) n := 0 for _, name := range images { if filter != "" && !Glob(filter, name) { continue } tags, err := tags(conn, url, name) if err != nil { fmt.Fprintln(os.Stderr, err) continue } img := &Image{ name: name, tags: tags, } for _, t := range tags { if n < len(name+t) { n = len(name + t) } } imageset = append(imageset, img) } for _, i := range imageset { for _, t := range i.tags { fmt.Printf("%-*s", n+1, i.name+":"+t) if getsize { sz, err := imagesize(conn, url, i.name, t) if err != nil { fmt.Fprintln(os.Stderr, err) continue } if usebytes { fmt.Printf(" %12d", sz) } else { fmt.Printf(" %12s", humanize.Bytes(sz)) } } if getdigest { digest, _, err := manifest(conn, url, i.name, t) if err != nil { fmt.Fprintln(os.Stderr, err) continue } fmt.Printf(" %s", digest) } fmt.Printf("\n") } } }
// RunGet implements the generic Get command // TODO: convert all direct flag accessors to a struct and pass that instead of cmd func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args []string, options *GetOptions) error { if len(options.Raw) > 0 { restClient, err := f.RESTClient() if err != nil { return err } stream, err := restClient.Get().RequestURI(options.Raw).Stream() if err != nil { return err } defer stream.Close() _, err = io.Copy(out, stream) if err != nil && err != io.EOF { return err } return nil } selector := cmdutil.GetFlagString(cmd, "selector") allNamespaces := cmdutil.GetFlagBool(cmd, "all-namespaces") showKind := cmdutil.GetFlagBool(cmd, "show-kind") mapper, typer, err := f.UnstructuredObject() if err != nil { return err } filterFuncs := f.DefaultResourceFilterFunc() filterOpts := f.DefaultResourceFilterOptions(cmd, allNamespaces) cmdNamespace, enforceNamespace, err := f.DefaultNamespace() if err != nil { return err } if allNamespaces { enforceNamespace = false } if len(args) == 0 && cmdutil.IsFilenameEmpty(options.Filenames) { fmt.Fprint(errOut, "You must specify the type of resource to get. ", valid_resources) fullCmdName := cmd.Parent().CommandPath() usageString := "Required resource not specified." if len(fullCmdName) > 0 && cmdutil.IsSiblingCommandExists(cmd, "explain") { usageString = fmt.Sprintf("%s\nUse \"%s explain <resource>\" for a detailed description of that resource (e.g. %[2]s explain pods).", usageString, fullCmdName) } return cmdutil.UsageError(cmd, usageString) } // always show resources when getting by name or filename argsHasNames, err := resource.HasNames(args) if err != nil { return err } if len(options.Filenames) > 0 || argsHasNames { cmd.Flag("show-all").Value.Set("true") } export := cmdutil.GetFlagBool(cmd, "export") // handle watch separately since we cannot watch multiple resource types isWatch, isWatchOnly := cmdutil.GetFlagBool(cmd, "watch"), cmdutil.GetFlagBool(cmd, "watch-only") if isWatch || isWatchOnly { r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.UnstructuredClientForMapping), runtime.UnstructuredJSONScheme). NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces). FilenameParam(enforceNamespace, &options.FilenameOptions). SelectorParam(selector). ExportParam(export). ResourceTypeOrNameArgs(true, args...). SingleResourceType(). Latest(). Do() err := r.Err() if err != nil { return err } infos, err := r.Infos() if err != nil { return err } if len(infos) != 1 { return fmt.Errorf("watch is only supported on individual resources and resource collections - %d resources were found", len(infos)) } info := infos[0] mapping := info.ResourceMapping() printer, err := f.PrinterForMapping(cmd, mapping, allNamespaces) if err != nil { return err } obj, err := r.Object() if err != nil { return err } // watching from resourceVersion 0, starts the watch at ~now and // will return an initial watch event. Starting form ~now, rather // the rv of the object will insure that we start the watch from // inside the watch window, which the rv of the object might not be. rv := "0" isList := meta.IsListType(obj) if isList { // the resourceVersion of list objects is ~now but won't return // an initial watch event rv, err = mapping.MetadataAccessor.ResourceVersion(obj) if err != nil { return err } } // print the current object filteredResourceCount := 0 if !isWatchOnly { if err := printer.PrintObj(obj, out); err != nil { return fmt.Errorf("unable to output the provided object: %v", err) } filteredResourceCount++ cmdutil.PrintFilterCount(filteredResourceCount, mapping.Resource, filterOpts) } // print watched changes w, err := r.Watch(rv) if err != nil { return err } first := true filteredResourceCount = 0 intr := interrupt.New(nil, w.Stop) intr.Run(func() error { _, err := watch.Until(0, w, func(e watch.Event) (bool, error) { if !isList && first { // drop the initial watch event in the single resource case first = false return false, nil } err := printer.PrintObj(e.Object, out) if err != nil { return false, err } filteredResourceCount++ cmdutil.PrintFilterCount(filteredResourceCount, mapping.Resource, filterOpts) return false, nil }) return err }) return nil } r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.UnstructuredClientForMapping), runtime.UnstructuredJSONScheme). NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces). FilenameParam(enforceNamespace, &options.FilenameOptions). SelectorParam(selector). ExportParam(export). ResourceTypeOrNameArgs(true, args...). ContinueOnError(). Latest(). Flatten(). Do() err = r.Err() if err != nil { return err } printer, generic, err := cmdutil.PrinterForCommand(cmd) if err != nil { return err } if generic { // we flattened the data from the builder, so we have individual items, but now we'd like to either: // 1. if there is more than one item, combine them all into a single list // 2. if there is a single item and that item is a list, leave it as its specific list // 3. if there is a single item and it is not a a list, leave it as a single item var errs []error singular := false infos, err := r.IntoSingular(&singular).Infos() if err != nil { if singular { return err } errs = append(errs, err) } if len(infos) == 0 && len(errs) == 0 { outputEmptyListWarning(errOut) } res := "" if len(infos) > 0 { res = infos[0].ResourceMapping().Resource } var obj runtime.Object if singular { obj = infos[0].Object } else { // we have more than one item, so coerce all items into a list list := &runtime.UnstructuredList{ Object: map[string]interface{}{ "kind": "List", "apiVersion": "v1", "metadata": map[string]interface{}{}, }, } for _, info := range infos { list.Items = append(list.Items, info.Object.(*runtime.Unstructured)) } obj = list } isList := meta.IsListType(obj) if isList { filteredResourceCount, items, err := cmdutil.FilterResourceList(obj, filterFuncs, filterOpts) if err != nil { return err } // take the filtered items and create a new list for display list := &runtime.UnstructuredList{ Object: map[string]interface{}{ "kind": "List", "apiVersion": "v1", "metadata": map[string]interface{}{}, }, } if listMeta, err := meta.ListAccessor(obj); err == nil { list.Object["selfLink"] = listMeta.GetSelfLink() list.Object["resourceVersion"] = listMeta.GetResourceVersion() } for _, item := range items { list.Items = append(list.Items, item.(*runtime.Unstructured)) } if err := printer.PrintObj(list, out); err != nil { errs = append(errs, err) } cmdutil.PrintFilterCount(filteredResourceCount, res, filterOpts) return utilerrors.Reduce(utilerrors.Flatten(utilerrors.NewAggregate(errs))) } filteredResourceCount := 0 if isFiltered, err := filterFuncs.Filter(obj, filterOpts); !isFiltered { if err != nil { glog.V(2).Infof("Unable to filter resource: %v", err) } else if err := printer.PrintObj(obj, out); err != nil { errs = append(errs, err) } } else if isFiltered { filteredResourceCount++ } cmdutil.PrintFilterCount(filteredResourceCount, res, filterOpts) return utilerrors.Reduce(utilerrors.Flatten(utilerrors.NewAggregate(errs))) } allErrs := []error{} errs := sets.NewString() infos, err := r.Infos() if err != nil { allErrs = append(allErrs, err) } if len(infos) == 0 && len(allErrs) == 0 { outputEmptyListWarning(errOut) } objs := make([]runtime.Object, len(infos)) for ix := range infos { objs[ix] = infos[ix].Object } sorting, err := cmd.Flags().GetString("sort-by") if err != nil { return err } var sorter *kubectl.RuntimeSort if len(sorting) > 0 && len(objs) > 1 { // TODO: questionable if sorter, err = kubectl.SortObjects(f.Decoder(true), objs, sorting); err != nil { return err } } // use the default printer for each object printer = nil var lastMapping *meta.RESTMapping w := kubectl.GetNewTabWriter(out) filteredResourceCount := 0 if resource.MultipleTypesRequested(args) || cmdutil.MustPrintWithKinds(objs, infos, sorter) { showKind = true } for ix := range objs { var mapping *meta.RESTMapping var original runtime.Object if sorter != nil { mapping = infos[sorter.OriginalPosition(ix)].Mapping original = infos[sorter.OriginalPosition(ix)].Object } else { mapping = infos[ix].Mapping original = infos[ix].Object } if printer == nil || lastMapping == nil || mapping == nil || mapping.Resource != lastMapping.Resource { if printer != nil { w.Flush() cmdutil.PrintFilterCount(filteredResourceCount, lastMapping.Resource, filterOpts) } printer, err = f.PrinterForMapping(cmd, mapping, allNamespaces) if err != nil { if !errs.Has(err.Error()) { errs.Insert(err.Error()) allErrs = append(allErrs, err) } continue } // add linebreak between resource groups (if there is more than one) // skip linebreak above first resource group noHeaders := cmdutil.GetFlagBool(cmd, "no-headers") if lastMapping != nil && !noHeaders { fmt.Fprintf(errOut, "%s\n", "") } lastMapping = mapping } // filter objects if filter has been defined for current object if isFiltered, err := filterFuncs.Filter(original, filterOpts); isFiltered { if err == nil { filteredResourceCount++ continue } if !errs.Has(err.Error()) { errs.Insert(err.Error()) allErrs = append(allErrs, err) } } if resourcePrinter, found := printer.(*kubectl.HumanReadablePrinter); found { resourceName := resourcePrinter.GetResourceKind() if mapping != nil { if resourceName == "" { resourceName = mapping.Resource } if alias, ok := kubectl.ResourceShortFormFor(mapping.Resource); ok { resourceName = alias } else if resourceName == "" { resourceName = "none" } } else { resourceName = "none" } if showKind { resourcePrinter.EnsurePrintWithKind(resourceName) } if err := printer.PrintObj(original, w); err != nil { if !errs.Has(err.Error()) { errs.Insert(err.Error()) allErrs = append(allErrs, err) } } continue } if err := printer.PrintObj(original, w); err != nil { if !errs.Has(err.Error()) { errs.Insert(err.Error()) allErrs = append(allErrs, err) } continue } } w.Flush() if printer != nil && lastMapping != nil { cmdutil.PrintFilterCount(filteredResourceCount, lastMapping.Resource, filterOpts) } return utilerrors.NewAggregate(allErrs) }
// RunProject contains all the necessary functionality for the OpenShift cli process command func RunProcess(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []string) error { templateName, valueArgs := "", []string{} for _, s := range args { isValue := strings.Contains(s, "=") switch { case isValue: valueArgs = append(valueArgs, s) case !isValue && len(templateName) == 0: templateName = s case !isValue && len(templateName) > 0: return kcmdutil.UsageError(cmd, "template name must be specified only once: %s", s) } } filename := kcmdutil.GetFlagString(cmd, "filename") if len(templateName) == 0 && len(filename) == 0 { return kcmdutil.UsageError(cmd, "Must pass a filename or name of stored template") } if kcmdutil.GetFlagBool(cmd, "parameters") { for _, flag := range []string{"value", "labels", "output", "output-version", "raw", "template"} { if f := cmd.Flags().Lookup(flag); f != nil && f.Changed { return kcmdutil.UsageError(cmd, "The --parameters flag does not process the template, can't be used with --%v", flag) } } } namespace, explicit, err := f.DefaultNamespace() if err != nil { return err } mapper, typer := f.Object() client, _, err := f.Clients() if err != nil { return err } var ( objects []runtime.Object infos []*resource.Info ) mapping, err := mapper.RESTMapping(templateapi.Kind("Template")) if err != nil { return err } // When templateName is not empty, then we fetch the template from the // server, otherwise we require to set the `-f` parameter. if len(templateName) > 0 { var ( storedTemplate, rs string sourceNamespace string ok bool ) sourceNamespace, rs, storedTemplate, ok = parseNamespaceResourceName(templateName, namespace) if !ok { return fmt.Errorf("invalid argument %q", templateName) } if len(rs) > 0 && (rs != "template" && rs != "templates") { return fmt.Errorf("unable to process invalid resource %q", rs) } if len(storedTemplate) == 0 { return fmt.Errorf("invalid value syntax %q", templateName) } templateObj, err := client.Templates(sourceNamespace).Get(storedTemplate) if err != nil { if errors.IsNotFound(err) { return fmt.Errorf("template %q could not be found", storedTemplate) } return err } templateObj.CreationTimestamp = unversioned.Now() infos = append(infos, &resource.Info{Object: templateObj}) } else { infos, err = resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder()). NamespaceParam(namespace).RequireNamespace(). FilenameParam(explicit, filename). Do(). Infos() if err != nil { return err } } outputFormat := kcmdutil.GetFlagString(cmd, "output") for i := range infos { obj, ok := infos[i].Object.(*templateapi.Template) if !ok { sourceName := filename if len(templateName) > 0 { sourceName = namespace + "/" + templateName } fmt.Fprintf(cmd.Out(), "unable to parse %q, not a valid Template but %s\n", sourceName, reflect.TypeOf(infos[i].Object)) continue } // If 'parameters' flag is set it does not do processing but only print // the template parameters to console for inspection. // If multiple templates are passed, this will print combined output for all // templates. if kcmdutil.GetFlagBool(cmd, "parameters") { if len(infos) > 1 { fmt.Fprintf(out, "\n%s:\n", obj.Name) } if err := describe.PrintTemplateParameters(obj.Parameters, out); err != nil { fmt.Fprintf(cmd.Out(), "error printing parameters for %q: %v\n", obj.Name, err) } continue } if label := kcmdutil.GetFlagString(cmd, "labels"); len(label) > 0 { lbl, err := kubectl.ParseLabels(label) if err != nil { fmt.Fprintf(cmd.Out(), "error parsing labels: %v\n", err) continue } if obj.ObjectLabels == nil { obj.ObjectLabels = make(map[string]string) } for key, value := range lbl { obj.ObjectLabels[key] = value } } // Override the values for the current template parameters // when user specify the --value if cmd.Flag("value").Changed { values := kcmdutil.GetFlagStringSlice(cmd, "value") injectUserVars(values, out, obj) } injectUserVars(valueArgs, out, obj) resultObj, err := client.TemplateConfigs(namespace).Create(obj) if err != nil { fmt.Fprintf(cmd.Out(), "error processing the template %q: %v\n", obj.Name, err) continue } if outputFormat == "describe" { if s, err := (&describe.TemplateDescriber{ MetadataAccessor: meta.NewAccessor(), ObjectTyper: kapi.Scheme, ObjectDescriber: nil, }).DescribeTemplate(resultObj); err != nil { fmt.Fprintf(cmd.Out(), "error describing %q: %v\n", obj.Name, err) } else { fmt.Fprintf(out, s) } continue } objects = append(objects, resultObj.Objects...) } // Do not print the processed templates when asked to only show parameters or // describe. if kcmdutil.GetFlagBool(cmd, "parameters") || outputFormat == "describe" { return nil } p, _, err := kubectl.GetPrinter(outputFormat, "") if err != nil { return err } gv := mapping.GroupVersionKind.GroupVersion() version, err := kcmdutil.OutputVersion(cmd, &gv) if err != nil { return err } p = kubectl.NewVersionedPrinter(p, kapi.Scheme, version) // use generic output if kcmdutil.GetFlagBool(cmd, "raw") { for i := range objects { p.PrintObj(objects[i], out) } return nil } return p.PrintObj(&kapi.List{ ListMeta: unversioned.ListMeta{}, Items: objects, }, out) }
func CommandCreate(cmd *cobra.Command, args []string) { mustLoadConfig(cmd) now := time.Now() // find the directory d := "" switch len(args) { case 0: d = "." case 1: d = args[0] default: cmd.Help() return } dir, err := filepath.Abs(d) if err != nil { log.Fatalf("error finding directory %q: %v", d, err) } // find the problem.cfg file for { path := filepath.Join(dir, ProblemConfigName) if _, err := os.Stat(path); err != nil { if os.IsNotExist(err) { // try moving up a directory old := dir dir = filepath.Dir(dir) if dir == old { log.Fatalf("unable to find %s in %s or an ancestor directory", ProblemConfigName, d) } log.Printf("could not find %s in %s, trying %s", ProblemConfigName, old, dir) continue } log.Fatalf("error searching for %s in %s: %v", ProblemConfigName, dir, err) } break } // parse problem.cfg cfg := struct { Problem struct { Unique string Note string Type string Tag []string Option []string } Step map[string]*struct { Note string Weight float64 } }{} configPath := filepath.Join(dir, ProblemConfigName) fmt.Printf("reading %s\n", configPath) err = gcfg.ReadFileInto(&cfg, configPath) if err != nil { log.Fatalf("failed to parse %s: %v", configPath, err) } // create problem object problem := &Problem{ Unique: cfg.Problem.Unique, Note: cfg.Problem.Note, ProblemType: cfg.Problem.Type, Tags: cfg.Problem.Tag, Options: cfg.Problem.Option, CreatedAt: now, UpdatedAt: now, } // start forming the problem bundle unsigned := &ProblemBundle{ Problem: problem, } // check if this is an existing problem existing := []*Problem{} mustGetObject("/problems", map[string]string{"unique": problem.Unique}, &existing) switch len(existing) { case 0: // new problem if cmd.Flag("update").Value.String() == "true" { log.Fatalf("you specified --update, but no existing problem with unique ID %q was found", problem.Unique) } // make sure the problem set with this unique name is free as well existingSets := []*ProblemSet{} mustGetObject("/problem_sets", map[string]string{"unique": problem.Unique}, &existingSets) if len(existingSets) > 1 { log.Fatalf("error: server found multiple problem sets with matching unique ID %q", problem.Unique) } if len(existingSets) != 0 { log.Printf("problem set %d already exists with unique ID %q", existingSets[0].ID, existingSets[0].Unique) log.Fatalf(" this would prevent creating a problem set containing just this problem with matching id") } log.Printf("this problem is new--no existing problem has the same unique ID") case 1: // update to existing problem if cmd.Flag("update").Value.String() == "false" { log.Fatalf("you did not specify --update, but a problem already exists with unique ID %q", problem.Unique) } log.Printf("unique ID is %s", problem.Unique) log.Printf(" this is an update of problem %d (%q)", existing[0].ID, existing[0].Note) problem.ID = existing[0].ID problem.CreatedAt = existing[0].CreatedAt default: // server does not know what "unique" means log.Fatalf("error: server found multiple problems with matching unique ID %q", problem.Unique) } // generate steps whitelist := make(map[string]bool) for i := int64(1); cfg.Step[strconv.FormatInt(i, 10)] != nil; i++ { log.Printf("gathering step %d", i) s := cfg.Step[strconv.FormatInt(i, 10)] step := &ProblemStep{ Step: i, Note: s.Note, Weight: s.Weight, Files: make(map[string]string), } commit := &Commit{ Step: i, Action: "confirm", Note: "author solution submitted via grind", Files: make(map[string]string), CreatedAt: now, UpdatedAt: now, } // read files starter, solution, root := make(map[string]string), make(map[string]string), make(map[string]string) stepdir := filepath.Join(dir, strconv.FormatInt(i, 10)) err := filepath.Walk(stepdir, func(path string, info os.FileInfo, err error) error { if err != nil { log.Fatalf("walk error for %s: %v", path, err) } if info.IsDir() { return nil } relpath, err := filepath.Rel(stepdir, path) if err != nil { log.Fatalf("error finding relative path of %s: %v", path, err) } // load the file and add it to the appropriate place contents, err := ioutil.ReadFile(path) if err != nil { log.Fatalf("error reading %s: %v", relpath, err) } // pick out solution/starter files reldir, relfile := filepath.Split(relpath) if reldir == "_solution/" && relfile != "" { solution[relfile] = string(contents) } else if reldir == "_starter/" && relfile != "" { starter[relfile] = string(contents) } else if reldir == "" && relfile != "" { root[relfile] = string(contents) } else { step.Files[relpath] = string(contents) } return nil }) if err != nil { log.Fatalf("walk error for %s: %v", stepdir, err) } // find starter files and solution files if len(solution) > 0 && len(starter) > 0 && len(root) > 0 { log.Fatalf("found files in _starter, _solution, and root directory; unsure how to proceed") } if len(solution) > 0 { // explicit solution } else if len(root) > 0 { // files in root directory must be the solution solution = root root = nil } else { log.Fatalf("no solution files found in _solution or root directory; problem must have a solution") } if len(starter) == 0 && root != nil { starter = root } // copy the starter files into the step for name, contents := range starter { step.Files[name] = contents // if the file exists as a starter in this or earlier steps, it can be part of the solution whitelist[name] = true } // copy the solution files into the commit for name, contents := range solution { if whitelist[name] { commit.Files[name] = contents } else { log.Printf("Warning: skipping solution file %q", name) log.Printf(" because it is not in the starter file set of this or any previous step") } } unsigned.ProblemSteps = append(unsigned.ProblemSteps, step) unsigned.Commits = append(unsigned.Commits, commit) log.Printf(" found %d problem definition file%s and %d solution file%s", len(step.Files), plural(len(step.Files)), len(commit.Files), plural(len(commit.Files))) } if len(unsigned.ProblemSteps) != len(cfg.Step) { log.Fatalf("expected to find %d step%s, but only found %d", len(cfg.Step), plural(len(cfg.Step)), len(unsigned.ProblemSteps)) } // get user ID user := new(User) mustGetObject("/users/me", nil, user) // get the request validated and signed signed := new(ProblemBundle) mustPostObject("/problem_bundles/unconfirmed", nil, unsigned, signed) // validate the commits one at a time for n := 0; n < len(signed.ProblemSteps); n++ { log.Printf("validating solution for step %d", n+1) unvalidated := &CommitBundle{ Problem: signed.Problem, ProblemSteps: signed.ProblemSteps, ProblemSignature: signed.ProblemSignature, Commit: signed.Commits[n], CommitSignature: signed.CommitSignatures[n], } validated := mustConfirmCommitBundle(user.ID, unvalidated, nil) log.Printf(" finished validating solution") if validated.Commit.ReportCard == nil || validated.Commit.Score != 1.0 || !validated.Commit.ReportCard.Passed { log.Printf(" solution for step %d failed: %s", n+1, validated.Commit.ReportCard.Note) // play the transcript for _, event := range validated.Commit.Transcript { switch event.Event { case "exec": color.Cyan("$ %s\n", strings.Join(event.ExecCommand, " ")) case "stdin": color.Yellow("%s", event.StreamData) case "stdout": color.White("%s", event.StreamData) case "stderr": color.Red("%s", event.StreamData) case "exit": color.Cyan("%s\n", event.ExitStatus) case "error": color.Red("Error: %s\n", event.Error) } } log.Fatalf("please fix solution and try again") } signed.Problem = validated.Problem signed.ProblemSteps = validated.ProblemSteps signed.ProblemSignature = validated.ProblemSignature signed.Commits[n] = validated.Commit signed.CommitSignatures[n] = validated.CommitSignature } log.Printf("problem and solution confirmed successfully") // save the problem final := new(ProblemBundle) if signed.Problem.ID == 0 { mustPostObject("/problem_bundles/confirmed", nil, signed, final) } else { mustPutObject(fmt.Sprintf("/problem_bundles/%d", signed.Problem.ID), nil, signed, final) } log.Printf("problem %q saved and ready to use", final.Problem.Unique) if signed.Problem.ID == 0 { // create a matching problem set // pause for a bit since the database seems to need to catch up time.Sleep(time.Second) // create a problem set with just this problem and the same unique name psBundle := &ProblemSetBundle{ ProblemSet: &ProblemSet{ Unique: final.Problem.Unique, Note: "set for single problem " + final.Problem.Unique + "\n" + final.Problem.Note, Tags: final.Problem.Tags, CreatedAt: now, UpdatedAt: now, }, ProblemIDs: []int64{final.Problem.ID}, Weights: []float64{1.0}, } finalPSBundle := new(ProblemSetBundle) mustPostObject("/problem_set_bundles", nil, psBundle, finalPSBundle) log.Printf("problem set %q created and ready to use for this problem", finalPSBundle.ProblemSet.Unique) } }
// RunGet implements the generic Get command // TODO: convert all direct flag accessors to a struct and pass that instead of cmd func RunGet(f *cmdutil.Factory, out io.Writer, errOut io.Writer, cmd *cobra.Command, args []string, options *GetOptions) error { if len(options.Raw) > 0 { restClient, err := f.RESTClient() if err != nil { return err } stream, err := restClient.Get().RequestURI(options.Raw).Stream() if err != nil { return err } defer stream.Close() for { buffer := make([]byte, 1024, 1024) bytesRead, err := stream.Read(buffer) if bytesRead > 0 { fmt.Printf("%s", string(buffer[:bytesRead])) } if err == io.EOF { return nil } if err != nil { return err } } } selector := cmdutil.GetFlagString(cmd, "selector") allNamespaces := cmdutil.GetFlagBool(cmd, "all-namespaces") showKind := cmdutil.GetFlagBool(cmd, "show-kind") mapper, typer := f.Object() printAll := false cmdNamespace, enforceNamespace, err := f.DefaultNamespace() if err != nil { return err } if allNamespaces { enforceNamespace = false } if len(args) == 0 && cmdutil.IsFilenameEmpty(options.Filenames) { fmt.Fprint(errOut, "You must specify the type of resource to get. ", valid_resources) return cmdutil.UsageError(cmd, "Required resource not specified.") } // determine if args contains "all" for _, a := range args { if a == "all" { printAll = true break } } // always show resources when getting by name or filename argsHasNames, err := resource.HasNames(args) if err != nil { return err } if len(options.Filenames) > 0 || argsHasNames { cmd.Flag("show-all").Value.Set("true") } export := cmdutil.GetFlagBool(cmd, "export") // handle watch separately since we cannot watch multiple resource types isWatch, isWatchOnly := cmdutil.GetFlagBool(cmd, "watch"), cmdutil.GetFlagBool(cmd, "watch-only") if isWatch || isWatchOnly { r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)). NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces). FilenameParam(enforceNamespace, &options.FilenameOptions). SelectorParam(selector). ExportParam(export). ResourceTypeOrNameArgs(true, args...). SingleResourceType(). Latest(). Do() err := r.Err() if err != nil { return err } infos, err := r.Infos() if err != nil { return err } if len(infos) != 1 { return fmt.Errorf("watch is only supported on individual resources and resource collections - %d resources were found", len(infos)) } info := infos[0] mapping := info.ResourceMapping() printer, err := f.PrinterForMapping(cmd, mapping, allNamespaces) if err != nil { return err } obj, err := r.Object() if err != nil { return err } // watching from resourceVersion 0, starts the watch at ~now and // will return an initial watch event. Starting form ~now, rather // the rv of the object will insure that we start the watch from // inside the watch window, which the rv of the object might not be. rv := "0" isList := meta.IsListType(obj) if isList { // the resourceVersion of list objects is ~now but won't return // an initial watch event rv, err = mapping.MetadataAccessor.ResourceVersion(obj) if err != nil { return err } } // print the current object if !isWatchOnly { if err := printer.PrintObj(obj, out); err != nil { return fmt.Errorf("unable to output the provided object: %v", err) } printer.AfterPrint(errOut, mapping.Resource) } // print watched changes w, err := r.Watch(rv) if err != nil { return err } first := true kubectl.WatchLoop(w, func(e watch.Event) error { if !isList && first { // drop the initial watch event in the single resource case first = false return nil } err := printer.PrintObj(e.Object, out) if err == nil { printer.AfterPrint(errOut, mapping.Resource) } return err }) return nil } r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)). NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces). FilenameParam(enforceNamespace, &options.FilenameOptions). SelectorParam(selector). ExportParam(export). ResourceTypeOrNameArgs(true, args...). ContinueOnError(). Latest(). Flatten(). Do() err = r.Err() if err != nil { return err } printer, generic, err := cmdutil.PrinterForCommand(cmd) if err != nil { return err } if generic { clientConfig, err := f.ClientConfig() if err != nil { return err } allErrs := []error{} singular := false infos, err := r.IntoSingular(&singular).Infos() if err != nil { if singular { return err } allErrs = append(allErrs, err) } // the outermost object will be converted to the output-version, but inner // objects can use their mappings version, err := cmdutil.OutputVersion(cmd, clientConfig.GroupVersion) if err != nil { return err } res := "" if len(infos) > 0 { res = infos[0].ResourceMapping().Resource } obj, err := resource.AsVersionedObject(infos, !singular, version, f.JSONEncoder()) if err != nil { return err } if err := printer.PrintObj(obj, out); err != nil { allErrs = append(allErrs, err) } printer.AfterPrint(errOut, res) return utilerrors.NewAggregate(allErrs) } allErrs := []error{} infos, err := r.Infos() if err != nil { allErrs = append(allErrs, err) } objs := make([]runtime.Object, len(infos)) for ix := range infos { objs[ix] = infos[ix].Object } sorting, err := cmd.Flags().GetString("sort-by") if err != nil { return err } var sorter *kubectl.RuntimeSort if len(sorting) > 0 && len(objs) > 1 { clientConfig, err := f.ClientConfig() if err != nil { return err } version, err := cmdutil.OutputVersion(cmd, clientConfig.GroupVersion) if err != nil { return err } for ix := range infos { objs[ix], err = infos[ix].Mapping.ConvertToVersion(infos[ix].Object, version) if err != nil { allErrs = append(allErrs, err) continue } } // TODO: questionable if sorter, err = kubectl.SortObjects(f.Decoder(true), objs, sorting); err != nil { return err } } // use the default printer for each object printer = nil var lastMapping *meta.RESTMapping w := kubectl.GetNewTabWriter(out) if mustPrintWithKinds(objs, infos, sorter, printAll) { showKind = true } for ix := range objs { var mapping *meta.RESTMapping var original runtime.Object if sorter != nil { mapping = infos[sorter.OriginalPosition(ix)].Mapping original = infos[sorter.OriginalPosition(ix)].Object } else { mapping = infos[ix].Mapping original = infos[ix].Object } if printer == nil || lastMapping == nil || mapping == nil || mapping.Resource != lastMapping.Resource { if printer != nil { w.Flush() printer.AfterPrint(errOut, lastMapping.Resource) } printer, err = f.PrinterForMapping(cmd, mapping, allNamespaces) if err != nil { allErrs = append(allErrs, err) continue } lastMapping = mapping } if resourcePrinter, found := printer.(*kubectl.HumanReadablePrinter); found { resourceName := resourcePrinter.GetResourceKind() if mapping != nil { if resourceName == "" { resourceName = mapping.Resource } if alias, ok := kubectl.ResourceShortFormFor(mapping.Resource); ok { resourceName = alias } else if resourceName == "" { resourceName = "none" } } else { resourceName = "none" } if showKind { resourcePrinter.EnsurePrintWithKind(resourceName) } if err := printer.PrintObj(original, w); err != nil { allErrs = append(allErrs, err) } continue } if err := printer.PrintObj(original, w); err != nil { allErrs = append(allErrs, err) continue } } w.Flush() if printer != nil { printer.AfterPrint(errOut, lastMapping.Resource) } return utilerrors.NewAggregate(allErrs) }
// RunProject contains all the necessary functionality for the OpenShift cli process command func RunProcess(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []string) error { templateName, valueArgs := "", []string{} for _, s := range args { isValue := strings.Contains(s, "=") switch { case isValue: valueArgs = append(valueArgs, s) case !isValue && len(templateName) == 0: templateName = s case !isValue && len(templateName) > 0: return kcmdutil.UsageError(cmd, "template name must be specified only once: %s", s) } } keys := sets.NewString() duplicatedKeys := sets.NewString() var flagValues []string if cmd.Flag("value").Changed { flagValues = kcmdutil.GetFlagStringSlice(cmd, "value") } for _, value := range flagValues { key := strings.Split(value, "=")[0] if keys.Has(key) { duplicatedKeys.Insert(key) } keys.Insert(key) } for _, value := range valueArgs { key := strings.Split(value, "=")[0] if keys.Has(key) { duplicatedKeys.Insert(key) } keys.Insert(key) } if len(duplicatedKeys) != 0 { return kcmdutil.UsageError(cmd, fmt.Sprintf("The following values were provided more than once: %s", strings.Join(duplicatedKeys.List(), ", "))) } filename := kcmdutil.GetFlagString(cmd, "filename") if len(templateName) == 0 && len(filename) == 0 { return kcmdutil.UsageError(cmd, "Must pass a filename or name of stored template") } if kcmdutil.GetFlagBool(cmd, "parameters") { for _, flag := range []string{"value", "labels", "output", "output-version", "raw", "template"} { if f := cmd.Flags().Lookup(flag); f != nil && f.Changed { return kcmdutil.UsageError(cmd, "The --parameters flag does not process the template, can't be used with --%v", flag) } } } namespace, explicit, err := f.DefaultNamespace() if err != nil { return err } mapper, typer := f.Object(false) client, _, err := f.Clients() if err != nil { return err } var ( objects []runtime.Object infos []*resource.Info ) mapping, err := mapper.RESTMapping(templateapi.Kind("Template")) if err != nil { return err } // When templateName is not empty, then we fetch the template from the // server, otherwise we require to set the `-f` parameter. if len(templateName) > 0 { var ( storedTemplate, rs string sourceNamespace string ok bool ) sourceNamespace, rs, storedTemplate, ok = parseNamespaceResourceName(templateName, namespace) if !ok { return fmt.Errorf("invalid argument %q", templateName) } if len(rs) > 0 && (rs != "template" && rs != "templates") { return fmt.Errorf("unable to process invalid resource %q", rs) } if len(storedTemplate) == 0 { return fmt.Errorf("invalid value syntax %q", templateName) } templateObj, err := client.Templates(sourceNamespace).Get(storedTemplate) if err != nil { if errors.IsNotFound(err) { return fmt.Errorf("template %q could not be found", storedTemplate) } return err } templateObj.CreationTimestamp = unversioned.Now() infos = append(infos, &resource.Info{Object: templateObj}) } else { infos, err = resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder()). NamespaceParam(namespace).RequireNamespace(). FilenameParam(explicit, false, filename). Do(). Infos() if err != nil { return err } } outputFormat := kcmdutil.GetFlagString(cmd, "output") if len(infos) > 1 { // in order to run validation on the input given to us by a user, we only support the processing // of one template in a list. For instance, we want to be able to fail when a user does not give // a parameter that the template wants or when they give a parameter the template doesn't need, // as this may indicate that they have mis-used `oc process`. This is much less complicated when // we process at most one template. fmt.Fprintf(out, "%d input templates found, but only the first will be processed", len(infos)) } obj, ok := infos[0].Object.(*templateapi.Template) if !ok { sourceName := filename if len(templateName) > 0 { sourceName = namespace + "/" + templateName } return fmt.Errorf("unable to parse %q, not a valid Template but %s\n", sourceName, reflect.TypeOf(infos[0].Object)) } // If 'parameters' flag is set it does not do processing but only print // the template parameters to console for inspection. if kcmdutil.GetFlagBool(cmd, "parameters") { return describe.PrintTemplateParameters(obj.Parameters, out) } if label := kcmdutil.GetFlagString(cmd, "labels"); len(label) > 0 { lbl, err := kubectl.ParseLabels(label) if err != nil { return fmt.Errorf("error parsing labels: %v\n", err) } if obj.ObjectLabels == nil { obj.ObjectLabels = make(map[string]string) } for key, value := range lbl { obj.ObjectLabels[key] = value } } // Override the values for the current template parameters // when user specify the --value if cmd.Flag("value").Changed { values := kcmdutil.GetFlagStringSlice(cmd, "value") if errs := injectUserVars(values, obj); errs != nil { return kerrors.NewAggregate(errs) } } if errs := injectUserVars(valueArgs, obj); errs != nil { return kerrors.NewAggregate(errs) } resultObj, err := client.TemplateConfigs(namespace).Create(obj) if err != nil { return fmt.Errorf("error processing the template %q: %v\n", obj.Name, err) } if outputFormat == "describe" { if s, err := (&describe.TemplateDescriber{ MetadataAccessor: meta.NewAccessor(), ObjectTyper: kapi.Scheme, ObjectDescriber: nil, }).DescribeTemplate(resultObj); err != nil { return fmt.Errorf("error describing %q: %v\n", obj.Name, err) } else { _, err := fmt.Fprintf(out, s) return err } } objects = append(objects, resultObj.Objects...) // Do not print the processed templates when asked to only show parameters or // describe. if kcmdutil.GetFlagBool(cmd, "parameters") || outputFormat == "describe" { return nil } p, _, err := kubectl.GetPrinter(outputFormat, "") if err != nil { return err } gv := mapping.GroupVersionKind.GroupVersion() version, err := kcmdutil.OutputVersion(cmd, &gv) if err != nil { return err } p = kubectl.NewVersionedPrinter(p, kapi.Scheme, version) // use generic output if kcmdutil.GetFlagBool(cmd, "raw") { for i := range objects { p.PrintObj(objects[i], out) } return nil } return p.PrintObj(&kapi.List{ ListMeta: unversioned.ListMeta{}, Items: objects, }, out) }
func (o *LogsOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error { containerName := cmdutil.GetFlagString(cmd, "container") switch len(args) { case 0: return cmdutil.UsageError(cmd, logsUsageStr) case 1: o.ResourceArg = args[0] case 2: if cmd.Flag("container").Changed { return cmdutil.UsageError(cmd, "only one of -c or an inline [CONTAINER] arg is allowed") } o.ResourceArg = args[0] containerName = args[1] default: return cmdutil.UsageError(cmd, logsUsageStr) } var err error o.Namespace, _, err = f.DefaultNamespace() if err != nil { return err } logOptions := &api.PodLogOptions{ Container: containerName, Follow: cmdutil.GetFlagBool(cmd, "follow"), Previous: cmdutil.GetFlagBool(cmd, "previous"), Timestamps: cmdutil.GetFlagBool(cmd, "timestamps"), } if sinceTime := cmdutil.GetFlagString(cmd, "since-time"); len(sinceTime) > 0 { t, err := api.ParseRFC3339(sinceTime, unversioned.Now) if err != nil { return err } logOptions.SinceTime = &t } if limit := cmdutil.GetFlagInt64(cmd, "limit-bytes"); limit != 0 { logOptions.LimitBytes = &limit } if tail := cmdutil.GetFlagInt64(cmd, "tail"); tail != -1 { logOptions.TailLines = &tail } if sinceSeconds := cmdutil.GetFlagDuration(cmd, "since"); sinceSeconds != 0 { // round up to the nearest second sec := int64(math.Ceil(float64(sinceSeconds) / float64(time.Second))) logOptions.SinceSeconds = &sec } o.Options = logOptions o.LogsForObject = f.LogsForObject o.ClientMapper = resource.ClientMapperFunc(f.ClientForMapping) o.Out = out mapper, typer := f.Object() decoder := f.Decoder(true) if o.Object == nil { infos, err := resource.NewBuilder(mapper, typer, o.ClientMapper, decoder). NamespaceParam(o.Namespace).DefaultNamespace(). ResourceNames("pods", o.ResourceArg). SingleResourceType(). Do().Infos() if err != nil { return err } if len(infos) != 1 { return errors.New("expected a resource") } o.Object = infos[0].Object } return nil }
// RunProject contains all the necessary functionality for the OpenShift cli process command func RunProcess(f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, args []string) error { storedTemplate := "" if len(args) > 0 { storedTemplate = args[0] } filename := kcmdutil.GetFlagString(cmd, "filename") if len(storedTemplate) == 0 && len(filename) == 0 { return kcmdutil.UsageError(cmd, "Must pass a filename or name of stored template") } namespace, err := f.DefaultNamespace() if err != nil { return err } mapper, typer := f.Object() client, _, err := f.Clients() if err != nil { return err } var ( objects []runtime.Object infos []*resource.Info mapping *meta.RESTMapping ) version, kind, err := mapper.VersionAndKindForResource("template") if mapping, err = mapper.RESTMapping(kind, version); err != nil { return err } // When storedTemplate is not empty, then we fetch the template from the // server, otherwise we require to set the `-f` parameter. if len(storedTemplate) > 0 { templateObj, err := client.Templates(namespace).Get(storedTemplate) if err != nil { if errors.IsNotFound(err) { return fmt.Errorf("template %q could not be found", storedTemplate) } return err } templateObj.CreationTimestamp = util.Now() infos = append(infos, &resource.Info{Object: templateObj}) } else { infos, err = resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()). NamespaceParam(namespace).RequireNamespace(). FilenameParam(filename). Do(). Infos() if err != nil { return err } } outputFormat := kcmdutil.GetFlagString(cmd, "output") for i := range infos { obj, ok := infos[i].Object.(*api.Template) if !ok { sourceName := filename if len(storedTemplate) > 0 { sourceName = namespace + "/" + storedTemplate } fmt.Fprintf(cmd.Out(), "unable to parse %q, not a valid Template but %s\n", sourceName, reflect.TypeOf(infos[i].Object)) continue } // If 'parameters' flag is set it does not do processing but only print // the template parameters to console for inspection. // If multiple templates are passed, this will print combined output for all // templates. if kcmdutil.GetFlagBool(cmd, "parameters") { if len(infos) > 1 { fmt.Fprintf(out, "\n%s:\n", obj.Name) } if err := describe.PrintTemplateParameters(obj.Parameters, out); err != nil { fmt.Fprintf(cmd.Out(), "error printing parameters for %q: %v\n", obj.Name, err) } continue } if label := kcmdutil.GetFlagString(cmd, "labels"); len(label) > 0 { lbl, err := kubectl.ParseLabels(label) if err != nil { fmt.Fprintf(cmd.Out(), "error parsing labels: %v\n", err) continue } if obj.ObjectLabels == nil { obj.ObjectLabels = make(map[string]string) } for key, value := range lbl { obj.ObjectLabels[key] = value } } // Override the values for the current template parameters // when user specify the --value if cmd.Flag("value").Changed { injectUserVars(cmd, obj) } resultObj, err := client.TemplateConfigs(namespace).Create(obj) if err != nil { fmt.Fprintf(cmd.Out(), "error processing the template %q: %v\n", obj.Name, err) continue } if outputFormat == "describe" { if s, err := (&describe.TemplateDescriber{ MetadataAccessor: meta.NewAccessor(), ObjectTyper: kapi.Scheme, ObjectDescriber: nil, }).DescribeTemplate(resultObj); err != nil { fmt.Fprintf(cmd.Out(), "error describing %q: %v\n", obj.Name, err) } else { fmt.Fprintf(out, s) } continue } objects = append(objects, resultObj.Objects...) } // Do not print the processed templates when asked to only show parameters or // describe. if kcmdutil.GetFlagBool(cmd, "parameters") || outputFormat == "describe" { return nil } p, _, err := kubectl.GetPrinter(outputFormat, "") if err != nil { return err } p = kubectl.NewVersionedPrinter(p, kapi.Scheme, kcmdutil.OutputVersion(cmd, mapping.APIVersion)) // use generic output if kcmdutil.GetFlagBool(cmd, "raw") { for i := range objects { p.PrintObj(objects[i], out) } return nil } return p.PrintObj(&kapi.List{ ListMeta: kapi.ListMeta{}, Items: objects, }, out) }