Skip to content

Commit

Permalink
Delete config options if value is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
xwjdsh committed Dec 9, 2017
1 parent 769331a commit 5682932
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/manssh/kv_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (kv *kvFlag) Set(value string) error {
kv.m = map[string]string{}
}
parts := strings.Split(value, "=")
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
if len(parts) != 2 || parts[0] == "" {
return fmt.Errorf("flag param(%s) parse error", value)
}
kv.m[parts[0]] = parts[1]
Expand Down
3 changes: 3 additions & 0 deletions cmd/manssh/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func printHost(host *manssh.HostConfig) {
fmt.Printf(" -> %s\n", host.Connect)
}
for k, v := range host.Config {
if v == "" {
continue
}
if isGlobal {
globalKeyStyle.Printf("\t\t%s ", k)
fmt.Printf("= %s\n", v)
Expand Down
36 changes: 32 additions & 4 deletions sshconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package manssh
import (
"io/ioutil"
"os"
"os/user"
"strings"

"github.com/kevinburke/ssh_config"
Expand Down Expand Up @@ -75,6 +76,13 @@ func List(path string, keywords ...string) []*HostConfig {
continue
}
if !isGlobal {
if connectMap[User] == "" {
user, _ := user.Current()
connectMap[User] = user.Username
}
if connectMap[Port] == "" {
connectMap[Port] = "22"
}
h.Connect = FormatConnect(connectMap[User], connectMap[Hostname], connectMap[Port])
}
hosts = append(hosts, h)
Expand Down Expand Up @@ -122,6 +130,9 @@ func Add(path string, host *HostConfig) error {

// Get nodes and delete repeat config
for k, v := range host.Config {
if v == "" {
continue
}
lk := strings.ToLower(k)
node := &ssh_config.KV{Key: lk, Value: v}
nodes = append(nodes, node.SetLeadingSpace(4))
Expand Down Expand Up @@ -183,13 +194,20 @@ func Update(path string, h *HostConfig, newAlias string) error {
connectMap[Port] = ""
}
// update node
for _, node := range updateHost.Nodes {
switch t := node.(type) {

for i := 0; i >= 0 && i < len(updateHost.Nodes); i++ {
switch t := updateHost.Nodes[i].(type) {
case *ssh_config.KV:
t.Key = strings.ToLower(t.Key)
if value, ok := updateKV[t.Key]; ok {
t.SetLeadingSpace(4)
t.Value = value
if value == "" {
// Remove node
updateHost.Nodes = append(updateHost.Nodes[:i], updateHost.Nodes[i+1:]...)
i--
} else {
t.SetLeadingSpace(4)
t.Value = value
}
delete(updateKV, t.Key)
}
if _, ok := connectMap[t.Key]; ok {
Expand All @@ -201,6 +219,9 @@ func Update(path string, h *HostConfig, newAlias string) error {
}
// append new node
for k, v := range updateKV {
if v == "" {
continue
}
kv := &ssh_config.KV{Key: k, Value: v}
updateHost.Nodes = append(updateHost.Nodes, kv.SetLeadingSpace(4))
if _, ok := connectMap[k]; ok {
Expand All @@ -209,6 +230,13 @@ func Update(path string, h *HostConfig, newAlias string) error {
h.Config[k] = v
}
}
if connectMap[User] == "" {
user, _ := user.Current()
connectMap[User] = user.Username
}
if connectMap[Port] == "" {
connectMap[Port] = "22"
}
h.Connect = FormatConnect(connectMap[User], connectMap[Hostname], connectMap[Port])
return ioutil.WriteFile(path, []byte(cfg.String()), 0644)
}
Expand Down

0 comments on commit 5682932

Please # to comment.