示例#1
0
文件: infra.go 项目: mbrodala/otto
// guessAndLoadPrivateKey takes a path to a public key and determines if a
// private key exists by just stripping ".pub" from the end of it. if so,
// it attempts to load that key into the agent.
func guessAndLoadPrivateKey(ui ui.Ui, pubKeyPath string) (bool, error) {
	fullPath, err := homedir.Expand(pubKeyPath)
	if err != nil {
		return false, err
	}
	if !strings.HasSuffix(fullPath, ".pub") {
		return false, fmt.Errorf("No .pub suffix, cannot guess path.")
	}
	privKeyGuess := strings.TrimSuffix(fullPath, ".pub")
	if _, err := os.Stat(privKeyGuess); os.IsNotExist(err) {
		return false, fmt.Errorf("No file at guessed path.")
	}

	ui.Header("Loading key into SSH Agent")
	ui.Message(fmt.Sprintf(
		"The key you provided (%s) was not found in your SSH Agent.", pubKeyPath))
	ui.Message(fmt.Sprintf(
		"However, Otto found a private key here: %s", privKeyGuess))
	ui.Message(fmt.Sprintf(
		"Automatically running 'ssh-add %s'.", privKeyGuess))
	ui.Message("If your SSH key has a passphrase, you will be prompted for it.")
	ui.Message("")

	if err := sshagent.Add(ui, privKeyGuess); err != nil {
		return false, err
	}

	return true, nil
}
示例#2
0
// guestAndLoadPrivateKey 获得private key路径,只是依赖判断去除.pub部分的内容
// 如果存在则直接装在agent
func guestAndLoadPrivateKey(ui ui.Ui, pubKeyPath string) (bool, error) {
	fullPath, err := homedir.Expand(pubKeyPath)
	if err != nil {
		return false, err
	}
	if !strings.HasSuffix(fullPath, ".pub") {
		return false, fmt.Errorf("没有找到.pub后缀的文件")
	}
	privKeyGuess := strings.TrimSuffix(fullPath, ".pub")
	if _, err := os.Stat(privKeyGuess); os.IsNotExist(err) {
		return false, fmt.Errorf("文件不存在!")
	}

	ui.Header("装载key到SSH Agent")
	ui.Message(fmt.Sprintf(
		"在SSH Agent中没有你提供的key (%s)", pubKeyPath))
	ui.Message(fmt.Sprintf(
		"然而,Otto在这里:%s 找的了一个private key", privKeyGuess))
	ui.Message(fmt.Sprintf(
		"自动运行'ssh-add %s'.", privKeyGuess))
	ui.Message("如果你的SSH key有密码,你会看到如下提示")
	ui.Message("")

	if err := sshagent.Add(ui, privKeyGuess); err != nil {
		return false, err
	}

	return true, nil
}