github.com/KusionStack/kpm@v0.8.4-0.20240326033734-dc72298a30e5/pkg/cmd/cmd_login.go (about)

     1  // Copyright 2023 The KCL Authors. All rights reserved.
     2  // Deprecated: The entire contents of this file will be deprecated. 
     3  // Please use the kcl cli - https://github.com/kcl-lang/cli.
     4  
     5  package cmd
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/urfave/cli/v2"
    11  	"kcl-lang.io/kpm/pkg/client"
    12  	"kcl-lang.io/kpm/pkg/reporter"
    13  	"kcl-lang.io/kpm/pkg/utils"
    14  )
    15  
    16  // NewLoginCmd new a Command for `kpm login`.
    17  func NewLoginCmd(kpmcli *client.KpmClient) *cli.Command {
    18  	return &cli.Command{
    19  		Hidden: false,
    20  		Name:   "login",
    21  		Usage:  "login to a registry",
    22  		Flags: []cli.Flag{
    23  			// The registry username.
    24  			&cli.StringFlag{
    25  				Name:    "username",
    26  				Aliases: []string{"u"},
    27  				Usage:   "registry username",
    28  			},
    29  			// The registry registry password or identity token.
    30  			&cli.StringFlag{
    31  				Name:    "password",
    32  				Aliases: []string{"p"},
    33  				Usage:   "registry password or identity token",
    34  			},
    35  		},
    36  		Action: func(c *cli.Context) error {
    37  			if c.NArg() == 0 {
    38  				return reporter.NewErrorEvent(
    39  					reporter.InvalidCmd,
    40  					fmt.Errorf("registry must be specified"),
    41  				)
    42  			}
    43  			registry := c.Args().First()
    44  
    45  			username, password, err := utils.GetUsernamePassword(c.String("username"), c.String("password"), c.Bool("password-stdin"))
    46  			if err != nil {
    47  				return err
    48  			}
    49  
    50  			err = kpmcli.LoginOci(registry, username, password)
    51  			if err != nil {
    52  				return err
    53  			}
    54  			reporter.ReportMsgTo("Login Succeeded", kpmcli.GetLogWriter())
    55  			return nil
    56  		},
    57  	}
    58  }