initial commit

This commit is contained in:
2018-04-02 13:50:31 +01:00
commit 05e4c42af9
7 changed files with 225 additions and 0 deletions

84
.gitignore vendored Normal file
View File

@@ -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

9
.idea/jumphost.iml generated Normal file
View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/jumphost.iml" filepath="$PROJECT_DIR$/.idea/jumphost.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

105
main.go Normal file
View File

@@ -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)
}

7
test/config.txt Normal file
View File

@@ -0,0 +1,7 @@
Host host1
HostName hostname1
User user1
Host host2
HostName hostname2
User someone
Host host3