github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/sealer/cmd/login.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmd
    16  
    17  import (
    18  	"os"
    19  
    20  	"github.com/spf13/cobra"
    21  
    22  	"github.com/alibaba/sealer/logger"
    23  	"github.com/alibaba/sealer/pkg/image"
    24  )
    25  
    26  type LoginFlag struct {
    27  	RegistryURL      string
    28  	RegistryUsername string
    29  	RegistryPasswd   string
    30  }
    31  
    32  var loginConfig *LoginFlag
    33  
    34  var loginCmd = &cobra.Command{
    35  	Use:     "login",
    36  	Short:   "login image repository",
    37  	Example: `sealer login registry.cn-qingdao.aliyuncs.com -u [username] -p [password]`,
    38  	Args:    cobra.ExactArgs(1),
    39  	RunE: func(cmd *cobra.Command, args []string) error {
    40  		imgSvc, err := image.NewImageService()
    41  		if err != nil {
    42  			return err
    43  		}
    44  
    45  		return imgSvc.Login(args[0], loginConfig.RegistryUsername, loginConfig.RegistryPasswd)
    46  	},
    47  }
    48  
    49  func init() {
    50  	loginConfig = &LoginFlag{}
    51  	rootCmd.AddCommand(loginCmd)
    52  	loginCmd.Flags().StringVarP(&loginConfig.RegistryUsername, "username", "u", "", "user name for login registry")
    53  	loginCmd.Flags().StringVarP(&loginConfig.RegistryPasswd, "passwd", "p", "", "password for login registry")
    54  	if err := loginCmd.MarkFlagRequired("username"); err != nil {
    55  		logger.Error("failed to init flag: %v", err)
    56  		os.Exit(1)
    57  	}
    58  	if err := loginCmd.MarkFlagRequired("passwd"); err != nil {
    59  		logger.Error("failed to init flag: %v", err)
    60  		os.Exit(1)
    61  	}
    62  }