Skip to content

Commit 2ed213f

Browse files
committed
Initial Commit
Initial Commit
1 parent 8ba0d4a commit 2ed213f

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# kv
2+
3+
Dumb Program to store simple Key-Value notes on persistent storage. Integrates with your command line pipeline so you can pull/store values at any time.
4+
5+
## Get the code
6+
7+
`go get -u github.com/st0le/kv`
8+
9+
## Build
10+
11+
`go build`
12+
13+
## Instructions
14+
15+
You can invoke `kv` in 4 modes. Ideally copy the binary into a location that is in your PATH.
16+
17+
1) list all keys
18+
19+
`kv`
20+
21+
2) get a single key
22+
23+
`kv <key>`
24+
25+
3) set a value
26+
27+
`kv <key> <value>`
28+
29+
4) pipe contents to a key
30+
31+
`echo test | kv <key>`
32+
33+
The values are stored in files named by the key in `$HOME\.kv`. Of course, keys will be unique. Files are not encrypted, I wouldn't use it for anything sensitive like your password, apikeys, etc
34+
35+
## Tip for Windows Powershell Users
36+
37+
Use
38+
39+
`New-Alias -Name kv -Value "kv.exe"`
40+
41+
To avoid typing ".\kv.exe" each time.
42+
43+

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/st0le/kv
2+
3+
go 1.15

main.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"io/ioutil"
7+
"log"
8+
"os"
9+
"os/user"
10+
"path/filepath"
11+
)
12+
13+
func getStorageDirectory() string {
14+
usr, _ := user.Current()
15+
path := filepath.Join(usr.HomeDir, ".kv")
16+
return path
17+
}
18+
19+
func ensureDirectory() string {
20+
path := getStorageDirectory()
21+
return path
22+
}
23+
24+
func list() {
25+
path := getStorageDirectory()
26+
files, err := ioutil.ReadDir(path)
27+
if err != nil {
28+
log.Fatal(err)
29+
}
30+
31+
for _, file := range files {
32+
fmt.Println(file.Name())
33+
}
34+
}
35+
36+
func set(key, value string) {
37+
keyFile := filepath.Join(ensureDirectory(), key)
38+
39+
f, err := os.Create(keyFile)
40+
41+
if err != nil {
42+
log.Fatal(err)
43+
}
44+
45+
defer f.Close()
46+
if value == "" {
47+
io.Copy(f, os.Stdin)
48+
} else {
49+
f.WriteString(value)
50+
}
51+
}
52+
53+
func get(key string) {
54+
keyFile := filepath.Join(getStorageDirectory(), key)
55+
f, err := os.Open(keyFile)
56+
57+
if err != nil {
58+
log.Fatal(err)
59+
}
60+
61+
defer f.Close()
62+
io.Copy(os.Stdout, f)
63+
}
64+
65+
func main() {
66+
67+
var args = os.Args
68+
69+
fi, _ := os.Stdin.Stat()
70+
71+
if (fi.Mode() & os.ModeCharDevice) == 0 {
72+
// if there is a piped stdin, assume its a set command.
73+
if len(args) < 2 {
74+
log.Fatal("specify a key")
75+
} else {
76+
set(args[1], "")
77+
}
78+
79+
} else {
80+
81+
switch len(args) {
82+
case 1:
83+
// list
84+
list()
85+
case 2:
86+
// get
87+
get(args[1])
88+
case 3:
89+
// set
90+
set(args[1], args[2])
91+
}
92+
}
93+
94+
}

0 commit comments

Comments
 (0)