git.colasdn.top/scaleway/scaleway-cli@v1.11.1/pkg/cli/cmd_login.go (about)

     1  // Copyright (C) 2015 Scaleway. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE.md file.
     4  
     5  package cli
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/scaleway/scaleway-cli/pkg/commands"
    11  )
    12  
    13  var cmdLogin = &Command{
    14  	Exec:        runLogin,
    15  	UsageLine:   "login [OPTIONS]",
    16  	Description: "Log in to Scaleway API",
    17  	Help: `Generates a configuration file in '/home/$USER/.scwrc'
    18  containing credentials used to interact with the Scaleway API. This
    19  configuration file is automatically used by the 'scw' commands.
    20  
    21  You can get your credentials on https://cloud.scaleway.com/#/credentials
    22  `,
    23  }
    24  
    25  func init() {
    26  	cmdLogin.Flag.StringVar(&organization, []string{"o", "-organization"}, "", "Organization")
    27  	cmdLogin.Flag.StringVar(&token, []string{"t", "-token"}, "", "Token")
    28  	cmdLogin.Flag.BoolVar(&loginHelp, []string{"h", "-help"}, false, "Print usage")
    29  	cmdLogin.Flag.BoolVar(&loginSkipSSHKey, []string{"s", "-skip-ssh-key"}, false, "Don't ask to upload an SSH Key")
    30  }
    31  
    32  // FLags
    33  var organization string  // -o flag
    34  var token string         // -t flag
    35  var loginHelp bool       // -h, --help flag
    36  var loginSkipSSHKey bool // -s, --skip-ssh-key flag
    37  
    38  func runLogin(cmd *Command, rawArgs []string) error {
    39  	if loginHelp {
    40  		return cmd.PrintUsage()
    41  	}
    42  	if len(rawArgs) != 0 {
    43  		return cmd.PrintShortUsage()
    44  	}
    45  	if (organization != "" || token != "") && (organization == "" || token == "") {
    46  		return fmt.Errorf("you must define organization AND token")
    47  	}
    48  	args := commands.LoginArgs{
    49  		Organization: organization,
    50  		Token:        token,
    51  		SkipSSHKey:   loginSkipSSHKey,
    52  	}
    53  	ctx := cmd.GetContext(rawArgs)
    54  	return commands.RunLogin(ctx, args)
    55  }