Skip to content

Commit

Permalink
fix: compatible with home path with ~, e.g. ~/.ssh/id_rsa
Browse files Browse the repository at this point in the history
  • Loading branch information
poneding committed Apr 29, 2024
1 parent a124ed1 commit 1da9316
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 22 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ssher 是一款轻量的 SSH Profile 管理命令行工具,让你可以更方

## 🔍 预览

![ssher](https://images.poneding.com/2024/04/202404260925762.gif)
![ssher](https://github.com/poneding/images/blob/master/2024/04/202404260925762.gif?raw=true)

## ⚙️ 安装

Expand All @@ -32,10 +32,10 @@ MacOS & Linux 安装,参考以下命令:

```bash
# MacOS
sudo wget https://ghproxy.ketches.cn/https://github.com/poneding/ssher/releases/download/v0.1.0/ssher_0.1.0_darwin_arm64 -O /user/local/bin/ssher && sudo chmod +x /user/local/bin/ssher
sudo wget https://ghproxy.ketches.cn/https://github.com/poneding/ssher/releases/download/v1.0.2/ssher_1.0.2_darwin_arm64 -O /user/local/bin/ssher && sudo chmod +x /user/local/bin/ssher

# Linux
sudo wget https://ghproxy.ketches.cn/https://github.com/poneding/ssher/releases/download/v0.1.0/ssher_0.1.0_linux_amd64 -O /user/local/bin/ssher && sudo chmod +x /user/local/bin/ssher
sudo wget https://ghproxy.ketches.cn/https://github.com/poneding/ssher/releases/download/v1.0.2/ssher_1.0.2_linux_amd64 -O /user/local/bin/ssher && sudo chmod +x /user/local/bin/ssher
```

> 注意:下载前确认你的系统是 `arm64` 还是 `amd64`,下载对应的二进制文件。
Expand All @@ -46,7 +46,7 @@ Windows 安装,参考以下步骤:

```bash
# 下载 .exe 文件
wget https://ghproxy.ketches.cn/https://github.com/poneding/ssher/releases/download/v0.1.0/ssher_0.1.0_windows_amd64.exe
wget https://ghproxy.ketches.cn/https://github.com/poneding/ssher/releases/download/v1.0.2/ssher_1.0.2_windows_amd64.exe
```

下载完成后,将 `ssher.exe` 文件路径添加到环境变量中,或者将其放到一个已经添加到环境变量的路径下。
Expand Down
8 changes: 4 additions & 4 deletions README_en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ssher developed with Go language, so it supports multiple platforms, including L

## 🔍 Preview

![ssher](https://images.poneding.com/2024/04/202404260925762.gif)
![ssher](https://github.com/poneding/images/blob/master/2024/04/202404260925762.gif?raw=true)

## ⚙️ Installation

Expand All @@ -32,10 +32,10 @@ MacOS & Linux installation, refer to the following commands:

```bash
# MacOS
sudo wget https://ghproxy.ketches.cn/https://github.com/poneding/ssher/releases/download/v0.1.0/ssher_0.1.0_darwin_arm64 -O /user/local/bin/ssher && sudo chmod +x /user/local/bin/ssher
sudo wget https://ghproxy.ketches.cn/https://github.com/poneding/ssher/releases/download/v1.0.2/ssher_1.0.2_darwin_arm64 -O /user/local/bin/ssher && sudo chmod +x /user/local/bin/ssher

# Linux
sudo wget https://ghproxy.ketches.cn/https://github.com/poneding/ssher/releases/download/v0.1.0/ssher_0.1.0_linux_amd64 -O /user/local/bin/ssher && sudo chmod +x /user/local/bin/ssher
sudo wget https://ghproxy.ketches.cn/https://github.com/poneding/ssher/releases/download/v1.0.2/ssher_1.0.2_linux_amd64 -O /user/local/bin/ssher && sudo chmod +x /user/local/bin/ssher
```

> Note: Before downloading, make sure your system is `arm64` or `amd64`, and download the corresponding binary file.
Expand All @@ -46,7 +46,7 @@ Download the `ssher.exe` file first:

```bash
# Download .exe file
wget https://github.com/poneding/ssher/releases/download/v0.1.0/ssher_0.1.0_windows_amd64.exe
wget https://github.com/poneding/ssher/releases/download/v1.0.2/ssher_1.0.2_windows_amd64.exe
```

Add the `ssher.exe` file path to the environment variable after download done, or put it in a path that has already been added to the environment variable.
Expand Down
18 changes: 6 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"

"github.com/poneding/ssher/internal/ssh"
"github.com/poneding/ssher/pkg/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -65,7 +66,7 @@ func initConfig() {
viper.SetConfigFile(cfgFile)
} else {
// Search config in home directory with name ".ssher" (without extension).
viper.AddConfigPath(userHomeDirOrDie())
viper.AddConfigPath(util.UserHomeDirOrDie())
viper.SetConfigType("yaml")
viper.SetConfigName(".ssher")
}
Expand All @@ -76,21 +77,12 @@ func initConfig() {
file := viper.ConfigFileUsed()
if file == "" {

file = userHomeDirOrDie() + "/.ssher.yaml"
file = util.UserHomeDirOrDie() + "/.ssher.yaml"
}
ssh.CreateProfileFile(file)
}
}

func userHomeDirOrDie() string {
path, err := os.UserHomeDir()
if err != nil {
fmt.Println("✗ Error:", err)
os.Exit(0)
}
return path
}

func runConnect() {
var profile *ssh.Profile

Expand All @@ -106,7 +98,9 @@ func runConnect() {
}

// reset current profile
ssh.GetCurrentProfile().Current = false
if current := ssh.GetCurrentProfile(); current != nil {
current.Current = false
}
profile.Current = true
ssh.SaveProfiles()

Expand Down
2 changes: 1 addition & 1 deletion cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func upgradeByDownload(v string) {

targetFile := fmt.Sprintf("ssher_%s_%s_%s%s", strings.Trim(v, "v"), runtime.GOOS, runtime.GOARCH, ext)

// download from github release. eg: https://github.com/poneding/ssher/releases/download/v0.1.0/ssher_0.1.0_linux_amd64
// download from github release. eg: https://github.com/poneding/ssher/releases/download/v1.0.2/ssher_1.0.2_linux_amd64
r, err := http.Get(fmt.Sprintf("%s/download/%s/%s", GH_RELEASE_PROXY_ADDR_BASE, v, targetFile))
if err != nil || r.StatusCode != http.StatusOK {
fmt.Println("✗ Failed to download:", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra"
)

const version = "v1.0.1"
const version = "v1.0.2"

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Expand Down
6 changes: 6 additions & 0 deletions internal/ssh/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

"github.com/manifoldco/promptui"
"github.com/poneding/ssher/pkg/util"
)

type PromptLable string
Expand Down Expand Up @@ -166,6 +167,11 @@ func FormPrompt() *Profile {
if input == "q" {
os.Exit(0)
}

// compatible with home path with ~, e.g. ~/.ssh/id_rsa
if strings.HasPrefix(input, "~") {
input = strings.Replace(input, "~", util.UserHomeDirOrDie(), 1)
}
_, err := os.Stat(input)
return err
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/util/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright © 2024 Pone Ding <[email protected]>
*/
package util

import (
"fmt"
"os"
)

// UserHomeDirOrDie returns the user's home directory.
// If the home directory cannot be determined, the program will exit.
func UserHomeDirOrDie() string {
path, err := os.UserHomeDir()
if err != nil {
fmt.Println("✗ Error:", err)
os.Exit(0)
}
return path
}

0 comments on commit 1da9316

Please sign in to comment.