-
-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8cae28c
commit 98336b1
Showing
7 changed files
with
387 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package action | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
// TemplatesPrint will pretty-print a tree of templates | ||
func (s *Action) TemplatesPrint(c *cli.Context) error { | ||
tree, err := s.Store.TemplateTree() | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(tree.Format()) | ||
return nil | ||
} | ||
|
||
// TemplateEdit will load and existing or new template into an | ||
// editor | ||
func (s *Action) TemplateEdit(c *cli.Context) error { | ||
name := c.Args().First() | ||
if name == "" { | ||
return fmt.Errorf("provide a template name") | ||
} | ||
|
||
var content []byte | ||
if s.Store.HasTemplate(name) { | ||
var err error | ||
content, err = s.Store.GetTemplate(name) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
nContent, err := s.editor(content) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// If content is equal, nothing changed, exiting | ||
if bytes.Equal(content, nContent) { | ||
return nil | ||
} | ||
|
||
return s.Store.SetTemplate(name, nContent) | ||
} | ||
|
||
// TemplateRemove will remove a single template | ||
func (s *Action) TemplateRemove(c *cli.Context) error { | ||
name := c.Args().First() | ||
if name == "" { | ||
return fmt.Errorf("provide a template name") | ||
} | ||
|
||
if !s.Store.HasTemplate(name) { | ||
return fmt.Errorf("template not found") | ||
} | ||
|
||
return s.Store.RemoveTemplate(name) | ||
} | ||
|
||
// TemplatesComplete prints a list of all templates for bash completion | ||
func (s *Action) TemplatesComplete(*cli.Context) { | ||
tree, err := s.Store.TemplateTree() | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
for _, v := range tree.List() { | ||
fmt.Println(v) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package password | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/justwatchcom/gopass/fsutil" | ||
"github.com/justwatchcom/gopass/tree" | ||
) | ||
|
||
const ( | ||
// TemplateFile is the name of a pass template | ||
TemplateFile = ".pass-template" | ||
) | ||
|
||
// LookupTemplate will lookup and return a template | ||
func (r *RootStore) LookupTemplate(name string) ([]byte, bool) { | ||
store := r.getStore(name) | ||
return store.LookupTemplate(strings.TrimPrefix(name, store.alias)) | ||
} | ||
|
||
// LookupTemplate will lookup and return a template | ||
func (s *Store) LookupTemplate(name string) ([]byte, bool) { | ||
for { | ||
if !strings.Contains(name, string(filepath.Separator)) { | ||
return []byte{}, false | ||
} | ||
name = filepath.Dir(name) | ||
tpl := filepath.Join(s.path, name, TemplateFile) | ||
if fsutil.IsFile(tpl) { | ||
if content, err := ioutil.ReadFile(tpl); err == nil { | ||
return content, true | ||
} | ||
} | ||
} | ||
} | ||
|
||
// TemplateTree returns a tree of all templates | ||
func (r *RootStore) TemplateTree() (*tree.Folder, error) { | ||
root := tree.New("gopass") | ||
mps := r.mountPoints() | ||
sort.Sort(sort.Reverse(byLen(mps))) | ||
for _, alias := range mps { | ||
substore := r.mounts[alias] | ||
if substore == nil { | ||
continue | ||
} | ||
if err := root.AddMount(alias, substore.path); err != nil { | ||
return nil, fmt.Errorf("failed to add mount: %s", err) | ||
} | ||
for _, t := range substore.ListTemplates(alias) { | ||
// TODO(dschulz) maybe: if err := root.AddFile(t); err != nil { | ||
if err := root.AddFile(alias + "/" + t); err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
} | ||
|
||
for _, t := range r.store.ListTemplates("") { | ||
if err := root.AddFile(t); err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
return root, nil | ||
} | ||
|
||
func mkTemplateStoreWalkerFunc(alias, folder string, fn func(...string)) func(string, os.FileInfo, error) error { | ||
return func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if info.IsDir() && strings.HasPrefix(info.Name(), ".") && path != folder { | ||
return filepath.SkipDir | ||
} | ||
if info.IsDir() { | ||
return nil | ||
} | ||
if info.Name() != TemplateFile { | ||
return nil | ||
} | ||
if path == folder { | ||
return nil | ||
} | ||
if info.Mode()&os.ModeSymlink != 0 { | ||
return nil | ||
} | ||
s := strings.TrimPrefix(path, folder+"/") | ||
s = strings.TrimSuffix(s, "/"+TemplateFile) | ||
if alias != "" { | ||
s = alias + "/" + s | ||
} | ||
fn(s) | ||
return nil | ||
} | ||
} | ||
|
||
// ListTemplates will list all templates in this store | ||
func (s *Store) ListTemplates(prefix string) []string { | ||
lst := make([]string, 0, 10) | ||
addFunc := func(in ...string) { | ||
for _, s := range in { | ||
lst = append(lst, s) | ||
} | ||
} | ||
|
||
if err := filepath.Walk(s.path, mkTemplateStoreWalkerFunc(prefix, s.path, addFunc)); err != nil { | ||
fmt.Printf("Failed to list templates: %s\n", err) | ||
} | ||
|
||
return lst | ||
} | ||
|
||
// templatefile returns the name of the given template on disk | ||
func (s *Store) templatefile(name string) string { | ||
return filepath.Join(s.path, name, TemplateFile) | ||
} | ||
|
||
// HasTemplate returns true if the template exists | ||
func (r *RootStore) HasTemplate(name string) bool { | ||
store := r.getStore(name) | ||
return store.HasTemplate(strings.TrimPrefix(name, store.alias)) | ||
} | ||
|
||
// HasTemplate returns true if the template exists | ||
func (s *Store) HasTemplate(name string) bool { | ||
return fsutil.IsFile(s.templatefile(name)) | ||
} | ||
|
||
// GetTemplate will return the content of the named template | ||
func (r *RootStore) GetTemplate(name string) ([]byte, error) { | ||
store := r.getStore(name) | ||
return store.GetTemplate(strings.TrimPrefix(name, store.alias)) | ||
} | ||
|
||
// GetTemplate will return the content of the named template | ||
func (s *Store) GetTemplate(name string) ([]byte, error) { | ||
return ioutil.ReadFile(s.templatefile(name)) | ||
} | ||
|
||
// SetTemplate will (over)write the content to the template file | ||
func (r *RootStore) SetTemplate(name string, content []byte) error { | ||
store := r.getStore(name) | ||
return store.SetTemplate(strings.TrimPrefix(name, store.alias), content) | ||
} | ||
|
||
// SetTemplate will (over)write the content to the template file | ||
func (s *Store) SetTemplate(name string, content []byte) error { | ||
return ioutil.WriteFile(s.templatefile(name), content, 0600) | ||
} | ||
|
||
// RemoveTemplate will delete the named template if it exists | ||
func (r *RootStore) RemoveTemplate(name string) error { | ||
store := r.getStore(name) | ||
return store.RemoveTemplate(strings.TrimPrefix(name, store.alias)) | ||
} | ||
|
||
// RemoveTemplate will delete the named template if it exists | ||
func (s *Store) RemoveTemplate(name string) error { | ||
t := s.templatefile(name) | ||
if !fsutil.IsFile(t) { | ||
return fmt.Errorf("template not found") | ||
} | ||
|
||
return os.Remove(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package tpl | ||
|
||
import ( | ||
"crypto/md5" | ||
"crypto/sha1" | ||
"fmt" | ||
"text/template" | ||
) | ||
|
||
// These constants defined the template function names used | ||
const ( | ||
FuncMd5sum = "md5sum" | ||
FuncSha1sum = "sha1sum" | ||
FuncGet = "get" | ||
) | ||
|
||
func md5sum() func(...string) (string, error) { | ||
return func(s ...string) (string, error) { | ||
return fmt.Sprintf("%x", md5.Sum([]byte(s[0]))), nil | ||
} | ||
} | ||
|
||
func sha1sum() func(...string) (string, error) { | ||
return func(s ...string) (string, error) { | ||
if len(s) < 2 { | ||
return "", nil | ||
} | ||
return fmt.Sprintf("%x", sha1.Sum([]byte(s[1]))), nil | ||
} | ||
} | ||
|
||
func get(kv kvstore) func(...string) (string, error) { | ||
return func(s ...string) (string, error) { | ||
if len(s) < 2 { | ||
return "", nil | ||
} | ||
buf, err := kv.Get(s[1]) | ||
return string(buf), err | ||
} | ||
} | ||
|
||
func funcMap(kv kvstore) template.FuncMap { | ||
return template.FuncMap{ | ||
FuncGet: get(kv), | ||
FuncMd5sum: md5sum(), | ||
FuncSha1sum: sha1sum(), | ||
} | ||
} |
Oops, something went wrong.