Skip to content

Commit

Permalink
Rework the code and TUI
Browse files Browse the repository at this point in the history
  • Loading branch information
quantumsheep committed Nov 5, 2023
1 parent 65eb9a8 commit 408e044
Show file tree
Hide file tree
Showing 15 changed files with 532 additions and 646 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
run: echo "FILENAME=sshs-${{ matrix.target.os }}-${{ matrix.target.arch }}${{ env.EXTENSION }}" >> $GITHUB_ENV

- name: Build
run: make OUTPUT=bin/${{ env.FILENAME }}
run: make build OUTPUT=bin/${{ env.FILENAME }}
env:
GOOS: "${{ matrix.target.os }}"
GOARCH: "${{ matrix.target.arch }}"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
run: echo "FILENAME=sshs-${{ matrix.target.os }}-${{ matrix.target.arch }}${{ env.EXTENSION }}" >> $GITHUB_ENV

- name: Build
run: make OUTPUT=bin/${{ env.FILENAME }}
run: make build OUTPUT=bin/${{ env.FILENAME }}
env:
GOOS: "${{ matrix.target.os }}"
GOARCH: "${{ matrix.target.arch }}"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GOLDFLAGS := -w -s
endif

build:
go build -ldflags "$(GOLDFLAGS) -X '$(GO_PACKAGE_PATH)/cmd.Version=$(or $(strip $(VERSION)),latest)'" -o $(OUTPUT)
go build -ldflags "$(GOLDFLAGS) -X 'main.Version=$(or $(strip $(VERSION)),latest)'" -o $(OUTPUT) cmd/main.go

clean:
rm -f sshs
Expand Down
135 changes: 135 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package main

import (
"fmt"
"log"
"os"
"os/user"
"path/filepath"
"strings"

"github.com/mikkeloscar/sshconfig"
"github.com/quantumsheep/sshs/internal/display"
"github.com/quantumsheep/sshs/internal/ssh"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
Version string
)

var rootCmd = &cobra.Command{
Use: "sshs",
Short: "SSH servers manager",
Long: "SSHS lets you manage your SSH servers and connect to them easily using a TUI.\nGitHub: https://github.com/quantumsheep/sshs",
Version: Version,
Run: run,
}

func run(cmd *cobra.Command, args []string) {
flags := cmd.Flags()

sshConfigPath, err := flags.GetString("config")
if err != nil {
log.Fatal(err)
}
if sshConfigPath == "" {
log.Fatal("empty config path")
}
if strings.HasPrefix(sshConfigPath, "~/") {
currentUser, err := user.Current()
if err != nil {
log.Fatal(err)
}

sshConfigPath = filepath.Join(currentUser.HomeDir, sshConfigPath[2:])
}

absoluteSSHConfigPath, err := filepath.Abs(sshConfigPath)
if err != nil {
log.Fatal(err)
}

if sshConfigPath == "~/.ssh/config" {
// Create the file if it doesn't exist
_, err = os.Stat(sshConfigPath)
if os.IsNotExist(err) {
err := os.MkdirAll(filepath.Dir(absoluteSSHConfigPath), 0700)
if err != nil {
log.Fatal(err)
}

_, err = os.Create(absoluteSSHConfigPath)
if err != nil {
log.Fatal(err)
}
}
}

shouldDisplayProxyCommand, err := flags.GetBool("proxy")
if err != nil {
log.Fatal(err)
}

searchFilter, err := flags.GetString("search")
if err != nil {
log.Fatal(err)
}

// exitAfterSessionEnds, err := flags.GetBool("exit")
// if err != nil {
// log.Fatal(err)
// }

additionalSSHArguments, err := flags.GetString("ssh-arguments")
if err != nil {
log.Fatal(err)
}

hosts, err := sshconfig.Parse(absoluteSSHConfigPath)
if err != nil {
log.Fatal(err)
}

var d *display.Display
d = display.NewDisplay(&display.DisplayConfig{
SSHHosts: hosts,

ShouldDisplayProxyCommand: shouldDisplayProxyCommand,
SearchFilter: searchFilter,

OnSSHHostSelected: func(host *sshconfig.SSHHost) {
d.Pause()

sshHost := ssh.ParseHosts(host.Host)
fmt.Printf("Connecting to %s...\n", sshHost)
ssh.Run(sshHost, absoluteSSHConfigPath, additionalSSHArguments)
d.Resume()
},
})

err = d.Start()
if err != nil {
log.Fatal(err)
}
}

func init() {
flags := rootCmd.PersistentFlags()
flags.StringP("config", "c", "~/.ssh/config", "SSH config file")
flags.StringP("ssh-arguments", "a", "", "Arguments for the ssh command (example: '-D 1080')")
flags.StringP("search", "s", "", "Host search filter")
flags.BoolP("proxy", "p", false, "Display full ProxyCommand")
flags.BoolP("exit", "e", false, "Exit when the ssh command terminated")

viper.SetDefault("author", "Nathanael Demacon <[email protected]>")
viper.SetDefault("license", "MIT")
}

func main() {
if e := rootCmd.Execute(); e != nil {
fmt.Println(e)
os.Exit(1)
}
}
138 changes: 0 additions & 138 deletions cmd/root.go

This file was deleted.

56 changes: 35 additions & 21 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,51 @@ module github.com/quantumsheep/sshs
go 1.18

require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/gdamore/tcell/v2 v2.5.3
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.24.2
github.com/charmbracelet/lipgloss v0.9.1
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/mikkeloscar/sshconfig v0.1.1
github.com/mitchellh/go-homedir v1.1.0
github.com/rivo/tview v0.0.0-20221029100920-c4a7e501810d
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.14.0
github.com/samber/lo v1.38.1
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.17.0
)

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/rivo/uniseg v0.4.2 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sahilm/fuzzy v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/term v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 408e044

Please sign in to comment.