From 81df13398f01f12a0524d0e9ddb1d0ea52f907aa Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Sat, 9 Dec 2023 17:26:12 +0900 Subject: [PATCH] Add output format option Fixes #10 Co-Authored-by: chksome --- README.md | 16 +++++++++++++++- config.go | 29 ++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d94e4ae..f8ef7aa 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,9 @@ chmod 755 wsl2-ssh-agent **If you are using ArchLinux, you can install the [wsl2-ssh-agent](https://aur.archlinux.org/packages/wsl2-ssh-agent) package from the AUR (maintained by @Hill-98).** -### 2. Modify `.bashrc` (or `.zshrc` if you are using `zsh`) +### 2. Modify your shell's rc file + +#### bash or zsh Add the following line to `.bashrc` (or `.zshrc` if you are using `zsh`). @@ -30,6 +32,18 @@ Add the following line to `.bashrc` (or `.zshrc` if you are using `zsh`). eval $($HOME/wsl2-ssh-agent) ``` +#### fish + +Add the following lines to `config.fish` + +``` +if status is-login + $HOME/wsl2-ssh-agent | source +end +``` + +### 3. Reopen your terminal + Close and reopen the terminal and execute `ssh your-machine`. The command should communicate with ssh-agent.exe service. diff --git a/config.go b/config.go index 702a19d..6337930 100644 --- a/config.go +++ b/config.go @@ -12,12 +12,14 @@ import ( "os/exec" "os/signal" "path/filepath" + "strings" "syscall" ) type config struct { socketPath string powershellPath string + format string foreground bool verbose bool stop bool @@ -76,6 +78,31 @@ func newConfig() *config { return c } +func getOutputFormat(format string) string { + if format == "auto" { + shell := os.Getenv("SHELL") + if strings.HasSuffix(shell, "fish") { + format = "fish" + } else if strings.HasSuffix(shell, "csh") { + format = "csh" + } else { + format = "sh" + } + } + switch format { + case "sh", "bash", "zsh": + return "SSH_AUTH_SOCK=%s; export SSH_AUTH_SOCK;" + case "csh", "tcsh": + return "setenv SSH_AUTH_SOCK %s" + case "fish": + return "set -x SSH_AUTH_SOCK %s" + default: + fmt.Printf("output format must be auto, bash, zsh, csh, tcsh, or fish\n") + os.Exit(1) + return "" + } +} + func (c *config) start() context.Context { if c.version { fmt.Printf("wsl2-ssh-agent %s\n", version) @@ -86,7 +113,7 @@ func (c *config) start() context.Context { parent := checkDaemonMode() // script output - output := fmt.Sprintf("SSH_AUTH_SOCK=%s; export SSH_AUTH_SOCK;", c.socketPath) + output := fmt.Sprintf(getOutputFormat(c.format), c.socketPath) // set up the log file c.setupLogFile()