github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/cmd/kcfi/cf_images.go (about)

     1  /*
     2  Copyright The Codefresh Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  	//"io/ioutil"
    24  	"path/filepath"
    25  	//"github.com/pkg/errors"
    26  	"github.com/spf13/cobra"
    27  	//"sigs.k8s.io/yaml"
    28  
    29  	"github.com/codefresh-io/kcfi/pkg/action"
    30  	c "github.com/codefresh-io/kcfi/pkg/config"
    31  	"helm.sh/helm/v3/cmd/helm/require"
    32  	"helm.sh/helm/v3/pkg/cli/values"
    33  	"helm.sh/helm/v3/pkg/getter"
    34  )
    35  
    36  const cfImagesDesc = `
    37  Commmand to push images to private registry
    38  Push whole release with images list defined in config file or by --image-list parameter:
    39     kcfi images push [--images-list <images-list-file>] [-c|--config /path/to/config.yaml] [options]
    40  
    41  Push single image
    42    kcfi images push [-c|--config /path/to/config.yaml] [options] repo/image:tag [repo/image:tag] ... 
    43  `
    44  
    45  func cfImagesCmd(out io.Writer) *cobra.Command {
    46  	cmdPush := "push"
    47  
    48  	var configFileName, imagesListFile, cfRegistrySecret string
    49  	var registry, registryUsername, registryPassword string
    50  
    51  	cmd := &cobra.Command{
    52  		Use:     "images",
    53  		Short:   "push images to private registry",
    54  		Long:    cfImagesDesc,
    55  		Aliases: []string{"image", "private-registry", "docker"},
    56  		Args:    require.MinimumNArgs(1),
    57  		RunE: func(cmd *cobra.Command, args []string) error {
    58  			if len(args) == 0 || !(args[0] == cmdPush) {
    59  				return cmd.Usage()
    60  			}
    61  
    62  			valueOpts := &values.Options{}
    63  			// set baseDir
    64  			var baseDir string
    65  			if configFileName != "" {
    66  				if fInfo, err := os.Stat(configFileName); err == nil && !fInfo.IsDir() {
    67  					debug("Using config file: %s", configFileName)
    68  					valueOpts.ValueFiles = []string{configFileName}
    69  					baseDir = filepath.Dir(configFileName)
    70  				} else {
    71  					return fmt.Errorf("%s is not a valid file", configFileName)
    72  				}
    73  			}
    74  			if cfRegistrySecret != "" {
    75  				valueOpts.Values = append(valueOpts.Values, fmt.Sprintf("%s=%s", c.KeyImagesCodefreshRegistrySa, cfRegistrySecret))
    76  			}
    77  			if registry != "" {
    78  				valueOpts.Values = append(valueOpts.Values, fmt.Sprintf("%s=%s", c.KeyImagesPrivateRegistryAddress, registry))
    79  			}
    80  			if registryUsername != "" {
    81  				valueOpts.Values = append(valueOpts.Values, fmt.Sprintf("%s=%s", c.KeyImagesPrivateRegistryUsername, registryUsername))
    82  			}
    83  			if registryPassword != "" {
    84  				valueOpts.Values = append(valueOpts.Values, fmt.Sprintf("%s=%s", c.KeyImagesPrivateRegistryPassword, registryPassword))
    85  			}
    86  			config, err := valueOpts.MergeValues(getter.Providers{})
    87  			if err != nil {
    88  				return err
    89  			}
    90  			config[c.KeyBaseDir] = baseDir
    91  
    92  			imagesPusher, err := action.NewImagesPusherFromConfig(config)
    93  			if err != nil {
    94  				return err
    95  			}
    96  
    97  			var imagesList []string
    98  			if len(args[1:]) > 0 {
    99  				imagesList = args[1:]
   100  			} else if imagesListFile != "" {
   101  				imagesList, err = action.ReadListFile(imagesListFile)
   102  				if err != nil {
   103  					return err
   104  				}
   105  			} else {
   106  				imagesList = imagesPusher.ImagesList
   107  			}
   108  
   109  			return imagesPusher.Run(imagesList)
   110  		},
   111  	}
   112  
   113  	f := cmd.Flags()
   114  	f.StringVarP(&configFileName, flagConfig, "c", "", "config file")
   115  	f.StringVar(&imagesListFile, "images-list", "", "file with list of images to push")
   116  	f.StringVar(&cfRegistrySecret, "codefresh-registry-secret", "", "file with Codefresh registry secret (sa.json)")
   117  	f.StringVar(&registry, "registry", "", "registry address")
   118  	f.StringVar(&registryUsername, "user", "", "registry username")
   119  	f.StringVar(&registryPassword, "password", "", "registry password")
   120  
   121  	origHelpFunc := cmd.HelpFunc()
   122  	cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
   123  		hideKubeFlags(cmd)
   124  		hideHelmCommonFlags(cmd)
   125  		origHelpFunc(cmd, args)
   126  	})
   127  	return cmd
   128  }