|
| 1 | +package template |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | + "github.com/spf13/cobra" |
| 12 | + |
| 13 | + "github.com/operator-framework/operator-registry/alpha/action" |
| 14 | + "github.com/operator-framework/operator-registry/alpha/action/migrations" |
| 15 | + "github.com/operator-framework/operator-registry/alpha/declcfg" |
| 16 | + "github.com/operator-framework/operator-registry/alpha/template/substitutes" |
| 17 | + "github.com/operator-framework/operator-registry/cmd/opm/internal/util" |
| 18 | +) |
| 19 | + |
| 20 | +func newSubstitutesForTemplateCmd() *cobra.Command { |
| 21 | + var ( |
| 22 | + migrateLevel string |
| 23 | + ) |
| 24 | + |
| 25 | + cmd := &cobra.Command{ |
| 26 | + Use: "substitutes [FILE]", |
| 27 | + Short: `Generate a file-based catalog from a single 'substitutes template' file |
| 28 | +When FILE is '-' or not provided, the template is read from standard input`, |
| 29 | + Long: `Generate a file-based catalog from a single 'substitutes template' file |
| 30 | +When FILE is '-' or not provided, the template is read from standard input`, |
| 31 | + Args: cobra.MaximumNArgs(1), |
| 32 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 33 | + // Handle different input argument types |
| 34 | + // When no arguments or "-" is passed to the command, |
| 35 | + // assume input is coming from stdin |
| 36 | + // Otherwise open the file passed to the command |
| 37 | + data, source, err := util.OpenFileOrStdin(cmd, args) |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + defer data.Close() |
| 42 | + |
| 43 | + var write func(declcfg.DeclarativeConfig, io.Writer) error |
| 44 | + output, err := cmd.Flags().GetString("output") |
| 45 | + if err != nil { |
| 46 | + log.Fatalf("unable to determine output format") |
| 47 | + } |
| 48 | + switch output { |
| 49 | + case "json": |
| 50 | + write = declcfg.WriteJSON |
| 51 | + case "yaml": |
| 52 | + write = declcfg.WriteYAML |
| 53 | + case "mermaid": |
| 54 | + write = func(cfg declcfg.DeclarativeConfig, writer io.Writer) error { |
| 55 | + mermaidWriter := declcfg.NewMermaidWriter() |
| 56 | + return mermaidWriter.WriteChannels(cfg, writer) |
| 57 | + } |
| 58 | + default: |
| 59 | + return fmt.Errorf("invalid output format %q", output) |
| 60 | + } |
| 61 | + |
| 62 | + // The bundle loading impl is somewhat verbose, even on the happy path, |
| 63 | + // so discard all logrus default logger logs. Any important failures will be |
| 64 | + // returned from template.Render and logged as fatal errors. |
| 65 | + logrus.SetOutput(io.Discard) |
| 66 | + |
| 67 | + reg, err := util.CreateCLIRegistry(cmd) |
| 68 | + if err != nil { |
| 69 | + log.Fatalf("creating containerd registry: %v", err) |
| 70 | + } |
| 71 | + defer func() { |
| 72 | + _ = reg.Destroy() |
| 73 | + }() |
| 74 | + |
| 75 | + var m *migrations.Migrations |
| 76 | + if migrateLevel != "" { |
| 77 | + m, err = migrations.NewMigrations(migrateLevel) |
| 78 | + if err != nil { |
| 79 | + log.Fatal(err) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + template := substitutes.Template{ |
| 84 | + RenderBundle: func(ctx context.Context, ref string) (*declcfg.DeclarativeConfig, error) { |
| 85 | + renderer := action.Render{ |
| 86 | + Refs: []string{ref}, |
| 87 | + Registry: reg, |
| 88 | + AllowedRefMask: action.RefBundleImage, |
| 89 | + Migrations: m, |
| 90 | + } |
| 91 | + return renderer.Run(ctx) |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + out, err := template.Render(cmd.Context(), data) |
| 96 | + if err != nil { |
| 97 | + log.Fatalf("substitutes %q: %v", source, err) |
| 98 | + } |
| 99 | + |
| 100 | + if out != nil { |
| 101 | + if err := write(*out, os.Stdout); err != nil { |
| 102 | + log.Fatal(err) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return nil |
| 107 | + }, |
| 108 | + } |
| 109 | + |
| 110 | + cmd.Flags().StringVar(&migrateLevel, "migrate-level", "", "Name of the last migration to run (default: none)\n"+migrations.HelpText()) |
| 111 | + |
| 112 | + return cmd |
| 113 | +} |
0 commit comments