Skip to content

Commit

Permalink
Use text/template's error reporting mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jun 27, 2019
1 parent 355f650 commit 89ed284
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 51 deletions.
2 changes: 1 addition & 1 deletion cmd/init.go
Expand Up @@ -167,7 +167,7 @@ func (c *Config) promptString(field string) string {
fmt.Fprintf(c.Stdout(), "%s? ", field)
value, err := bufio.NewReader(c.Stdin()).ReadString('\n')
if err != nil {
chezmoi.ReturnTemplateFuncError(err)
panic(err)
}
return strings.TrimSuffix(value, "\n")
}
5 changes: 2 additions & 3 deletions cmd/secretbitwarden.go
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/twpayne/chezmoi/lib/chezmoi"
vfs "github.com/twpayne/go-vfs"
)

Expand Down Expand Up @@ -51,11 +50,11 @@ func (c *Config) bitwardenFunc(args ...string) interface{} {
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
chezmoi.ReturnTemplateFuncError(fmt.Errorf("bitwarden: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
panic(fmt.Errorf("bitwarden: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
}
var data interface{}
if err := json.Unmarshal(output, &data); err != nil {
chezmoi.ReturnTemplateFuncError(fmt.Errorf("bitwarden: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
panic(fmt.Errorf("bitwarden: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
}
bitwardenCache[key] = data
return data
Expand Down
7 changes: 3 additions & 4 deletions cmd/secretgeneric.go
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/twpayne/chezmoi/lib/chezmoi"
vfs "github.com/twpayne/go-vfs"
)

Expand Down Expand Up @@ -54,7 +53,7 @@ func (c *Config) secretFunc(args ...string) interface{} {
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
chezmoi.ReturnTemplateFuncError(fmt.Errorf("secret: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
panic(fmt.Errorf("secret: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
}
value := bytes.TrimSpace(output)
secretCache[key] = value
Expand All @@ -75,11 +74,11 @@ func (c *Config) secretJSONFunc(args ...string) interface{} {
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
chezmoi.ReturnTemplateFuncError(fmt.Errorf("secretJSON: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
panic(fmt.Errorf("secretJSON: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
}
var value interface{}
if err := json.Unmarshal(output, &value); err != nil {
chezmoi.ReturnTemplateFuncError(fmt.Errorf("secretJSON: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
panic(fmt.Errorf("secretJSON: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
}
secretJSONCache[key] = value
return value
Expand Down
5 changes: 2 additions & 3 deletions cmd/secretkeepassxc.go
Expand Up @@ -11,7 +11,6 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/twpayne/chezmoi/lib/chezmoi"
vfs "github.com/twpayne/go-vfs"
"golang.org/x/crypto/ssh/terminal"
)
Expand Down Expand Up @@ -51,7 +50,7 @@ func (c *Config) keePassXCFunc(entry string) interface{} {
return data
}
if c.KeePassXC.Database == "" {
chezmoi.ReturnTemplateFuncError(errors.New("keepassxc: keepassxc.database not set"))
panic(errors.New("keepassxc: keepassxc.database not set"))
}
name := c.KeePassXC.Command
args := []string{"show"}
Expand All @@ -62,7 +61,7 @@ func (c *Config) keePassXCFunc(entry string) interface{} {
}
data, err := c.runKeePassXCCLICommand(name, args)
if err != nil {
chezmoi.ReturnTemplateFuncError(fmt.Errorf("keepassxc: %s %s: %s", name, strings.Join(args, " "), err))
panic(fmt.Errorf("keepassxc: %s %s: %s", name, strings.Join(args, " "), err))
}
keePassXCCache[entry] = data
return data
Expand Down
3 changes: 1 addition & 2 deletions cmd/secretkeyring.go
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/spf13/cobra"
"github.com/twpayne/chezmoi/lib/chezmoi"
keyring "github.com/zalando/go-keyring"
)

Expand Down Expand Up @@ -51,7 +50,7 @@ func (*Config) keyringFunc(service, user string) string {
}
password, err := keyring.Get(service, user)
if err != nil {
chezmoi.ReturnTemplateFuncError(fmt.Errorf("keyring %q %q: %v", service, user, err))
panic(fmt.Errorf("keyring %q %q: %v", service, user, err))
}
keyringCache[key] = password
return password
Expand Down
9 changes: 4 additions & 5 deletions cmd/secretlastpass.go
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/coreos/go-semver/semver"
"github.com/spf13/cobra"
"github.com/twpayne/chezmoi/lib/chezmoi"
vfs "github.com/twpayne/go-vfs"
)

Expand Down Expand Up @@ -71,19 +70,19 @@ func (c *Config) lastpassOutput(args ...string) ([]byte, error) {
func (c *Config) lastpassFunc(id string) interface{} {
c.Lastpass.versionCheckOnce.Do(func() {
if err := c.lastpassVersionCheck(); err != nil {
chezmoi.ReturnTemplateFuncError(err)
panic(err)
}
})
if data, ok := lastPassCache[id]; ok {
return data
}
output, err := c.lastpassOutput("show", "--json", id)
if err != nil {
chezmoi.ReturnTemplateFuncError(err)
panic(err)
}
var data []map[string]interface{}
if err := json.Unmarshal(output, &data); err != nil {
chezmoi.ReturnTemplateFuncError(fmt.Errorf("lastpass: parse error: %v\n%q", err, output))
panic(fmt.Errorf("lastpass: parse error: %v\n%q", err, output))
}
for _, d := range data {
if note, ok := d["note"].(string); ok {
Expand Down Expand Up @@ -130,7 +129,7 @@ func lastpassParseNote(note string) map[string]string {
}
}
if err := s.Err(); err != nil {
chezmoi.ReturnTemplateFuncError(fmt.Errorf("lastpassParseNote: %v", err))
panic(fmt.Errorf("lastpassParseNote: %v", err))
}
return result
}
5 changes: 2 additions & 3 deletions cmd/secretonepassword.go
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/twpayne/chezmoi/lib/chezmoi"
vfs "github.com/twpayne/go-vfs"
)

Expand Down Expand Up @@ -50,11 +49,11 @@ func (c *Config) onepasswordFunc(item string) interface{} {
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
chezmoi.ReturnTemplateFuncError(fmt.Errorf("onepassword: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
panic(fmt.Errorf("onepassword: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
}
var data interface{}
if err := json.Unmarshal(output, &data); err != nil {
chezmoi.ReturnTemplateFuncError(fmt.Errorf("onepassword: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
panic(fmt.Errorf("onepassword: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
}
onepasswordCache[item] = data
return data
Expand Down
3 changes: 1 addition & 2 deletions cmd/secretpass.go
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/twpayne/chezmoi/lib/chezmoi"
vfs "github.com/twpayne/go-vfs"
)

Expand Down Expand Up @@ -46,7 +45,7 @@ func (c *Config) passFunc(id string) string {
}
output, err := exec.Command(name, args...).Output()
if err != nil {
chezmoi.ReturnTemplateFuncError(fmt.Errorf("pass: %s %s: %v", name, strings.Join(args, " "), err))
panic(fmt.Errorf("pass: %s %s: %v", name, strings.Join(args, " "), err))
}
var password string
if index := bytes.IndexByte(output, '\n'); index != -1 {
Expand Down
5 changes: 2 additions & 3 deletions cmd/secretvault.go
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/twpayne/chezmoi/lib/chezmoi"
vfs "github.com/twpayne/go-vfs"
)

Expand Down Expand Up @@ -50,11 +49,11 @@ func (c *Config) vaultFunc(key string) interface{} {
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
chezmoi.ReturnTemplateFuncError(fmt.Errorf("vault: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
panic(fmt.Errorf("vault: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
}
var data interface{}
if err := json.Unmarshal(output, &data); err != nil {
chezmoi.ReturnTemplateFuncError(fmt.Errorf("vault: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
panic(fmt.Errorf("vault: %s %s: %v\n%s", name, strings.Join(args, " "), err, output))
}
vaultCache[key] = data
return data
Expand Down
13 changes: 0 additions & 13 deletions lib/chezmoi/chezmoi.go
Expand Up @@ -26,12 +26,6 @@ const (
TemplateSuffix = ".tmpl"
)

// A templateFuncError is an error encountered while executing a template
// function.
type templateFuncError struct {
err error
}

// A PersistentState is an interface to a persistent state.
type PersistentState interface {
Delete(bucket, key []byte) error
Expand Down Expand Up @@ -68,13 +62,6 @@ type parsedSourceFilePath struct {
scriptAttributes *ScriptAttributes
}

// ReturnTemplateFuncError causes template execution to return an error.
func ReturnTemplateFuncError(err error) {
panic(templateFuncError{
err: err,
})
}

// dirNames returns the dir names from dirAttributes.
func dirNames(dirAttributes []DirAttributes) []string {
dns := make([]string, len(dirAttributes))
Expand Down
3 changes: 1 addition & 2 deletions lib/chezmoi/chezmoi_test.go
Expand Up @@ -10,8 +10,7 @@ import (
func TestReturnTemplateError(t *testing.T) {
funcs := map[string]interface{}{
"returnTemplateError": func() string {
ReturnTemplateFuncError(errors.New("error"))
return "foo"
panic(errors.New("error"))
},
}
for name, dataString := range map[string]string{
Expand Down
11 changes: 1 addition & 10 deletions lib/chezmoi/targetstate.go
Expand Up @@ -657,7 +657,7 @@ func (ts *TargetState) executeTemplate(fs vfs.FS, path string) ([]byte, error) {
return ts.executeTemplateData(path, data)
}

func (ts *TargetState) executeTemplateData(name string, data []byte) (_ []byte, err error) {
func (ts *TargetState) executeTemplateData(name string, data []byte) ([]byte, error) {
tmpl, err := template.New(name).Option("missingkey=error").Funcs(ts.TemplateFuncs).Parse(string(data))
if err != nil {
return nil, err
Expand All @@ -668,15 +668,6 @@ func (ts *TargetState) executeTemplateData(name string, data []byte) (_ []byte,
return nil, err
}
}
defer func() {
if r := recover(); r != nil {
if tfe, ok := r.(templateFuncError); ok {
err = tfe.err
} else {
panic(r)
}
}
}()
output := &bytes.Buffer{}
if err = tmpl.Execute(output, ts.Data); err != nil {
return nil, err
Expand Down

0 comments on commit 89ed284

Please sign in to comment.