|
| 1 | +package index |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/sirupsen/logrus" |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + "github.com/operator-framework/operator-registry/pkg/containertools" |
| 10 | + "github.com/operator-framework/operator-registry/pkg/lib/indexer" |
| 11 | +) |
| 12 | + |
| 13 | +func newIndexPruneStrandedCmd() *cobra.Command { |
| 14 | + indexCmd := &cobra.Command{ |
| 15 | + Use: "prune-stranded", |
| 16 | + Short: "prune an index of stranded bundles", |
| 17 | + Long: `prune an index of stranded bundles - bundles that are not associated with a particular package`, |
| 18 | + |
| 19 | + PreRunE: func(cmd *cobra.Command, args []string) error { |
| 20 | + if debug, _ := cmd.Flags().GetBool("debug"); debug { |
| 21 | + logrus.SetLevel(logrus.DebugLevel) |
| 22 | + } |
| 23 | + return nil |
| 24 | + }, |
| 25 | + |
| 26 | + RunE: runIndexPruneStrandedCmdFunc, |
| 27 | + } |
| 28 | + |
| 29 | + indexCmd.Flags().Bool("debug", false, "enable debug logging") |
| 30 | + indexCmd.Flags().Bool("generate", false, "if enabled, just creates the dockerfile and saves it to local disk") |
| 31 | + indexCmd.Flags().StringP("out-dockerfile", "d", "", "if generating the dockerfile, this flag is used to (optionally) specify a dockerfile name") |
| 32 | + indexCmd.Flags().StringP("from-index", "f", "", "index to prune") |
| 33 | + if err := indexCmd.MarkFlagRequired("from-index"); err != nil { |
| 34 | + logrus.Panic("Failed to set required `from-index` flag for `index prune-stranded`") |
| 35 | + } |
| 36 | + indexCmd.Flags().StringP("binary-image", "i", "", "container image for on-image `opm` command") |
| 37 | + indexCmd.Flags().StringP("container-tool", "c", "podman", "tool to interact with container images (save, build, etc.). One of: [docker, podman]") |
| 38 | + indexCmd.Flags().StringP("tag", "t", "", "custom tag for container image being built") |
| 39 | + |
| 40 | + if err := indexCmd.Flags().MarkHidden("debug"); err != nil { |
| 41 | + logrus.Panic(err.Error()) |
| 42 | + } |
| 43 | + |
| 44 | + return indexCmd |
| 45 | + |
| 46 | +} |
| 47 | + |
| 48 | +func runIndexPruneStrandedCmdFunc(cmd *cobra.Command, args []string) error { |
| 49 | + generate, err := cmd.Flags().GetBool("generate") |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + outDockerfile, err := cmd.Flags().GetString("out-dockerfile") |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + fromIndex, err := cmd.Flags().GetString("from-index") |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + binaryImage, err := cmd.Flags().GetString("binary-image") |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + containerTool, err := cmd.Flags().GetString("container-tool") |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + if containerTool == "none" { |
| 75 | + return fmt.Errorf("none is not a valid container-tool for index prune") |
| 76 | + } |
| 77 | + |
| 78 | + tag, err := cmd.Flags().GetString("tag") |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + logger := logrus.WithFields(logrus.Fields{}) |
| 84 | + |
| 85 | + logger.Info("pruning stranded bundles from the index") |
| 86 | + |
| 87 | + indexPruner := indexer.NewIndexStrandedPruner(containertools.NewContainerTool(containerTool, containertools.PodmanTool), logger) |
| 88 | + |
| 89 | + request := indexer.PruneStrandedFromIndexRequest{ |
| 90 | + Generate: generate, |
| 91 | + FromIndex: fromIndex, |
| 92 | + BinarySourceImage: binaryImage, |
| 93 | + OutDockerfile: outDockerfile, |
| 94 | + Tag: tag, |
| 95 | + } |
| 96 | + |
| 97 | + err = indexPruner.PruneStrandedFromIndex(request) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
0 commit comments