- Line -> PrependLine
- dont' overwrite profileDefaults
- update ssh/config.tpl to always include includes file
This commit is contained in:
max furman 2021-11-11 22:05:47 -08:00
parent 3e9830e363
commit 43cba993bb
3 changed files with 22 additions and 17 deletions

View file

@ -950,9 +950,13 @@ func (p *PKI) Save(opt ...ConfigOption) error {
return errs.FileError(err, p.defaults)
}
if p.profileDefaults != "" {
if _, err := os.Stat(p.profileDefaults); os.IsNotExist(err) {
if err = fileutil.WriteFile(p.profileDefaults, []byte("{}"), 0644); err != nil {
return errs.FileError(err, p.profileDefaults)
}
} else if err != nil {
return errs.FileError(err, p.profileDefaults)
}
}
// Generate and write templates

View file

@ -20,8 +20,9 @@ type TemplateType string
const (
// Snippet will mark a template as a part of a file.
Snippet TemplateType = "snippet"
// Line is a template for a single line in a file.
Line TemplateType = "line"
// PrependLine is a template for prepending a single line to a file. If the
// line already exists in the file it will be removed first.
PrependLine TemplateType = "prepend-line"
// File will mark a templates as a full file.
File TemplateType = "file"
// Directory will mark a template as a directory.
@ -120,8 +121,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 != Line:
return errors.Errorf("invalid template type %s, it must be %s, %s, %s, or %s", t.Type, Snippet, Line, File, Directory)
case t.Type != Snippet && t.Type != File && t.Type != Directory && t.Type != PrependLine:
return errors.Errorf("invalid template type %s, it must be %s, %s, %s, or %s", t.Type, Snippet, PrependLine, 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:
@ -267,7 +268,7 @@ func (o *Output) Write() error {
return fileutil.WriteFile(path, o.Content, 0600)
case Snippet:
return fileutil.WriteSnippet(path, o.Content, 0600)
case Line:
case PrependLine:
return fileutil.PrependLine(path, o.Content, 0600)
default:
return errors.Errorf("unexpected output template type %s", string(o.Type))

View file

@ -22,23 +22,23 @@ type StepSSH struct {
var DefaultSSHTemplates = SSHTemplates{
User: []Template{
{
Name: "base_config.tpl",
Name: "config.tpl",
Type: Snippet,
TemplatePath: "templates/ssh/base_config.tpl",
TemplatePath: "templates/ssh/config.tpl",
Path: "~/.ssh/config",
Comment: "#",
},
{
Name: "includes.tpl",
Type: Line,
TemplatePath: "templates/ssh/includes.tpl",
Name: "step_includes.tpl",
Type: PrependLine,
TemplatePath: "templates/ssh/step_includes.tpl",
Path: "${STEPPATH}/ssh/includes",
Comment: "#",
},
{
Name: "config.tpl",
Name: "step_config.tpl",
Type: File,
TemplatePath: "templates/ssh/config.tpl",
TemplatePath: "templates/ssh/step_config.tpl",
Path: "ssh/config",
Comment: "#",
},
@ -76,16 +76,16 @@ var DefaultSSHTemplateData = map[string]string{
// 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 }}
{{- if .User.StepBasePath }}
Include "{{ .User.StepBasePath | replace "\\" "/" | trimPrefix "C:" }}/ssh/includes"
{{- else }}
Include "{{ .User.StepPath | replace "\\" "/" | trimPrefix "C:" }}/ssh/config"
Include "{{ .User.StepPath | replace "\\" "/" | trimPrefix "C:" }}/ssh/includes"
{{- end }}
{{- else }}
{{- if .User.Authority }}
{{- if .User.StepBasePath }}
Include "{{.User.StepBasePath}}/ssh/includes"
{{- else }}
Include "{{.User.StepPath}}/ssh/config"
Include "{{.User.StepPath}}/ssh/includes"
{{- end }}
{{- end }}`,