1
0
mirror of https://github.com/ssh-vault/ssh-vault.git synced 2025-07-31 05:24:22 +03:00

cache, http client, vault

This commit is contained in:
nbari
2016-09-30 18:14:54 +02:00
parent 2d936ab639
commit 4b820cc7dc
6 changed files with 158 additions and 128 deletions

50
cache.go Normal file
View File

@ -0,0 +1,50 @@
package sshvault
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
)
type cache struct {
dir string
}
func Cache() *cache {
usr, _ := user.Current()
sv := filepath.Join(usr.HomeDir, ".ssh-vault", "keys")
if _, err := os.Stat(sv); os.IsNotExist(err) {
os.MkdirAll(sv, os.ModePerm)
}
return &cache{sv}
}
func (c *cache) Get(u string) (string, error) {
uKey := fmt.Sprintf("%s/%s.key", c.dir, u)
if c.isFile(uKey) {
// read from file and return
}
key, err := GetKey(u)
if err != nil {
return "", err
}
err = ioutil.WriteFile(uKey, []byte(key), 0644)
if err != nil {
log.Println(err)
}
return key, nil
}
func (c *cache) isFile(path string) bool {
f, err := os.Stat(path)
if err != nil {
return false
}
if m := f.Mode(); !m.IsDir() && m.IsRegular() && m&400 != 0 {
return true
}
return false
}