Skip to content

Commit

Permalink
Create SSHTask type. Load SSHTask objects into Konnect.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandeep Jadoonanan committed Nov 12, 2017
1 parent b1a7c10 commit ac07067
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func InteractivePrompt(cmd *cobra.Command) {
handleErr(err)

// Get host names.
hosts := konnect.GetHosts()
hosts := konnect.GetHostNames()

// Create survey.
prompt := []*survey.Question{
Expand Down
12 changes: 10 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ var ListCmd = &cobra.Command{

// Show info for all hosts.
hostList := ""
for _, host := range konnect.GetHosts() {
hostInfo := konnect.Hosts[host].Info()
for _, hostName := range konnect.GetHostNames() {
hostInfo := konnect.Hosts[hostName].Info()
hostList += fmt.Sprintln(hostInfo)
}
fmt.Println(hostList)

// Show info for all tasks.
taskList := ""
for _, taskName := range konnect.GetTaskNames() {
taskInfo := konnect.Tasks[taskName].Info()
taskList += fmt.Sprintln(taskInfo)
}
fmt.Println(taskList)
},
}
2 changes: 1 addition & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var StatusCmd = &cobra.Command{
// If `allHosts` is specified, then use
// all hosts in the Konnect engine.
if allHosts == true {
hosts = konnect.GetHosts()
hosts = konnect.GetHostNames()
}

if allHosts == true && len(args) > 0 {
Expand Down
54 changes: 48 additions & 6 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
yaml "gopkg.in/yaml.v2"

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

// Konnect is a collection of SSHProxy objects.
Expand All @@ -17,6 +18,7 @@ type Konnect struct {
ProxyChan chan bool
CompletedChan chan bool
Hosts map[string]*proxy.SSHProxy
Tasks map[string]*task.SSHTask
}

// Get an SSHProxy object by name.
Expand All @@ -30,8 +32,8 @@ func (k *Konnect) Get(name string) (*proxy.SSHProxy, error) {
return proxy, nil
}

// GetHosts - Get host names in sorted order (asc).
func (k *Konnect) GetHosts() []string {
// GetHostNames - Get host names in sorted order (asc).
func (k *Konnect) GetHostNames() []string {
names := []string{}
for host := range k.Hosts {
names = append(names, host)
Expand All @@ -40,6 +42,16 @@ func (k *Konnect) GetHosts() []string {
return names
}

// GetTaskNames - Get task names in sorted order (asc).
func (k *Konnect) GetTaskNames() []string {
names := []string{}
for task := range k.Tasks {
names = append(names, task)
}
sort.Strings(names)
return names
}

// CheckHosts - Ensure that the given host names exist.
func (k *Konnect) CheckHosts(hosts []string) error {
// If a given host does not exist
Expand Down Expand Up @@ -108,16 +120,16 @@ func (k *Konnect) UnmarshalHosts(byteStr []byte) error {
// Turn the value into a byte string.
byteStr, _ := yaml.Marshal(val)
// Construct an SSHProxy object.
proxy := proxy.Default()
newProxy := proxy.Default()
// Unmarshal the byte string into an SSHProxy object.
err := yaml.Unmarshal(byteStr, proxy)
err := yaml.Unmarshal(byteStr, newProxy)
if err != nil {
return err
}
// Fill in values from global config.
proxy.PopulateFromProxy(k.Global)
newProxy.PopulateFromProxy(k.Global)
// Assign to Konnect.
k.Hosts[key] = proxy
k.Hosts[key] = newProxy

default:
return errors.New("Unknown type for temp host")
Expand All @@ -127,6 +139,31 @@ func (k *Konnect) UnmarshalHosts(byteStr []byte) error {
return nil
}

// UnmarshalTasks - Unmarshal SSHTask objects from a byte string.
func (k *Konnect) UnmarshalTasks(byteStr []byte) error {
// Make a temporary type to hold the task data.
var tempTasks struct {
Tasks map[string]string `yaml:"tasks"`
}

// Unmarshal the byteStr into the temp tasks struct.
if err := yaml.Unmarshal(byteStr, &tempTasks); err != nil {
return err
}

// Iterate through the unmarshalled tasks,
// and create SSHTask objects.
for key, val := range tempTasks.Tasks {
// Construct an SSHTask object.
newTask := task.New(key, val)

// Assign to Konnect.
k.Tasks[key] = newTask
}

return nil
}

// LoadFromFile - Load and validate SSHProxy objects from a yaml config file.
func (k *Konnect) LoadFromFile(filename string) error {
// Read config file.
Expand All @@ -146,6 +183,11 @@ func (k *Konnect) LoadFromFile(filename string) error {
return err
}

// Unmarshal SSHTask objects from a byte string.
if err := k.UnmarshalTasks(byteStr); err != nil {
return err
}

// Validate each SSHProxy in Konnect.
for name, proxy := range k.Hosts {
proxy.Filename = filename
Expand Down
2 changes: 2 additions & 0 deletions engine/utils_.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"path/filepath"

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

// New - Create a new Konnect object.
func New() *Konnect {
return &Konnect{
Hosts: make(map[string]*proxy.SSHProxy),
Tasks: make(map[string]*task.SSHTask),
ProxyChan: make(chan bool),
CompletedChan: make(chan bool),
}
Expand Down
21 changes: 21 additions & 0 deletions task/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package task

import "fmt"

// type SSHTask string
type SSHTask struct {
Command string
Name string
}

// String representation of an SSHTask object.
func (t *SSHTask) String() string {
return fmt.Sprintf("<SSHTask %v: %v>", t.Name, t.Command)
}

// Info - Return info for an SSHTask object.
func (t *SSHTask) Info() string {
return fmt.Sprintf("[%v]\n"+
" Command: %v\n",
t.Name, t.Command)
}
9 changes: 9 additions & 0 deletions task/utils_.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package task

// New - Create a new SSHTask object with given values.
func New(name, command string) *SSHTask {
return &SSHTask{
Name: name,
Command: command,
}
}

0 comments on commit ac07067

Please sign in to comment.