Skip to content

Commit 1b0d050

Browse files
marainodopey
authored andcommitted
Add Write method to templates.Output.
1 parent b792d5c commit 1b0d050

File tree

2 files changed

+56
-22
lines changed

2 files changed

+56
-22
lines changed

pki/templates.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@ import (
44
"os"
55
"path/filepath"
66

7-
"github.com/smallstep/cli/utils"
8-
97
"github.com/pkg/errors"
108
"github.com/smallstep/certificates/templates"
9+
"github.com/smallstep/cli/config"
1110
"github.com/smallstep/cli/errs"
11+
"github.com/smallstep/cli/utils"
1212
)
1313

1414
// sshTemplates contains the configuration of default templates used on ssh.
15+
// Relative paths are relative to the StepPath.
1516
var sshTemplates = &templates.SSHTemplates{
1617
User: []templates.Template{
17-
{Name: "include.tpl", Type: templates.Snippet, TemplatePath: "ssh/include.tpl", Path: "~/.ssh/config", Comment: "#"},
18-
{Name: "config.tpl", Type: templates.File, TemplatePath: "ssh/config.tpl", Path: "ssh/config", Comment: "#"},
19-
{Name: "known_hosts.tpl", Type: templates.File, TemplatePath: "ssh/known_hosts.tpl", Path: "ssh/known_hosts", Comment: "#"},
18+
{Name: "include.tpl", Type: templates.Snippet, TemplatePath: "templates/ssh/include.tpl", Path: "~/.ssh/config", Comment: "#"},
19+
{Name: "config.tpl", Type: templates.File, TemplatePath: "templates/ssh/config.tpl", Path: "ssh/config", Comment: "#"},
20+
{Name: "known_hosts.tpl", Type: templates.File, TemplatePath: "templates/ssh/known_hosts.tpl", Path: "ssh/known_hosts", Comment: "#"},
2021
},
2122
Host: []templates.Template{
22-
{Name: "sshd_config.tpl", Type: templates.Snippet, TemplatePath: "ssh/sshd_config.tpl", Path: "/etc/ssh/sshd_config", Comment: "#"},
23-
{Name: "ca.tpl", Type: templates.Snippet, TemplatePath: "ssh/ca.tpl", Path: "/etc/ssh/ca.pub", Comment: "#"},
23+
{Name: "sshd_config.tpl", Type: templates.Snippet, TemplatePath: "templates/ssh/sshd_config.tpl", Path: "/etc/ssh/sshd_config", Comment: "#"},
24+
{Name: "ca.tpl", Type: templates.Snippet, TemplatePath: "templates/ssh/ca.tpl", Path: "/etc/ssh/ca.pub", Comment: "#"},
2425
},
2526
}
2627

@@ -36,7 +37,7 @@ var sshTemplateData = map[string]string{
3637
ForwardAgent yes
3738
UserKnownHostsFile {{.User.StepPath}}/config/ssh/known_hosts`,
3839

39-
// known_hosts.tpl authorizes the ssh hosst key
40+
// known_hosts.tpl authorizes the ssh hosts key
4041
"known_hosts.tpl": "@cert-authority * {{.Step.SSH.HostKey.Type}} {{.Step.SSH.HostKey.Marshal | toString | b64enc}}",
4142

4243
// sshd_config.tpl adds the configuration to support certificates
@@ -82,7 +83,7 @@ func generateTemplates(t *templates.Templates) error {
8283
if !ok {
8384
return errors.Errorf("template %s does not exists", t.Name)
8485
}
85-
if err := utils.WriteFile(filepath.Join(base, t.TemplatePath), []byte(data), 0644); err != nil {
86+
if err := utils.WriteFile(config.StepAbs(t.TemplatePath), []byte(data), 0644); err != nil {
8687
return err
8788
}
8889
}
@@ -91,7 +92,7 @@ func generateTemplates(t *templates.Templates) error {
9192
if !ok {
9293
return errors.Errorf("template %s does not exists", t.Name)
9394
}
94-
if err := utils.WriteFile(filepath.Join(base, t.TemplatePath), []byte(data), 0644); err != nil {
95+
if err := utils.WriteFile(config.StepAbs(t.TemplatePath), []byte(data), 0644); err != nil {
9596
return err
9697
}
9798
}

templates/templates.go

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ package templates
33
import (
44
"bytes"
55
"io/ioutil"
6+
"os"
7+
"path/filepath"
68
"text/template"
79

810
"github.com/Masterminds/sprig"
911
"github.com/pkg/errors"
12+
"github.com/smallstep/cli/config"
13+
"github.com/smallstep/cli/utils"
1014
)
1115

1216
// TemplateType defines how a template will be written in disk.
@@ -17,17 +21,10 @@ const (
1721
Snippet TemplateType = "snippet"
1822
// File will mark a templates as a full file.
1923
File TemplateType = "file"
24+
// Directory will mark a template as a directory.
25+
Directory TemplateType = "directory"
2026
)
2127

22-
// Output represents the text representation of a rendered template.
23-
type Output struct {
24-
Name string `json:"name"`
25-
Type TemplateType `json:"type"`
26-
Comment string `json:"comment"`
27-
Path string `json:"path"`
28-
Content []byte `json:"content"`
29-
}
30-
3128
// Templates is a collection of templates and variables.
3229
type Templates struct {
3330
SSH *SSHTemplates `json:"ssh,omitempty"`
@@ -137,13 +134,14 @@ func (t *Template) Validate() error {
137134
// template fails.
138135
func (t *Template) Load() error {
139136
if t.Template == nil {
140-
b, err := ioutil.ReadFile(t.TemplatePath)
137+
filename := config.StepAbs(t.TemplatePath)
138+
b, err := ioutil.ReadFile(filename)
141139
if err != nil {
142-
return errors.Wrapf(err, "error reading %s", t.TemplatePath)
140+
return errors.Wrapf(err, "error reading %s", filename)
143141
}
144142
tmpl, err := template.New(t.Name).Funcs(sprig.TxtFuncMap()).Parse(string(b))
145143
if err != nil {
146-
return errors.Wrapf(err, "error parsing %s", t.TemplatePath)
144+
return errors.Wrapf(err, "error parsing %s", filename)
147145
}
148146
t.Template = tmpl
149147
}
@@ -179,3 +177,38 @@ func (t *Template) Output(data interface{}) (Output, error) {
179177
Content: b,
180178
}, nil
181179
}
180+
181+
// Output represents the text representation of a rendered template.
182+
type Output struct {
183+
Name string `json:"name"`
184+
Type TemplateType `json:"type"`
185+
Comment string `json:"comment"`
186+
Path string `json:"path"`
187+
Content []byte `json:"content"`
188+
}
189+
190+
// Write writes the Output to the filesystem as a directory, file or snippet.
191+
func (o *Output) Write() error {
192+
path := config.StepAbs(o.Path)
193+
if o.Type == Directory {
194+
return mkdir(path, 0700)
195+
}
196+
197+
dir := filepath.Dir(path)
198+
if err := mkdir(dir, 0700); err != nil {
199+
return err
200+
}
201+
202+
if o.Type == File {
203+
return utils.WriteFile(path, o.Content, 0600)
204+
}
205+
206+
return utils.WriteSnippet(path, o.Content, 0600)
207+
}
208+
209+
func mkdir(path string, perm os.FileMode) error {
210+
if err := os.MkdirAll(path, perm); err != nil {
211+
return errors.Wrapf(err, "error creating %s", path)
212+
}
213+
return nil
214+
}

0 commit comments

Comments
 (0)