| 
 | 1 | +package cmd  | 
 | 2 | + | 
 | 3 | +import (  | 
 | 4 | +	"fmt"  | 
 | 5 | +	"strings"  | 
 | 6 | + | 
 | 7 | +	"github.com/spf13/cobra"  | 
 | 8 | + | 
 | 9 | +	operatorsv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/v1"  | 
 | 10 | + | 
 | 11 | +	"github.com/operator-framework/kubectl-operator/internal/cmd/internal/log"  | 
 | 12 | +	"github.com/operator-framework/kubectl-operator/internal/pkg/action"  | 
 | 13 | +)  | 
 | 14 | + | 
 | 15 | +var (  | 
 | 16 | +	// output helpers for the describe subcommand  | 
 | 17 | +	pkgHdr  = asHeader("Package")  | 
 | 18 | +	repoHdr = asHeader("Repository")  | 
 | 19 | +	catHdr  = asHeader("Catalog")  | 
 | 20 | +	chHdr   = asHeader("Channels")  | 
 | 21 | +	imHdr   = asHeader("Install Modes")  | 
 | 22 | +	sdHdr   = asHeader("Description")  | 
 | 23 | +	ldHdr   = asHeader("Long Description")  | 
 | 24 | + | 
 | 25 | +	repoAnnot = "repository"  | 
 | 26 | +	descAnnot = "description"  | 
 | 27 | +)  | 
 | 28 | + | 
 | 29 | +func newOperatorDescribeCmd(cfg *action.Configuration) *cobra.Command {  | 
 | 30 | +	l := action.NewOperatorListAvailable(cfg)  | 
 | 31 | +	// receivers for cmdline flags  | 
 | 32 | +	var channel string  | 
 | 33 | +	var longDescription bool  | 
 | 34 | + | 
 | 35 | +	cmd := &cobra.Command{  | 
 | 36 | +		Use:   "describe <operator>",  | 
 | 37 | +		Short: "Describe an operator",  | 
 | 38 | +		Args:  cobra.ExactArgs(1),  | 
 | 39 | +		Run: func(cmd *cobra.Command, args []string) {  | 
 | 40 | +			// the operator to show details about, provided by the user  | 
 | 41 | +			l.Package = args[0]  | 
 | 42 | + | 
 | 43 | +			// Find the package manifest and package channel for the operator  | 
 | 44 | +			pms, err := l.Run(cmd.Context())  | 
 | 45 | +			if err != nil {  | 
 | 46 | +				log.Fatalf("failed to find operator: %v", err)  | 
 | 47 | +			}  | 
 | 48 | + | 
 | 49 | +			// we only expect one item because describe always searches  | 
 | 50 | +			// for a specific operator by name  | 
 | 51 | +			pm := &pms[0]  | 
 | 52 | + | 
 | 53 | +			// If the user asked for a channel, look for that  | 
 | 54 | +			if channel == "" {  | 
 | 55 | +				channel = pm.Status.DefaultChannel  | 
 | 56 | +			}  | 
 | 57 | + | 
 | 58 | +			pc, err := getPackageChannel(channel, pm)  | 
 | 59 | +			if err != nil {  | 
 | 60 | +				// the requested channel doesn't exist  | 
 | 61 | +				log.Fatalf("failed to find channel for operator: %v", err)  | 
 | 62 | +			}  | 
 | 63 | + | 
 | 64 | +			// prepare what we want to print to the console  | 
 | 65 | +			out := make([]string, 0)  | 
 | 66 | + | 
 | 67 | +			// Starting adding data to our output.  | 
 | 68 | +			out = append(out,  | 
 | 69 | +				// package  | 
 | 70 | +				pkgHdr+fmt.Sprintf("%s %s (by %s)\n\n",  | 
 | 71 | +					pc.CurrentCSVDesc.DisplayName,  | 
 | 72 | +					pc.CurrentCSVDesc.Version,  | 
 | 73 | +					pc.CurrentCSVDesc.Provider.Name),  | 
 | 74 | +				// repo  | 
 | 75 | +				repoHdr+fmt.Sprintf("%s\n\n",  | 
 | 76 | +					pc.CurrentCSVDesc.Annotations[repoAnnot]),  | 
 | 77 | +				// catalog  | 
 | 78 | +				catHdr+fmt.Sprintf("%s\n\n", pm.Status.CatalogSourceDisplayName),  | 
 | 79 | +				// available channels  | 
 | 80 | +				chHdr+fmt.Sprintf("%s\n\n",  | 
 | 81 | +					strings.Join(getAvailableChannelsWithMarkers(channel, pm), "\n")),  | 
 | 82 | +				// install modes  | 
 | 83 | +				imHdr+fmt.Sprintf("%s\n\n",  | 
 | 84 | +					strings.Join(getSupportedInstallModes(pc), "\n")),  | 
 | 85 | +				// description  | 
 | 86 | +				sdHdr+fmt.Sprintf("%s\n",  | 
 | 87 | +					pc.CurrentCSVDesc.Annotations[descAnnot]),  | 
 | 88 | +			)  | 
 | 89 | + | 
 | 90 | +			// if the user requested a long description, add it to the output as well  | 
 | 91 | +			if longDescription {  | 
 | 92 | +				out = append(out,  | 
 | 93 | +					"\n"+ldHdr+pm.Status.Channels[0].CurrentCSVDesc.LongDescription)  | 
 | 94 | +			}  | 
 | 95 | + | 
 | 96 | +			// finally, print operator information to the console  | 
 | 97 | +			for _, v := range out {  | 
 | 98 | +				fmt.Print(v)  | 
 | 99 | +			}  | 
 | 100 | + | 
 | 101 | +		},  | 
 | 102 | +	}  | 
 | 103 | + | 
 | 104 | +	// add flags to the flagset for this command.  | 
 | 105 | +	cmd.Flags().StringVarP(&channel, "channel", "c", "", "channel")  | 
 | 106 | +	cmd.Flags().BoolVarP(&longDescription, "with-long-description", "L", false, "long description")  | 
 | 107 | + | 
 | 108 | +	return cmd  | 
 | 109 | +}  | 
 | 110 | + | 
 | 111 | +// asHeader returns the string with "header bars" for displaying in  | 
 | 112 | +// plain text cases.  | 
 | 113 | +func asHeader(s string) string {  | 
 | 114 | +	return fmt.Sprintf("== %s ==\n", s)  | 
 | 115 | +}  | 
 | 116 | + | 
 | 117 | +// getPackageChannel returns the package channel specified, or the default if none was specified.  | 
 | 118 | +func getPackageChannel(channel string, pm *operatorsv1.PackageManifest) (*operatorsv1.PackageChannel, error) {  | 
 | 119 | +	var packageChannel *operatorsv1.PackageChannel  | 
 | 120 | +	for _, ch := range pm.Status.Channels {  | 
 | 121 | +		ch := ch  | 
 | 122 | +		if ch.Name == channel {  | 
 | 123 | +			packageChannel = &ch  | 
 | 124 | +		}  | 
 | 125 | +	}  | 
 | 126 | +	if packageChannel == nil {  | 
 | 127 | +		return nil, fmt.Errorf("channel %q does not exist for package %q", channel, pm.GetName())  | 
 | 128 | +	}  | 
 | 129 | +	return packageChannel, nil  | 
 | 130 | +}  | 
 | 131 | + | 
 | 132 | +// GetSupportedInstallModes returns a string slice representation of install mode  | 
 | 133 | +// objects the operator supports.  | 
 | 134 | +func getSupportedInstallModes(pc *operatorsv1.PackageChannel) []string {  | 
 | 135 | +	supportedInstallModes := make([]string, 1)  | 
 | 136 | +	for _, imode := range pc.CurrentCSVDesc.InstallModes {  | 
 | 137 | +		if imode.Supported {  | 
 | 138 | +			supportedInstallModes = append(supportedInstallModes, string(imode.Type))  | 
 | 139 | +		}  | 
 | 140 | +	}  | 
 | 141 | + | 
 | 142 | +	return supportedInstallModes  | 
 | 143 | +}  | 
 | 144 | + | 
 | 145 | +// getAvailableChannelsWithMarkers parses all available package channels for a package manifest  | 
 | 146 | +// and returns those channel names with indicators for pretty-printing whether they are shown  | 
 | 147 | +// or the default channel  | 
 | 148 | +func getAvailableChannelsWithMarkers(channel string, pm *operatorsv1.PackageManifest) []string {  | 
 | 149 | +	channels := make([]string, len(pm.Status.Channels))  | 
 | 150 | +	for i, ch := range pm.Status.Channels {  | 
 | 151 | +		n := ch.Name  | 
 | 152 | +		if ch.IsDefaultChannel(*pm) {  | 
 | 153 | +			n += " (default)"  | 
 | 154 | +		}  | 
 | 155 | +		if channel == ch.Name {  | 
 | 156 | +			n += " (shown)"  | 
 | 157 | +		}  | 
 | 158 | +		channels[i] = n  | 
 | 159 | +	}  | 
 | 160 | + | 
 | 161 | +	return channels  | 
 | 162 | +}  | 
0 commit comments