github.com/section/sectionctl@v1.12.3/commands/login.go (about)

     1  package commands
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"runtime"
     9  
    10  	"github.com/rs/zerolog/log"
    11  
    12  	"github.com/section/sectionctl/api"
    13  	"github.com/section/sectionctl/credentials"
    14  )
    15  
    16  // LoginCmd handles authenticating the CLI against Section's API
    17  type LoginCmd struct {
    18  	in  io.Reader
    19  	out io.Writer
    20  }
    21  
    22  // Run executes the command
    23  func (c *LoginCmd) Run() (err error) {
    24  	screenshot := "https://raw.githubusercontent.com/section/sectionctl/main/docs/section_token_control_panel.png"
    25  	windowsStr := fmt.Sprintf("Unable to write credential.\n\nPlease execute the following, add it to your Powershell profile, or add it to your environment variables in control panel: \nWith Powershell:\n$env:SECTION_TOKEN=\"%s\"\n\nWith CMD:\nset SECTION_TOKEN=%s\n\nWith control panel:\n%s", api.Token, api.Token, screenshot)
    26  	linuxStr := fmt.Sprintf("Unable to write credential.\n\nPlease run this command, and add it to your ~/.bashrc (you do not need to run sectionctl login again)\n\nexport SECTION_TOKEN=%s", api.Token)
    27  	if api.Token != "" {
    28  		err = credentials.Write(api.PrefixURI.Host, api.Token)
    29  		if err != nil {
    30  			if runtime.GOOS == "windows" {
    31  				fmt.Print(windowsStr)
    32  				return nil
    33  			}
    34  			fmt.Printf("%s\n", linuxStr)
    35  			return nil
    36  		}
    37  	} else {
    38  		t, err := credentials.PromptAndWrite(c.In(), c.Out(), api.PrefixURI.Host)
    39  		if err != nil {
    40  			if runtime.GOOS == "windows" {
    41  				fmt.Printf("Unable to write credential.\n\nPlease execute the following, add it to your Powershell profile, or add it to your environment variables in control panel: \nWith Powershell:\n$env:SECTION_TOKEN=\"%s\"\n\nWith CMD:\nset SECTION_TOKEN=%s\n\nWith control panel:\n%s", t, t, screenshot)
    42  				return nil
    43  			}
    44  			fmt.Printf("%s%s\n", linuxStr, t)
    45  			return nil
    46  		}
    47  		api.Token = t
    48  	}
    49  	log.Info().Msg("Validating credentials...")
    50  	_, err = api.CurrentUser()
    51  	if err != nil {
    52  		fmt.Println("error!")
    53  		if errors.Is(err, api.ErrAuthDenied) {
    54  			return err
    55  		}
    56  		return fmt.Errorf("could not fetch current user: %w", err)
    57  	}
    58  	log.Info().Msg(fmt.Sprintln("success!"))
    59  	
    60  	return err
    61  }
    62  
    63  // In returns the input to read from
    64  func (c *LoginCmd) In() io.Reader {
    65  	if c.in != nil {
    66  		return c.in
    67  	}
    68  	return os.Stdin
    69  }
    70  
    71  // Out returns the output to write to
    72  func (c *LoginCmd) Out() io.Writer {
    73  	if c.out != nil {
    74  		return c.out
    75  	}
    76  	return os.Stdout
    77  }