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

View file

@ -22,12 +22,19 @@ type StepSSH struct {
var DefaultSSHTemplates = SSHTemplates{ var DefaultSSHTemplates = SSHTemplates{
User: []Template{ User: []Template{
{ {
Name: "include.tpl", Name: "base_config.tpl",
Type: Fragment, Type: Snippet,
TemplatePath: "templates/ssh/include.tpl", TemplatePath: "templates/ssh/base_config.tpl",
Path: "~/.ssh/config", Path: "~/.ssh/config",
Comment: "#", Comment: "#",
}, },
{
Name: "include.tpl",
Type: Line,
TemplatePath: "templates/ssh/include.tpl",
Path: "~/.ssh/include",
Comment: "#",
},
{ {
Name: "config.tpl", Name: "config.tpl",
Type: File, Type: File,
@ -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{
// 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. // include.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 }} "include.tpl": `{{- if or .User.GOOS "none" | eq "windows" }}Include "{{ .User.StepPath | replace "\\" "/" | trimPrefix "C:" }}/ssh/config"{{- else }}Include "{{.User.StepPath}}/ssh/config"{{- end }}`,
{{ 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`,
// 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 }}
`, `,