Skip to content

Commit

Permalink
[F] Allow configurable host templatee
Browse files Browse the repository at this point in the history
This is the first step in #14
  • Loading branch information
lthurston committed Mar 12, 2016
1 parent 8279776 commit 9347313
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
15 changes: 12 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import (
)

var (
configFile = goTomlConfig.String("file", util.GetHomeDir()+"/.ssh/config")
configListFields = goTomlConfig.String("list.fields", "User, Hostname")
configNewFrom = goTomlConfig.String("new.from", "")
configFile = goTomlConfig.String("file", util.GetHomeDir()+"/.ssh/config")
configListFields = goTomlConfig.String("list.fields", "User, Hostname")
configNewFrom = goTomlConfig.String("new.from", "")
configExportFilenameOptions = goTomlConfig.String("export.filenameOptions", "IdentityFile")
configHostTemplate = goTomlConfig.String("template", `
Host {{.Name}}{{if .Aliases}}{{range .Aliases}} {{.}}{{end}}{{end}}
{{range .Options }} {{.}}
{{end}}`)
)

// quietConfig stores the location of the quiet config, defaults to "~/.quiet"
Expand Down Expand Up @@ -44,6 +48,11 @@ func GetConfigExportFilenameOptions() string {
return *configExportFilenameOptions
}

// GetConfigTemplate returns the template
func GetConfigTemplate() string {
return *configHostTemplate
}

// SetQuietConfig sets the location of the quiet config file
func SetQuietConfig(qc string) {
quietConfig = qc
Expand Down
25 changes: 19 additions & 6 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import (
"strings"
"text/template"
"regexp"
"github.com/lthurston/quiet/config"
)

const defaultHostTemplate = `
Host {{.Name}}{{if .Aliases}}{{range .Aliases}} {{.}}{{end}}{{end}}
{{range .Options }} {{.}}
{{end}}`

// Option contains a keyword and an argument
type Option struct {
keyword string
Expand Down Expand Up @@ -99,12 +105,14 @@ func (host Host) Options() []Option {
return host.options
}

// RenderSnippet renders a host snippet
// String renders a host snippet
func (host Host) String() string {
tmpl, err := template.New("snip").Parse(`
Host {{.Name}}{{if .Aliases}}{{range .Aliases}} {{.}}{{end}}{{end}}
{{range .Options }} {{.}}
{{end}}`)
return host.RenderHostTemplate(configTemplate())
}

// RenderHostTemplate renders the host, given a template string
func (host Host) RenderHostTemplate(templateString string) string {
tmpl, err := template.New("snip").Parse(templateString)
if err != nil {
panic(err)
}
Expand All @@ -117,12 +125,17 @@ Host {{.Name}}{{if .Aliases}}{{range .Aliases}} {{.}}{{end}}{{end}}
return buf.String()
}


func configTemplate() string {
return config.GetConfigTemplate()
}

// ContainsStrings returns true if the host contains the findStrings
func (host Host) ContainsStrings(findStrings []string) bool {
for _, findString := range findStrings {
escArg := regexp.QuoteMeta(findString)
re := regexp.MustCompile("(?i)(" + escArg +")")
if(!re.Match([]byte(host.String()))) {
if(!re.Match([]byte(host.RenderHostTemplate(defaultHostTemplate)))) {
return false
}
}
Expand Down
7 changes: 6 additions & 1 deletion host/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
h "github.com/lthurston/quiet/host"
)

const testTemplate = `
Host {{.Name}}{{if .Aliases}}{{range .Aliases}} {{.}}{{end}}{{end}}
{{range .Options }} {{.}}
{{end}}`

func TestString(t *testing.T) {
name := "skeins"
aliases := []string{"sizzle", "toast", "blort"}
Expand All @@ -19,7 +24,7 @@ func TestString(t *testing.T) {
host.AddOptionFromString(line)
}

rendered := host.String()
rendered := host.RenderHostTemplate(testTemplate)

hosts := h.HostsCollection{}
hosts.ReadFromString(rendered)
Expand Down

0 comments on commit 9347313

Please sign in to comment.