mirror of
https://github.com/ssh-vault/ssh-vault.git
synced 2025-07-29 18:01:12 +03:00
save cache
This commit is contained in:
36
cache.go
36
cache.go
@ -13,6 +13,7 @@ type cache struct {
|
|||||||
dir string
|
dir string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cache creates ~/.ssh-vault
|
||||||
func Cache() *cache {
|
func Cache() *cache {
|
||||||
usr, _ := user.Current()
|
usr, _ := user.Current()
|
||||||
sv := filepath.Join(usr.HomeDir, ".ssh-vault", "keys")
|
sv := filepath.Join(usr.HomeDir, ".ssh-vault", "keys")
|
||||||
@ -22,29 +23,20 @@ func Cache() *cache {
|
|||||||
return &cache{sv}
|
return &cache{sv}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get return ssh-key
|
||||||
func (c *cache) Get(u string) (string, error) {
|
func (c *cache) Get(u string) (string, error) {
|
||||||
uKey := fmt.Sprintf("%s/%s.key", c.dir, u)
|
keyPath := fmt.Sprintf("%s/%s.key", c.dir, u)
|
||||||
if c.isFile(uKey) {
|
key, err := ioutil.ReadFile(keyPath)
|
||||||
// read from file and return
|
|
||||||
}
|
|
||||||
key, err := GetKey(u)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
key, err := GetKey(u)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
err = ioutil.WriteFile(keyPath, []byte(key), 0644)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
return key, nil
|
||||||
}
|
}
|
||||||
err = ioutil.WriteFile(uKey, []byte(key), 0644)
|
return string(key), err
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
const GITHUB = "https://github.com"
|
const GITHUB = "https://github.com"
|
||||||
|
|
||||||
|
// GetKey fetches ssh-key from url
|
||||||
func GetKey(u string) (string, error) {
|
func GetKey(u string) (string, error) {
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
// create a new request
|
// create a new request
|
||||||
|
Reference in New Issue
Block a user