Skip to content

Commit b080b75

Browse files
committed
Template updates to support multiple SSH include snippets
1 parent d777fc2 commit b080b75

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

templates/templates.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type TemplateType string
2020
const (
2121
// Snippet will mark a template as a part of a file.
2222
Snippet TemplateType = "snippet"
23+
// FullSnippet will mark a template that includes header and footer as a part of a file.
24+
FullSnippet TemplateType = "fullSnippet"
2325
// File will mark a templates as a full file.
2426
File TemplateType = "file"
2527
// Directory will mark a template as a directory.
@@ -99,7 +101,7 @@ func (t *SSHTemplates) Validate() (err error) {
99101
return
100102
}
101103

102-
// Template represents on template file.
104+
// Template represents a template file.
103105
type Template struct {
104106
*template.Template
105107
Name string `json:"name"`
@@ -118,8 +120,8 @@ func (t *Template) Validate() error {
118120
return nil
119121
case t.Name == "":
120122
return errors.New("template name cannot be empty")
121-
case t.Type != Snippet && t.Type != File && t.Type != Directory:
122-
return errors.Errorf("invalid template type %s, it must be %s, %s, or %s", t.Type, Snippet, File, Directory)
123+
case t.Type != Snippet && t.Type != File && t.Type != Directory && t.Type != FullSnippet:
124+
return errors.Errorf("invalid template type %s, it must be %s, %s, %s, or %s", t.Type, Snippet, FullSnippet, File, Directory)
123125
case t.TemplatePath == "" && t.Type != Directory && len(t.Content) == 0:
124126
return errors.New("template template cannot be empty")
125127
case t.TemplatePath != "" && t.Type == Directory:
@@ -257,11 +259,17 @@ func (o *Output) Write() error {
257259
return err
258260
}
259261

260-
if o.Type == File {
262+
switch o.Type {
263+
case File:
261264
return fileutil.WriteFile(path, o.Content, 0600)
265+
case Snippet:
266+
return fileutil.WriteSnippet(path, o.Content, 0600)
267+
case FullSnippet:
268+
lines := strings.Split(string(o.Content), "\n")
269+
return fileutil.WriteFullSnippet(path, o.Content, lines[0], lines[len(lines)-1], 0600)
270+
default:
271+
return errors.Errorf("unexpected output template type %s", string(o.Type))
262272
}
263-
264-
return fileutil.WriteSnippet(path, o.Content, 0600)
265273
}
266274

267275
func mkdir(path string, perm os.FileMode) error {

templates/values.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var DefaultSSHTemplates = SSHTemplates{
2323
User: []Template{
2424
{
2525
Name: "include.tpl",
26-
Type: Snippet,
26+
Type: FullSnippet,
2727
TemplatePath: "templates/ssh/include.tpl",
2828
Path: "~/.ssh/config",
2929
Comment: "#",
@@ -67,18 +67,21 @@ var DefaultSSHTemplateData = map[string]string{
6767
// include.tpl adds the step ssh config file.
6868
//
6969
// Note: on windows `Include C:\...` is treated as a relative path.
70-
"include.tpl": `Host *
70+
"include.tpl": `{{- if .User.Authority }}# {{ .User.Authority }}
71+
{{ end }}# autogenerated by step
72+
# @ {{ now }}
73+
Host *
7174
{{- if or .User.GOOS "none" | eq "windows" }}
7275
Include "{{ .User.StepPath | replace "\\" "/" | trimPrefix "C:" }}/ssh/config"
7376
{{- else }}
7477
Include "{{.User.StepPath}}/ssh/config"
75-
{{- end }}`,
78+
{{ end }}# end`,
7679

7780
// config.tpl is the step ssh config file, it includes the Match rule and
7881
// references the step known_hosts file.
7982
//
8083
// Note: on windows ProxyCommand requires the full path
81-
"config.tpl": `Match exec "step ssh check-host %h"
84+
"config.tpl": `Match exec "step ssh{{- if .User.Context }} --context {{ .User.Context }}{{- end }} check-host %h"
8285
{{- if .User.User }}
8386
User {{.User.User}}
8487
{{- end }}

0 commit comments

Comments
 (0)