|
| 1 | +// Copyright 2019 The Kubernetes Authors. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package publish |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "sigs.k8s.io/kustomize/api/ifc" |
| 11 | + "sigs.k8s.io/kustomize/api/resource" |
| 12 | + "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile" |
| 13 | + "sigs.k8s.io/kustomize/kyaml/filesys" |
| 14 | +) |
| 15 | + |
| 16 | +type publishOptions struct { |
| 17 | + registry string |
| 18 | + noVerify bool |
| 19 | +} |
| 20 | + |
| 21 | +// NewCmdEdit returns an instance of 'edit' subcommand. |
| 22 | +func NewCmdPublish( |
| 23 | + fSys filesys.FileSystem, v ifc.Validator, rf *resource.Factory, |
| 24 | +) *cobra.Command { |
| 25 | + var o publishOptions |
| 26 | + |
| 27 | + cmd := &cobra.Command{ |
| 28 | + Use: "publish", |
| 29 | + Short: "Publishes a kustomization resource to an OCI registry", |
| 30 | + Long: "", |
| 31 | + Example: ` |
| 32 | + publish <registry> |
| 33 | +`, |
| 34 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 35 | + err := o.Validate(args) |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + return o.RunPublish(fSys) |
| 40 | + }, |
| 41 | + |
| 42 | + Args: cobra.MinimumNArgs(1), |
| 43 | + } |
| 44 | + cmd.Flags().BoolVar(&o.noVerify, "no-verify", false, |
| 45 | + "skip validation for resources", |
| 46 | + ) |
| 47 | + return cmd |
| 48 | +} |
| 49 | + |
| 50 | +// Validate validates addResource command. |
| 51 | +func (o *publishOptions) Validate(args []string) error { |
| 52 | + if len(args) == 0 { |
| 53 | + return errors.New("must specify a registry") |
| 54 | + } |
| 55 | + o.registry = args[0] |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +// RunAddResource runs addResource command (do real work). |
| 60 | +func (o *publishOptions) RunPublish(fSys filesys.FileSystem) error { |
| 61 | + mf, err := kustfile.NewKustomizationFile(fSys) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + m, err := mf.Read() |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + for _, r := range m.Resources { |
| 71 | + println(r) |
| 72 | + } |
| 73 | + |
| 74 | + return nil |
| 75 | +} |
0 commit comments