|
| 1 | +// SPDX-FileCopyrightText: 2020 SAP SE or an SAP affiliate company and Gardener contributors. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package set |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + |
| 14 | + cdvalidation "github.com/gardener/component-spec/bindings-go/apis/v2/validation" |
| 15 | + "github.com/gardener/component-spec/bindings-go/ctf" |
| 16 | + "github.com/go-logr/logr" |
| 17 | + "github.com/mandelsoft/vfs/pkg/osfs" |
| 18 | + "github.com/mandelsoft/vfs/pkg/vfs" |
| 19 | + "github.com/spf13/cobra" |
| 20 | + "github.com/spf13/pflag" |
| 21 | + "sigs.k8s.io/yaml" |
| 22 | + |
| 23 | + "github.com/gardener/component-cli/pkg/componentarchive" |
| 24 | + "github.com/gardener/component-cli/pkg/logger" |
| 25 | +) |
| 26 | + |
| 27 | +// Options defines the options that are used to add resources to a component descriptor |
| 28 | +type Options struct { |
| 29 | + componentarchive.BuilderOptions |
| 30 | +} |
| 31 | + |
| 32 | +// NewSetCommand creates a command to add additional resources to a component descriptor. |
| 33 | +func NewSetCommand(ctx context.Context) *cobra.Command { |
| 34 | + opts := &Options{} |
| 35 | + cmd := &cobra.Command{ |
| 36 | + Use: "set COMPONENT_ARCHIVE_PATH [options...]", |
| 37 | + Args: cobra.MinimumNArgs(1), |
| 38 | + Short: "set some component descriptor properties", |
| 39 | + Long: ` |
| 40 | +the set command sets some component descriptor properies like the component name and/or version. |
| 41 | +
|
| 42 | +The component archive can be specified by the first argument, the flag "--archive" or as env var "COMPONENT_ARCHIVE_PATH". |
| 43 | +The component archive is expected to be a filesystem archive. |
| 44 | +`, |
| 45 | + Run: func(cmd *cobra.Command, args []string) { |
| 46 | + if err := opts.Complete(args); err != nil { |
| 47 | + fmt.Println(err.Error()) |
| 48 | + os.Exit(1) |
| 49 | + } |
| 50 | + |
| 51 | + if err := opts.Run(ctx, logger.Log, osfs.New()); err != nil { |
| 52 | + fmt.Println(err.Error()) |
| 53 | + os.Exit(1) |
| 54 | + } |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + opts.AddFlags(cmd.Flags()) |
| 59 | + |
| 60 | + return cmd |
| 61 | +} |
| 62 | + |
| 63 | +func (o *Options) Run(ctx context.Context, log logr.Logger, fs vfs.FileSystem) error { |
| 64 | + compDescFilePath := filepath.Join(o.ComponentArchivePath, ctf.ComponentDescriptorFileName) |
| 65 | + |
| 66 | + o.Modify = true |
| 67 | + archive, err := o.BuilderOptions.Build(fs) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + if len(o.Name) != 0 { |
| 73 | + archive.ComponentDescriptor.Name = o.Name |
| 74 | + } |
| 75 | + if len(o.Version) != 0 { |
| 76 | + archive.ComponentDescriptor.Version = o.Version |
| 77 | + } |
| 78 | + |
| 79 | + if err := cdvalidation.Validate(archive.ComponentDescriptor); err != nil { |
| 80 | + return fmt.Errorf("invalid component descriptor: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + data, err := yaml.Marshal(archive.ComponentDescriptor) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("unable to encode component descriptor: %w", err) |
| 86 | + } |
| 87 | + if err := vfs.WriteFile(fs, compDescFilePath, data, 0664); err != nil { |
| 88 | + return fmt.Errorf("unable to write modified comonent descriptor: %w", err) |
| 89 | + } |
| 90 | + log.V(2).Info("Successfully changed component descriptor") |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +func (o *Options) Complete(args []string) error { |
| 95 | + if len(args) == 0 { |
| 96 | + return errors.New("at least a component archive path argument has to be defined") |
| 97 | + } |
| 98 | + o.BuilderOptions.ComponentArchivePath = args[0] |
| 99 | + o.BuilderOptions.Default() |
| 100 | + |
| 101 | + return o.validate() |
| 102 | +} |
| 103 | + |
| 104 | +func (o *Options) validate() error { |
| 105 | + return o.BuilderOptions.Validate() |
| 106 | +} |
| 107 | + |
| 108 | +func (o *Options) AddFlags(fs *pflag.FlagSet) { |
| 109 | + o.BuilderOptions.AddFlags(fs) |
| 110 | +} |
0 commit comments