From 4dbfd5f7297fdd7a237c3c563fdda79d77f299bb Mon Sep 17 00:00:00 2001
From: Joe <rbo@gmx.us>
Date: Mon, 18 Dec 2023 20:20:20 +0100
Subject: fking good

---
 c_exec.go  | 42 ++++++++++++++++++++++++++++++++----------
 c_init.go  |  2 +-
 c_parse.go |  8 ++++----
 3 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/c_exec.go b/c_exec.go
index 0759526..e759acc 100644
--- a/c_exec.go
+++ b/c_exec.go
@@ -57,36 +57,58 @@ import (
 func exec_cmd(cmd_fmt []string) {
 	cmd := exec.Command(cmd_fmt[0], cmd_fmt[1:]...)
 
+	fmt.Println(cmd_fmt)
 	cmd.Stdin = os.Stdin
 	cmd.Stdout = os.Stdout
 	cmd.Stderr = os.Stderr
 	cmd.Run()
 }
 
+func format_ssh(host *HostNode) []string {
+	cmd_fmt := []string{"ssh"}
+	user := host.User
+
+	if len(host.Priv) > 0 {
+		cmd_fmt = append(cmd_fmt, "-i", host.Priv)
+	}
+	if host.Port != 0 {
+		cmd_fmt = append(cmd_fmt, "-p", strconv.Itoa(int(host.Port)))
+	}
+	if len(host.User) == 0 {
+		user = "root"
+	}
+	cmd_fmt = append(cmd_fmt, user + "@" + host.Host)
+	return cmd_fmt
+}
+
+func format_rdp() {
+}
+
 func format_cmd(id uint64, lhost *HostList) {
-	curr := lhost.head
+	host := lhost.head
 	var cmd_fmt []string
 
-	curr = lhost.sel(id)
-	if curr == nil {
+	host = lhost.sel(id)
+	if host == nil {
 		c_die("host id not found", nil)
 	}
-	cmd_fmt = []string{"ssh", "-i", curr.Priv, "-p", strconv.Itoa(int(curr.Port)), curr.User + "@" + curr.Host}
-	fmt.Println(cmd_fmt)
+	if host.Type == 0 {
+		cmd_fmt = format_ssh(host)
+	}
 	exec_cmd(cmd_fmt)
 }
 
 func display_servers(lhost *HostList) {
-	curr := lhost.head
+	host := lhost.head
 
 	if lhost.head == nil {
 		fmt.Println("no hosts")
 		return
 	}
-	for curr != nil {
-		fmt.Println(curr.ID, curr.Folder + curr.Name)
-		curr = curr.next
+	for host != nil {
+		fmt.Println(host.ID, host.Folder + host.Name)
+		host = host.next
 	}
 	fmt.Println()
-	format_cmd(3, lhost)
+	format_cmd(0, lhost)
 }
diff --git a/c_init.go b/c_init.go
index 3935d17..e06084b 100644
--- a/c_init.go
+++ b/c_init.go
@@ -94,7 +94,7 @@ func c_recurse_data_dir(dir string, root string, lhost *HostList) {
 			c_recurse_data_dir(dir + file.Name() + "/", root, lhost)
 		} else if filepath.Ext(file.Name()) == ".yml" {
 			host := c_read_yaml_file(root + dir + file.Name())
-			if len(host.Name) == 0 {
+			if host == nil {
 				return
 			}
 			host.Filename = file.Name()
diff --git a/c_parse.go b/c_parse.go
index 54d25f9..358b2d3 100644
--- a/c_parse.go
+++ b/c_parse.go
@@ -62,11 +62,11 @@ func c_read_yaml_file(file string) *HostNode {
 	if err = yaml.Unmarshal(yaml_file, &host); err != nil {
 		c_die("error reading yaml file " + file, err)
 	}
-	if len(host.User) == 0 {
-		host.User = "root"
+	if len(host.Name) == 0 {
+		return nil
 	}
-	if host.Port == 0 {
-		host.Port = 22
+	if len(host.Host) == 0 {
+		return nil
 	}
 	return &host
 }
-- 
cgit v1.2.3