Replace Fragment template with Line

pull/692/head
max furman 3 years ago
parent da74fa2eb9
commit 74eea88343

@ -20,8 +20,8 @@ type TemplateType string
const ( const (
// Snippet will mark a template as a part of a file. // Snippet will mark a template as a part of a file.
Snippet TemplateType = "snippet" Snippet TemplateType = "snippet"
// Fragment will mark a template that includes header and footer as a part of a file. // Line is a template for a single line in a file.
Fragment TemplateType = "fragment" Line TemplateType = "line"
// File will mark a templates as a full file. // File will mark a templates as a full file.
File TemplateType = "file" File TemplateType = "file"
// Directory will mark a template as a directory. // Directory will mark a template as a directory.
@ -120,8 +120,8 @@ func (t *Template) Validate() error {
return nil return nil
case t.Name == "": case t.Name == "":
return errors.New("template name cannot be empty") return errors.New("template name cannot be empty")
case t.Type != Snippet && t.Type != File && t.Type != Directory && t.Type != Fragment: 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, Fragment, File, Directory) 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: case t.TemplatePath == "" && t.Type != Directory && len(t.Content) == 0:
return errors.New("template template cannot be empty") return errors.New("template template cannot be empty")
case t.TemplatePath != "" && t.Type == Directory: case t.TemplatePath != "" && t.Type == Directory:
@ -264,9 +264,8 @@ func (o *Output) Write() error {
return fileutil.WriteFile(path, o.Content, 0600) return fileutil.WriteFile(path, o.Content, 0600)
case Snippet: case Snippet:
return fileutil.WriteSnippet(path, o.Content, 0600) return fileutil.WriteSnippet(path, o.Content, 0600)
case Fragment: case Line:
lines := strings.Split(string(o.Content), "\n") return fileutil.WriteLine(path, o.Content, 0600)
return fileutil.WriteFragment(path, o.Content, lines[0], lines[len(lines)-1], 0600)
default: default:
return errors.Errorf("unexpected output template type %s", string(o.Type)) return errors.Errorf("unexpected output template type %s", string(o.Type))
} }

@ -21,11 +21,18 @@ type StepSSH struct {
// Relative paths are relative to the StepPath. // Relative paths are relative to the StepPath.
var DefaultSSHTemplates = SSHTemplates{ var DefaultSSHTemplates = SSHTemplates{
User: []Template{ User: []Template{
{
Name: "base_config.tpl",
Type: Snippet,
TemplatePath: "templates/ssh/base_config.tpl",
Path: "~/.ssh/config",
Comment: "#",
},
{ {
Name: "include.tpl", Name: "include.tpl",
Type: Fragment, Type: Line,
TemplatePath: "templates/ssh/include.tpl", TemplatePath: "templates/ssh/include.tpl",
Path: "~/.ssh/config", Path: "~/.ssh/include",
Comment: "#", Comment: "#",
}, },
{ {
@ -64,18 +71,28 @@ var DefaultSSHTemplates = SSHTemplates{
// DefaultSSHTemplateData contains the data of the default templates used on ssh. // DefaultSSHTemplateData contains the data of the default templates used on ssh.
var DefaultSSHTemplateData = map[string]string{ 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. // Note: on windows `Include C:\...` is treated as a relative path.
"include.tpl": `{{- if .User.Authority }}# {{ .User.Authority }} "base_config.tpl": `Host *
{{ end }}# autogenerated by step
# @ {{ now }}
Host *
{{- if or .User.GOOS "none" | eq "windows" }} {{- 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" Include "{{ .User.StepPath | replace "\\" "/" | trimPrefix "C:" }}/ssh/config"
{{- end }}
{{- else }}
{{- if .User.Authority }}
Include "{{.User.Home}}/.ssh/include"
{{- else }} {{- else }}
Include "{{.User.StepPath}}/ssh/config" 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 // config.tpl is the step ssh config file, it includes the Match rule and
// references the step known_hosts file. // references the step known_hosts file.
@ -87,10 +104,10 @@ Host *
{{- end }} {{- end }}
{{- if or .User.GOOS "none" | eq "windows" }} {{- if or .User.GOOS "none" | eq "windows" }}
UserKnownHostsFile "{{.User.StepPath}}\ssh\known_hosts" 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 }} {{- else }}
UserKnownHostsFile "{{.User.StepPath}}/ssh/known_hosts" 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 }} {{- end }}
`, `,

Loading…
Cancel
Save