Skip to content

Commit

Permalink
[FEATURE] Better listing of configured fields
Browse files Browse the repository at this point in the history
  • Loading branch information
lthurston committed Dec 14, 2014
1 parent 3f13394 commit 07ebc97
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ var configCmd = &cobra.Command{
Long: `Shows all configuration values if no arguments are provided, otherwise modifies with argument key/value pairs`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("file: " + config.GetConfigFile())
fmt.Println("list.fields: " + config.GetConfigListFields())
},
}
18 changes: 16 additions & 2 deletions commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"fmt"
"strings"

"github.com/lthurston/quiet/config"
"github.com/lthurston/quiet/parser"
Expand All @@ -16,9 +17,22 @@ var listCmd = &cobra.Command{
hosts := parser.HostsCollection{}
hosts.ReadFromFile(config.GetConfigFile())
for _, host := range hosts.Hosts {
fmt.Println(host.Name)
fmt.Println(host.Config)
configFields := formatHostConfigFields(config.GetConfigListFields(), host.Config)
fmt.Printf("%-20s %s", host.Name, configFields)
fmt.Println("")
}
},
}

func formatHostConfigFields(fieldsString string, configs map[string]string) string {
out := ""
fields := strings.Split(fieldsString, ",")
for key, value := range configs {
for _, field := range fields {
if key == strings.TrimSpace(field) {
out = out + field + ": " + value + " "
}
}
}
return out
}
12 changes: 10 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
goTomlConfig "github.com/stvp/go-toml-config"
)

var configFile = goTomlConfig.String("file", getHomeDir()+"/.ssh/config")
var (
configFile = goTomlConfig.String("file", getHomeDir()+"/.ssh/config")
configListFields = goTomlConfig.String("list.fields", "User, Hostname")
)

// quietConfig stores the location of the quiet config, defaults to "~/.quiet"
var quietConfig = ""
Expand All @@ -18,11 +21,16 @@ func Parseable(quietConfig string) bool {
return goTomlConfig.Parse(quietConfig) != nil
}

// GetConfigFile returns a config value
// GetConfigFile returns `file`
func GetConfigFile() string {
return *configFile
}

// GetConfigListFields returns `list.show`
func GetConfigListFields() string {
return *configListFields
}

// GetConfigMap returns a map of all configuration values
func GetConfigMap() map[string]string {
configMap := make(map[string]string)
Expand Down
1 change: 1 addition & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (hosts *HostsCollection) fromScanner(s *bufio.Scanner) {
hosts.Hosts = append(hosts.Hosts, host)
}
foundFirstHostLine = true
host = makeHost()
host.StartLine = lineIndex
host.Name = getHostName(line)
} else {
Expand Down
6 changes: 3 additions & 3 deletions quiet.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"github.com/lthurston/quiet/commands"
"github.com/lthurston/quiet/commands"
)

func main() {
commands.Execute()
}
commands.Execute()
}

0 comments on commit 07ebc97

Please sign in to comment.