Skip to content

Commit

Permalink
Move , and logic into their cobra commands. For engine and proxy pack…
Browse files Browse the repository at this point in the history
…ages, move functions into utils_ file.
  • Loading branch information
Sandeep Jadoonanan committed Oct 10, 2017
1 parent 2c832be commit 38f818a
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 114 deletions.
11 changes: 8 additions & 3 deletions cmd/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"log"
"strings"

"github.com/exitshell/konnect/engine"
"github.com/spf13/cobra"
Expand All @@ -22,14 +23,18 @@ var ArgsCmd = &cobra.Command{
if len(args) != 1 {
log.Fatal("Please specify one host")
}
host := args[0]

// Init engine.
konnect, err := engine.Init(filename)
handleErr(err)

// Print Host SSH command.
hostArgs, err := konnect.Args(args[0])
// Get host.
proxy, err := konnect.Get(host)
handleErr(err)
fmt.Println(hostArgs)

// Get args for host.
argsStr := strings.Join(proxy.Args(), " ")
fmt.Println(argsStr)
},
}
6 changes: 5 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ func InteractivePrompt(cmd *cobra.Command) {
log.Fatal("No host was selected")
}

// Get proxy.
proxy, err := konnect.Get(answer.Hostname)
handleErr(err)

// Connect to host.
if err := konnect.Connect(answer.Hostname); err != nil {
if err := proxy.Connect(); err != nil {
log.Fatal(err)
}
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ var ConnectCmd = &cobra.Command{
if len(args) != 1 {
log.Fatal("Please specify one host")
}
host := args[0]

// Init engine.
konnect, err := engine.Init(filename)
handleErr(err)

// Get host.
proxy, err := konnect.Get(host)
handleErr(err)

// Connect to host.
if err := konnect.Connect(args[0]); err != nil {
if err := proxy.Connect(); err != nil {
log.Fatal(err)
}
},
Expand Down
1 change: 1 addition & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var InitCmd = &cobra.Command{
dir = args[0]
}

// Get absolute path of the dir.
dir, _ = filepath.Abs(dir)

// Get the default name for the config file.
Expand Down
9 changes: 7 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ var ListCmd = &cobra.Command{
konnect, err := engine.Init(filename)
handleErr(err)

// List all hosts.
fmt.Print(konnect.List())
// Show info for all hosts.
hostList := ""
for _, host := range konnect.GetHosts() {
hostInfo := konnect.Hosts[host].Info()
hostList += fmt.Sprintln(hostInfo)
}
fmt.Println(hostList)
},
}
81 changes: 0 additions & 81 deletions engine/api.go

This file was deleted.

31 changes: 25 additions & 6 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,30 @@ func (k *Konnect) TestConnection(host string) {
k.ProxyChan <- true
}

// New - Create a new Konnect object.
func New() *Konnect {
return &Konnect{
Hosts: make(map[string]*proxy.SSHProxy),
ProxyChan: make(chan bool),
CompletedChan: make(chan bool),
// Status - Check the status of one or more hosts.
func (k *Konnect) Status(hosts []string) {
// For each specified host, launch a goroutine
// to test the ssh connection.
for _, host := range hosts {
go k.TestConnection(host)
}

// This goroutine blocks until all specified
// host connections have been tested.
go func() {
for i := 0; i < len(hosts); i++ {
<-k.ProxyChan
}
k.CompletedChan <- true
}()

// Block until above goroutine completes.
<-k.CompletedChan
// Done

// Print the status of all hosts.
for _, host := range hosts {
proxy := k.Hosts[host]
proxy.PrintStatus()
}
}
31 changes: 31 additions & 0 deletions engine/utils_.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package engine

import (
"path/filepath"

"github.com/exitshell/konnect/proxy"
)

// New - Create a new Konnect object.
func New() *Konnect {
return &Konnect{
Hosts: make(map[string]*proxy.SSHProxy),
ProxyChan: make(chan bool),
CompletedChan: make(chan bool),
}
}

// Init - Create a new Konnect object.
func Init(filename string) (*Konnect, error) {
filename, err := filepath.Abs(filename)
if err != nil {
return nil, err
}

konnect := New()
if err = konnect.LoadFromFile(filename); err != nil {
return nil, err
}

return konnect, nil
}
20 changes: 0 additions & 20 deletions proxy/sshproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,3 @@ func (s *SSHProxy) TestConnection() {

s.Connection = true
}

// New - Create a new SSHProxy object with given values.
func New(user, host string, port int, key string) *SSHProxy {
return &SSHProxy{
User: user,
Host: host,
Port: port,
Key: key,
}
}

// Default - Create a new SSHProxy object with default values.
func Default() *SSHProxy {
return &SSHProxy{
User: "",
Host: "",
Port: getDefaultPort(),
Key: getDefaultKey(),
}
}
20 changes: 20 additions & 0 deletions proxy/utils_.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,23 @@ func getDefaultKey() string {
func getDefaultPort() int {
return 22
}

// New - Create a new SSHProxy object with given values.
func New(user, host string, port int, key string) *SSHProxy {
return &SSHProxy{
User: user,
Host: host,
Port: port,
Key: key,
}
}

// Default - Create a new SSHProxy object with default values.
func Default() *SSHProxy {
return &SSHProxy{
User: "",
Host: "",
Port: getDefaultPort(),
Key: getDefaultKey(),
}
}

0 comments on commit 38f818a

Please sign in to comment.