-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
4 changed files
with
111 additions
and
20 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package vdir | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/boltdb/bolt" | ||
) | ||
|
||
var bucketName = "todos" | ||
|
||
func dbPath() (dbPath string, err error) { | ||
userCacheDir, err := os.UserCacheDir() | ||
if err != nil { | ||
return "", fmt.Errorf("get cache dir: %w", err) | ||
} | ||
cacheDir := filepath.Join(userCacheDir, "tdx") | ||
if err := os.MkdirAll(cacheDir, 0700); err != nil { | ||
return "", fmt.Errorf("create cache dir: %w", err) | ||
} | ||
dbPath = filepath.Join(cacheDir, "db") | ||
return | ||
} | ||
|
||
func updateDB() error { | ||
path, err := dbPath() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
db, err := bolt.Open(path, 0644, nil) | ||
if err != nil { | ||
return fmt.Errorf("open db: %w", err) | ||
} | ||
defer db.Close() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
key := []byte("hey") | ||
val := []byte("world") | ||
|
||
err = db.Update(func(tx *bolt.Tx) error { | ||
bucket, err := tx.CreateBucketIfNotExists([]byte(bucketName)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = bucket.Put(key, val) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
func viewDB() (string, error) { | ||
path, err := dbPath() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
db, err := bolt.Open(path, 0644, nil) | ||
if err != nil { | ||
return "", fmt.Errorf("open db: %w", err) | ||
} | ||
defer db.Close() | ||
|
||
key := []byte("hey") | ||
var val []byte | ||
|
||
err = db.View(func(tx *bolt.Tx) error { | ||
bucket := tx.Bucket([]byte(bucketName)) | ||
if bucket == nil { | ||
return fmt.Errorf("Bucket %q not found", bucketName) | ||
} | ||
val = bucket.Get(key) | ||
|
||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
fmt.Println(string(key), string(val)) | ||
|
||
return string(val), nil | ||
} |
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