// Complete turns a partially defined PruneBuildsOptions into a solvent structure // which can be validated and used for pruning builds. func (o *PruneBuildsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []string, out io.Writer) error { if len(args) > 0 { return kcmdutil.UsageError(cmd, "no arguments are allowed to this command") } o.Out = out osClient, _, err := f.Clients() if err != nil { return err } o.Client = osClient buildConfigList, err := osClient.BuildConfigs(kapi.NamespaceAll).List(kapi.ListOptions{}) if err != nil { return err } buildConfigs := []*buildapi.BuildConfig{} for i := range buildConfigList.Items { buildConfigs = append(buildConfigs, &buildConfigList.Items[i]) } buildList, err := osClient.Builds(kapi.NamespaceAll).List(kapi.ListOptions{}) if err != nil { return err } builds := []*buildapi.Build{} for i := range buildList.Items { builds = append(builds, &buildList.Items[i]) } options := prune.PrunerOptions{ KeepYoungerThan: o.KeepYoungerThan, Orphans: o.Orphans, KeepComplete: o.KeepComplete, KeepFailed: o.KeepFailed, BuildConfigs: buildConfigs, Builds: builds, } o.Pruner = prune.NewPruner(options) return nil }
// Run contains all the necessary functionality for the OpenShift cli prune builds command. func (o PruneBuildsOptions) Run() error { buildConfigList, err := o.OSClient.BuildConfigs(o.Namespace).List(kapi.ListOptions{}) if err != nil { return err } buildConfigs := []*buildapi.BuildConfig{} for i := range buildConfigList.Items { buildConfigs = append(buildConfigs, &buildConfigList.Items[i]) } buildList, err := o.OSClient.Builds(o.Namespace).List(kapi.ListOptions{}) if err != nil { return err } builds := []*buildapi.Build{} for i := range buildList.Items { builds = append(builds, &buildList.Items[i]) } options := prune.PrunerOptions{ KeepYoungerThan: o.KeepYoungerThan, Orphans: o.Orphans, KeepComplete: o.KeepComplete, KeepFailed: o.KeepFailed, BuildConfigs: buildConfigs, Builds: builds, } pruner := prune.NewPruner(options) w := tabwriter.NewWriter(o.Out, 10, 4, 3, ' ', 0) defer w.Flush() buildDeleter := &describingBuildDeleter{w: w} if o.Confirm { buildDeleter.delegate = prune.NewBuildDeleter(o.OSClient) } else { fmt.Fprintln(os.Stderr, "Dry run enabled - no modifications will be made. Add --confirm to remove builds") } return pruner.Prune(buildDeleter) }