Replace Fragment template with Line

This commit is contained in:
max furman 2021-11-10 00:42:57 -08:00
parent da74fa2eb9
commit 74eea88343
2 changed files with 37 additions and 21 deletions

View file

@ -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))
}

View file

@ -22,12 +22,19 @@ type StepSSH struct {
var DefaultSSHTemplates = SSHTemplates{
User: []Template{
{
Name: "include.tpl",
Type: Fragment,
TemplatePath: "templates/ssh/include.tpl",
Name: "base_config.tpl",
Type: Snippet,
TemplatePath: "templates/ssh/base_config.tpl",
Path: "~/.ssh/config",
Comment: "#",
},
{
Name: "include.tpl",
Type: Line,
TemplatePath: "templates/ssh/include.tpl",
Path: "~/.ssh/include",
Comment: "#",
},
{
Name: "config.tpl",
Type: File,
@ -64,18 +71,28 @@ var DefaultSSHTemplates = SSHTemplates{
// DefaultSSHTemplateData contains the data of the default templates used on ssh.
var DefaultSSHTemplateData = map[string]string{
// base_config.tpl adds the step ssh config file.
//
// Note: on windows `Include C:\...` is treated as a relative path.
"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 }}`,
// include.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 *
{{- if or .User.GOOS "none" | eq "windows" }}
Include "{{ .User.StepPath | replace "\\" "/" | trimPrefix "C:" }}/ssh/config"
{{- else }}
Include "{{.User.StepPath}}/ssh/config"
{{ end }}# end`,
"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 }}
`,