github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/utils/ioutils/ioutils.go (about)

     1  package ioutils
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"github.com/jfrog/jfrog-cli-go/utils/cliutils"
     7  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
     8  	"golang.org/x/crypto/ssh/terminal"
     9  	"io"
    10  	"os"
    11  	"strings"
    12  	"syscall"
    13  )
    14  
    15  // @param allowUsingSavedPassword - Prevent changing username or url without changing the password.
    16  // False iff the user changed the username or the url.
    17  func ReadCredentialsFromConsole(details, savedDetails cliutils.Credentials, allowUsingSavedPassword bool) error {
    18  	if details.GetUser() == "" {
    19  		tempUser := ""
    20  		ScanFromConsole("User", &tempUser, savedDetails.GetUser())
    21  		details.SetUser(tempUser)
    22  		allowUsingSavedPassword = false
    23  	}
    24  	if details.GetPassword() == "" {
    25  		print("Password/API key: ")
    26  		bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
    27  		err = errorutils.CheckError(err)
    28  		if err != nil {
    29  			return err
    30  		}
    31  		details.SetPassword(string(bytePassword))
    32  		if details.GetPassword() == "" && allowUsingSavedPassword {
    33  			details.SetPassword(savedDetails.GetPassword())
    34  		}
    35  		// New-line required after the password input:
    36  		fmt.Println()
    37  	}
    38  	return nil
    39  }
    40  
    41  func ScanFromConsole(caption string, scanInto *string, defaultValue string) {
    42  	if defaultValue != "" {
    43  		print(caption + " [" + defaultValue + "]: ")
    44  	} else {
    45  		print(caption + ": ")
    46  	}
    47  
    48  	scanner := bufio.NewScanner(os.Stdin)
    49  	scanner.Scan()
    50  	*scanInto = scanner.Text()
    51  	if *scanInto == "" {
    52  		*scanInto = defaultValue
    53  	}
    54  }
    55  
    56  func CopyFile(src, dst string, fileMode os.FileMode) error {
    57  	from, err := os.Open(src)
    58  	if err != nil {
    59  		return errorutils.CheckError(err)
    60  	}
    61  	defer from.Close()
    62  
    63  	to, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE, fileMode)
    64  	if err != nil {
    65  		return errorutils.CheckError(err)
    66  	}
    67  	defer to.Close()
    68  
    69  	if _, err = io.Copy(to, from); err != nil {
    70  		return errorutils.CheckError(err)
    71  	}
    72  
    73  	return errorutils.CheckError(os.Chmod(dst, fileMode))
    74  }
    75  
    76  func DoubleWinPathSeparator(filePath string) string {
    77  	return strings.Replace(filePath, "\\", "\\\\", -1)
    78  }
    79  
    80  func UnixToWinPathSeparator(filePath string) string {
    81  	return strings.Replace(filePath, "/", "\\\\", -1)
    82  }
    83  
    84  func PrepareFilePathForUnix(path string) string {
    85  	if cliutils.IsWindows() {
    86  		path = strings.Replace(path, "\\\\", "/", -1)
    87  		path = strings.Replace(path, "\\", "/", -1)
    88  	}
    89  	return path
    90  }