diff --git a/config/config.go b/config/config.go index 108be6a..c5abdd1 100644 --- a/config/config.go +++ b/config/config.go @@ -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" @@ -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 diff --git a/host/host.go b/host/host.go index 414c451..71db3fc 100644 --- a/host/host.go +++ b/host/host.go @@ -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 @@ -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) } @@ -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 } } diff --git a/host/host_test.go b/host/host_test.go index 60846b5..25ea61f 100644 --- a/host/host_test.go +++ b/host/host_test.go @@ -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"} @@ -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)