diff --git a/templates/templates.go b/templates/templates.go index bfc5b596..ab0f1040 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -20,8 +20,8 @@ type TemplateType string const ( // Snippet will mark a template as a part of a file. Snippet TemplateType = "snippet" - // Fragment will mark a template that includes header and footer as a part of a file. - Fragment TemplateType = "fragment" + // Line is a template for a single line in a file. + Line TemplateType = "line" // File will mark a templates as a full file. File TemplateType = "file" // Directory will mark a template as a directory. @@ -120,8 +120,8 @@ func (t *Template) Validate() error { return nil case t.Name == "": return errors.New("template name cannot be empty") - case t.Type != Snippet && t.Type != File && t.Type != Directory && t.Type != Fragment: - return errors.Errorf("invalid template type %s, it must be %s, %s, %s, or %s", t.Type, Snippet, Fragment, File, Directory) + case t.Type != Snippet && t.Type != File && t.Type != Directory && t.Type != Line: + return errors.Errorf("invalid template type %s, it must be %s, %s, %s, or %s", t.Type, Snippet, Line, File, Directory) case t.TemplatePath == "" && t.Type != Directory && len(t.Content) == 0: return errors.New("template template cannot be empty") case t.TemplatePath != "" && t.Type == Directory: @@ -264,9 +264,8 @@ func (o *Output) Write() error { return fileutil.WriteFile(path, o.Content, 0600) case Snippet: return fileutil.WriteSnippet(path, o.Content, 0600) - case Fragment: - lines := strings.Split(string(o.Content), "\n") - return fileutil.WriteFragment(path, o.Content, lines[0], lines[len(lines)-1], 0600) + case Line: + return fileutil.WriteLine(path, o.Content, 0600) default: return errors.Errorf("unexpected output template type %s", string(o.Type)) } diff --git a/templates/values.go b/templates/values.go index 3749f9f0..60c0ad8f 100644 --- a/templates/values.go +++ b/templates/values.go @@ -21,11 +21,18 @@ type StepSSH struct { // Relative paths are relative to the StepPath. var DefaultSSHTemplates = SSHTemplates{ User: []Template{ + { + Name: "base_config.tpl", + Type: Snippet, + TemplatePath: "templates/ssh/base_config.tpl", + Path: "~/.ssh/config", + Comment: "#", + }, { Name: "include.tpl", - Type: Fragment, + Type: Line, TemplatePath: "templates/ssh/include.tpl", - Path: "~/.ssh/config", + Path: "~/.ssh/include", Comment: "#", }, { @@ -64,18 +71,28 @@ var DefaultSSHTemplates = SSHTemplates{ // DefaultSSHTemplateData contains the data of the default templates used on ssh. var DefaultSSHTemplateData = map[string]string{ - // include.tpl adds the step ssh config file. + // base_config.tpl adds the step ssh config file. // // Note: on windows `Include C:\...` is treated as a relative path. - "include.tpl": `{{- if .User.Authority }}# {{ .User.Authority }} -{{ end }}# autogenerated by step -# @ {{ now }} -Host * + "base_config.tpl": `Host * {{- if or .User.GOOS "none" | eq "windows" }} +{{- if .User.Authority }} + Include "{{ .User.Home | replace "\\" "/" | trimPrefix "C:" }}/.ssh/include" +{{- else }} Include "{{ .User.StepPath | replace "\\" "/" | trimPrefix "C:" }}/ssh/config" +{{- end }} +{{- else }} +{{- if .User.Authority }} + Include "{{.User.Home}}/.ssh/include" {{- else }} Include "{{.User.StepPath}}/ssh/config" -{{ end }}# end`, +{{- end }} +{{- end }}`, + + // include.tpl adds the step ssh config file. + // + // Note: on windows `Include C:\...` is treated as a relative path. + "include.tpl": `{{- if or .User.GOOS "none" | eq "windows" }}Include "{{ .User.StepPath | replace "\\" "/" | trimPrefix "C:" }}/ssh/config"{{- else }}Include "{{.User.StepPath}}/ssh/config"{{- end }}`, // config.tpl is the step ssh config file, it includes the Match rule and // references the step known_hosts file. @@ -87,10 +104,10 @@ Host * {{- end }} {{- if or .User.GOOS "none" | eq "windows" }} UserKnownHostsFile "{{.User.StepPath}}\ssh\known_hosts" - ProxyCommand C:\Windows\System32\cmd.exe /c step ssh proxycommand %r %h %p + ProxyCommand C:\Windows\System32\cmd.exe /c step ssh proxycommand{{- if .User.Context }} --context {{ .User.Context }}{{- end }} %r %h %p {{- else }} UserKnownHostsFile "{{.User.StepPath}}/ssh/known_hosts" - ProxyCommand step ssh proxycommand %r %h %p + ProxyCommand step ssh proxycommand{{- if .User.Context }} --context {{ .User.Context }}{{- end }} %r %h %p {{- end }} `,