From 05e4c42af9ca59f7206c13854532ba5f94269aa4 Mon Sep 17 00:00:00 2001 From: Sam Stevens Date: Mon, 2 Apr 2018 13:50:31 +0100 Subject: [PATCH] initial commit --- .gitignore | 84 ++++++++++++++++++++++++++++++++++++ .idea/jumphost.iml | 9 ++++ .idea/misc.xml | 6 +++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ main.go | 105 +++++++++++++++++++++++++++++++++++++++++++++ test/config.txt | 7 +++ 7 files changed, 225 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/jumphost.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 main.go create mode 100644 test/config.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..87642f1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,84 @@ + +# Created by https://www.gitignore.io/api/go,jetbrains + +### Go ### +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +### JetBrains ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Ruby plugin and RubyMine +/.rakeTasks + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### JetBrains Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +.idea/sonarlint + + +# End of https://www.gitignore.io/api/go,jetbrains \ No newline at end of file diff --git a/.idea/jumphost.iml b/.idea/jumphost.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/jumphost.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..28a804d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a3b5a76 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..5d110a4 --- /dev/null +++ b/main.go @@ -0,0 +1,105 @@ +package main + +import ( + "os" + "log" + "io/ioutil" + "fmt" + "strings" + "os/user" + "github.com/manifoldco/promptui" +) + +type host struct { + Name string + HostName string + User string +} + +func readFile(file string) string { + contents, err := ioutil.ReadFile(file) + if err != nil { + log.Fatalf("%q does not exist", file) + } + return fmt.Sprintf("%s", contents) +} + +func readHosts(lines []string) []host { + hosts := make([]host,0) + current := host{} + + for _, line := range lines { + fields := strings.Fields(line) + if len(fields) <= 1 { + continue + } + nameLower := strings.ToLower(fields[0]) + if nameLower == "host" { + if current.Name != "" { + hosts = append(hosts, current) + } + currentUser, err := user.Current() + currentUserName := "" + if err == nil { + currentUserName = currentUser.Username + } + current = host{Name:fields[1], HostName: fields[1], User: currentUserName} + } + if nameLower == "hostname" { + current.HostName = fields[1] + } + if nameLower == "user" { + current.User = fields[1] + } + } + if current.Name != "" { + hosts = append(hosts, current) + } + + return hosts +} + +func main() { + args := os.Args[1:] + if len(args) == 0 { + log.Fatalf("Usage: %s [configfile]", os.Args[0]) + } + fileName := args[0] + lines := strings.Split(readFile(fileName), "\n") + for i, line := range lines { + lines[i] = strings.Trim(line, "\r\t ") + } + hosts := readHosts(lines) + + searcher := func(input string, index int) bool { + host := hosts[index] + name := strings.Replace(strings.ToLower(host.Name+host.User+host.HostName), " ", "", -1) + input = strings.Replace(strings.ToLower(input), " ", "", -1) + + return strings.Contains(name, input) + } + + templates := &promptui.SelectTemplates { + Label: "{{ . }}", + Active: ">{{ .User | cyan }}@({{ .HostName | red }})", + Inactive: " {{ .User | cyan }}@({{ .HostName | red }})", + Selected: "{{ .User | cyan }}@({{ .HostName | red }})", + } + + prompt := promptui.Select{ + Label: "Select Host/Option", + Items: hosts, + Templates: templates, + Size: 4, + Searcher: searcher, + } + + i, _, err := prompt.Run() + + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + + fmt.Printf("You choose number %d: %s\n", i+1, hosts[i].Name) +} \ No newline at end of file diff --git a/test/config.txt b/test/config.txt new file mode 100644 index 0000000..85405a5 --- /dev/null +++ b/test/config.txt @@ -0,0 +1,7 @@ +Host host1 + HostName hostname1 + User user1 +Host host2 + HostName hostname2 + User someone +Host host3 \ No newline at end of file