github.com/jcarley/cli@v0.0.0-20180201210820-966d90434c30/commands/keys/set.go (about)

     1  package keys
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/Sirupsen/logrus"
     9  	"github.com/daticahealth/cli/lib/auth"
    10  	"github.com/daticahealth/cli/lib/prompts"
    11  	"github.com/daticahealth/cli/models"
    12  	"github.com/mitchellh/go-homedir"
    13  )
    14  
    15  func CmdSet(path string, settings *models.Settings) error {
    16  	homePath, err := homedir.Expand(path)
    17  	if err != nil {
    18  		return err
    19  	}
    20  	fullPath, err := filepath.Abs(homePath)
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	// make sure both files exist
    26  	_, err = os.Stat(fullPath + ".pub")
    27  	if err != nil {
    28  		if os.IsNotExist(err) {
    29  			return fmt.Errorf("Public key file '%s' does not exist.", fullPath+".pub")
    30  		}
    31  		return err
    32  	}
    33  
    34  	_, err = os.Stat(fullPath)
    35  	if err != nil {
    36  		if os.IsNotExist(err) {
    37  			return fmt.Errorf("Private key file '%s' does not exist.", fullPath)
    38  		}
    39  		return err
    40  	}
    41  
    42  	settings.PrivateKeyPath = fullPath
    43  	settings.SessionToken = ""
    44  	a := auth.New(settings, prompts.New())
    45  	user, err := a.Signin()
    46  	if err != nil {
    47  		return err
    48  	}
    49  	logrus.Printf("Successfully added key and signed in as %s.", user.Email)
    50  	return nil
    51  }