github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/internal/commands/manifest_annotate.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/spf13/cobra"
     7  
     8  	"github.com/buildpacks/pack/pkg/client"
     9  	"github.com/buildpacks/pack/pkg/logging"
    10  )
    11  
    12  // ManifestAnnotateFlags define flags provided to the ManifestAnnotate
    13  type ManifestAnnotateFlags struct {
    14  	os, arch, variant string
    15  	annotations       map[string]string
    16  }
    17  
    18  // ManifestAnnotate modifies a manifest list and updates the platform information within the index for an image in the list.
    19  func ManifestAnnotate(logger logging.Logger, pack PackClient) *cobra.Command {
    20  	var flags ManifestAnnotateFlags
    21  	cmd := &cobra.Command{
    22  		Use:     "annotate [OPTIONS] <manifest-list> <manifest> [flags]",
    23  		Args:    cobra.MatchAll(cobra.ExactArgs(2), cobra.OnlyValidArgs),
    24  		Short:   "Add or update information about an entry in a manifest list.",
    25  		Example: `pack manifest annotate my-image-index my-image:some-arch --arch some-other-arch`,
    26  		RunE: logError(logger, func(cmd *cobra.Command, args []string) (err error) {
    27  			if err = validateManifestAnnotateFlags(flags); err != nil {
    28  				return err
    29  			}
    30  			return pack.AnnotateManifest(cmd.Context(), client.ManifestAnnotateOptions{
    31  				IndexRepoName: args[0],
    32  				RepoName:      args[1],
    33  				OS:            flags.os,
    34  				OSArch:        flags.arch,
    35  				OSVariant:     flags.variant,
    36  				Annotations:   flags.annotations,
    37  			})
    38  		}),
    39  	}
    40  
    41  	cmd.Flags().StringVar(&flags.os, "os", "", "Set the OS")
    42  	cmd.Flags().StringVar(&flags.arch, "arch", "", "Set the architecture")
    43  	cmd.Flags().StringVar(&flags.variant, "variant", "", "Set the architecture variant")
    44  	cmd.Flags().StringToStringVar(&flags.annotations, "annotations", make(map[string]string, 0), "Set an `annotation` for the specified image")
    45  
    46  	AddHelpFlag(cmd, "annotate")
    47  	return cmd
    48  }
    49  
    50  func validateManifestAnnotateFlags(flags ManifestAnnotateFlags) error {
    51  	if flags.os == "" && flags.arch == "" && flags.variant == "" && len(flags.annotations) == 0 {
    52  		return fmt.Errorf("one of --os, --arch, or --variant is required")
    53  	}
    54  	return nil
    55  }