mirror of
https://github.com/sbrow/envr.git
synced 2026-06-28 02:58:33 -04:00
Compare commits
45 Commits
623c2f5f29
...
release-pl
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7df83b064e | ||
| fe2b256bd6 | |||
| 41decd9cdb | |||
| 397f45d4d0 | |||
| 73a41830d1 | |||
| e17d04c93d | |||
| 4600c81401 | |||
| ec96dff055 | |||
| 4a26ee8145 | |||
| e23ea960d7 | |||
| 3db86f0d2e | |||
| 567cc8b1e2 | |||
| fe3253f274 | |||
| f6ffeeee65 | |||
| 23d5ff5e01 | |||
| 4599b25b1b | |||
| e32f0ea6d2 | |||
| 650c91d51b | |||
| ad3ce748bb | |||
| 930c3d4c5d | |||
| b1b0449b7b | |||
| 0a74b0dbcc | |||
| d56f11250c | |||
| 23b8c2dc67 | |||
| 2f4a7887ea | |||
| 5eee6cd6ea | |||
| 67f735a654 | |||
| 7d16dae4f4 | |||
| 365e9149b1 | |||
| 1068458f32 | |||
| 22a517340a | |||
| fcee4ca7b1 | |||
| dff5235d65 | |||
| 5865315161 | |||
| 191ba305ef | |||
| d890c88b6d | |||
| f8add2ad22 | |||
| 2de7e20f5c | |||
| 8dd6b17cb9 | |||
| 83b940337c | |||
| 83a8caf691 | |||
| 1964698e35 | |||
| de2186a2e5 | |||
| cb51a398ad | |||
| e989b88303 |
1
.github/workflows/release-please.yml
vendored
1
.github/workflows/release-please.yml
vendored
@@ -25,3 +25,4 @@ jobs:
|
|||||||
# this is a built-in strategy in release-please, see "Action Inputs"
|
# this is a built-in strategy in release-please, see "Action Inputs"
|
||||||
# for more options
|
# for more options
|
||||||
release-type: simple
|
release-type: simple
|
||||||
|
target-branch: ${{ github.ref_name }}
|
||||||
|
|||||||
18
CHANGELOG.md
18
CHANGELOG.md
@@ -1,5 +1,23 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## [0.3.0](https://github.com/sbrow/envr/compare/v0.2.1...v0.3.0) (2026-06-16)
|
||||||
|
|
||||||
|
|
||||||
|
### ⚠ BREAKING CHANGES
|
||||||
|
|
||||||
|
* The encryption format of databases has changed. Age encryption is no longer supported, and no automatic migration path was implemented.
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* All encryption/decryption now happens in-memory. ([fe2b256](https://github.com/sbrow/envr/commit/fe2b256bd61eaf551d53faf3893b473a64a94667))
|
||||||
|
* Config can be loaded from any path with `--config-file (-c)` flag. ([4a26ee8](https://github.com/sbrow/envr/commit/4a26ee814591e6aab0eb99d2359d51b31011edfe))
|
||||||
|
* Switched from age to libsodium. ([23b8c2d](https://github.com/sbrow/envr/commit/23b8c2dc671a23cf76cf6746b33806ded9381486))
|
||||||
|
|
||||||
|
|
||||||
|
### Performance Improvements
|
||||||
|
|
||||||
|
* Improved writer performance. ([365e914](https://github.com/sbrow/envr/commit/365e9149b1a738ac9119bb5f74dc7e047ecfed5b))
|
||||||
|
|
||||||
## [0.2.1](https://github.com/sbrow/envr/compare/v0.2.0...v0.2.1) (2026-01-12)
|
## [0.2.1](https://github.com/sbrow/envr/compare/v0.2.0...v0.2.1) (2026-01-12)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
19
TODOS.md
19
TODOS.md
@@ -1,12 +1,7 @@
|
|||||||
# TODO
|
# TODOs
|
||||||
|
|
||||||
Note: These todos can wait until all the subcommands have been ported.
|
|
||||||
|
|
||||||
## HIGH
|
1. Consider giving db its own allocator
|
||||||
|
|
||||||
1. **db.odin:380-383, 405, 446** — `sqlite.bind_text` return values overwritten but never checked. A failed bind means `sqlite.step` operates on unbound params.
|
|
||||||
|
|
||||||
## MEDIUM
|
|
||||||
|
|
||||||
2. **db.odin:324-327** — Map iteration (`remote_set`) is non-deterministic. Same file can produce different JSON on each backup, causing spurious DB diffs. Sort remotes before storing.
|
2. **db.odin:324-327** — Map iteration (`remote_set`) is non-deterministic. Same file can produce different JSON on each backup, causing spurious DB diffs. Sort remotes before storing.
|
||||||
|
|
||||||
@@ -18,22 +13,14 @@ Note: These todos can wait until all the subcommands have been ported.
|
|||||||
|
|
||||||
6. **cmd_restore.odin:44** — `os.mkdir_all` error silently discarded. Subsequent write failure will be confusing.
|
6. **cmd_restore.odin:44** — `os.mkdir_all` error silently discarded. Subsequent write failure will be confusing.
|
||||||
|
|
||||||
7. **cmd_edit_config.odin:27** — `$EDITOR` used as single binary name. Breaks for multi-word values like `"code -w"`. Needs `strings.fields()`.
|
|
||||||
|
|
||||||
8. **config.odin:178** — `search_paths` silently ignores `os.user_home_dir` error. If home is empty, `~` isn't expanded. Same class of bug as issue 3.
|
8. **config.odin:178** — `search_paths` silently ignores `os.user_home_dir` error. If home is empty, `~` isn't expanded. Same class of bug as issue 3.
|
||||||
|
|
||||||
9. **prompt.odin:124** — `make([dynamic]bool, len(options))` creates N zero-initialized elements. Works because `false` is the default, but same footgun as original issue 1. Should be `make([dynamic]bool, 0, len(options))`.
|
|
||||||
|
|
||||||
## LOW
|
|
||||||
|
|
||||||
10. **db.odin:115** — `json.unmarshal_string` error not checked. Malformed JSON silently produces empty/partial data.
|
10. **db.odin:115** — `json.unmarshal_string` error not checked. Malformed JSON silently produces empty/partial data.
|
||||||
|
|
||||||
11. **db.odin:352-353** — `hex.encode` error ignored. `string(hex_bytes)` aliases the byte slice.
|
11. **db.odin:352-353** — `hex.encode` error ignored. `string(hex_bytes)` aliases the byte slice.
|
||||||
|
|
||||||
12. **cmd_sync.odin:80, cmd_list.odin:33, cmd_deps.odin:9** — `make([]string, 2)` for table rows never freed. Leaks per row. Defer to memory pass.
|
12. **cmd_sync.odin:80, cmd_list.odin:33, cmd_deps.odin:9** — `make([]string, 2)` for table rows never freed. Leaks per row. Defer to memory pass.
|
||||||
|
|
||||||
## REFACTOR
|
|
||||||
|
|
||||||
13. **cmd_list.odin** — Non-TTY branch builds `ListEntry` structs and marshals JSON separately. Now that `render_json_rows` (issue 1) accepts an `io.Writer` and uses `json.marshal`, unify both branches to use it. Note: will change JSON keys from `"directory"/"path"` to `"Directory"/"Path"`.
|
13. **cmd_list.odin** — Non-TTY branch builds `ListEntry` structs and marshals JSON separately. Now that `render_json_rows` (issue 1) accepts an `io.Writer` and uses `json.marshal`, unify both branches to use it. Note: will change JSON keys from `"directory"/"path"` to `"Directory"/"Path"`.
|
||||||
|
|
||||||
14. Check for prealloc opportunities. i.e. `make([dynamic]string)` -> `make([dynamic]string, 5)`.
|
14. Check for prealloc opportunities. i.e. `make([dynamic]string)` -> `make([dynamic]string, 5)`.
|
||||||
@@ -54,6 +41,8 @@ Note: These todos can wait until all the subcommands have been ported.
|
|||||||
|
|
||||||
22. Change struct field names from PascalCase to snake_case.
|
22. Change struct field names from PascalCase to snake_case.
|
||||||
|
|
||||||
|
23. procedures should be ordered by use, main at the top, then in the order they are called from main.
|
||||||
|
|
||||||
## Double-check AI output
|
## Double-check AI output
|
||||||
|
|
||||||
- [ ] cli.odin
|
- [ ] cli.odin
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ envr_dir :: proc(config_path: string) -> string {
|
|||||||
return filepath.dir(config_path)
|
return filepath.dir(config_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
data_encrypted_path :: proc(config_path: string) -> string {
|
data_path :: proc(config_path: string) -> string {
|
||||||
path, _ := filepath.join([]string{envr_dir(config_path), "data.envr"})
|
path, _ := filepath.join([]string{envr_dir(config_path), "data.envr"})
|
||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
@@ -126,6 +126,7 @@ find_ssh_private_keys :: proc() -> (keys: [dynamic]string, ok: bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Caller is responsible for calling delete_config()
|
||||||
new_config :: proc(
|
new_config :: proc(
|
||||||
private_key_paths: []string,
|
private_key_paths: []string,
|
||||||
cfg_path: string = "~/.envr/config.json",
|
cfg_path: string = "~/.envr/config.json",
|
||||||
|
|||||||
@@ -163,8 +163,8 @@ test_envr_dir :: proc(t: ^testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@(test)
|
@(test)
|
||||||
test_data_encrypted_path :: proc(t: ^testing.T) {
|
test_data_path :: proc(t: ^testing.T) {
|
||||||
p := data_encrypted_path("/tmp/envr-fake-home-datapath/config.json")
|
p := data_path("/tmp/envr-fake-home-datapath/config.json")
|
||||||
defer delete(p)
|
defer delete(p)
|
||||||
testing.expectf(t, strings.has_suffix(p, "data.envr"), "should end with data.envr, got %s", p)
|
testing.expectf(t, strings.has_suffix(p, "data.envr"), "should end with data.envr, got %s", p)
|
||||||
testing.expectf(t, strings.contains(p, ".envr"), "should contain .envr dir, got %s", p)
|
testing.expectf(t, strings.contains(p, ".envr"), "should contain .envr dir, got %s", p)
|
||||||
|
|||||||
121
db.odin
121
db.odin
@@ -66,7 +66,7 @@ db_open :: proc(cfg_path: string) -> (Db, bool) {
|
|||||||
return Db{}, false
|
return Db{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
data_path := data_encrypted_path(cfg.config_path)
|
data_path := data_path(cfg.config_path)
|
||||||
_, stat_err := os.stat(data_path, context.allocator)
|
_, stat_err := os.stat(data_path, context.allocator)
|
||||||
|
|
||||||
db: ^rawptr
|
db: ^rawptr
|
||||||
@@ -95,32 +95,31 @@ db_open :: proc(cfg_path: string) -> (Db, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
db_close :: proc(d: ^Db) {
|
db_close :: proc(d: ^Db) {
|
||||||
|
defer sqlite.db_close(d.db)
|
||||||
|
|
||||||
if d.changed {
|
if d.changed {
|
||||||
tmp_path := make_temp_path()
|
rc := sqlite.db_exec(d.db, "VACUUM", nil, nil, nil)
|
||||||
|
if rc != sqlite.OK {
|
||||||
if !db_vacuum_to_file(d.db, tmp_path) {
|
fmt.printf("Error vacuuming database: %s\n", sqlite.db_errmsg(d.db))
|
||||||
os.remove(tmp_path)
|
|
||||||
sqlite.db_close(d.db)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlite_data, read_err := os.read_entire_file_from_path(tmp_path, context.allocator)
|
sz: i64
|
||||||
os.remove(tmp_path)
|
data := sqlite.serialize(d.db, "main", &sz, 0)
|
||||||
if read_err != nil {
|
if data == nil {
|
||||||
fmt.printf("Error reading vacuumed database: %v\n", read_err)
|
fmt.println("Error: failed to serialize database")
|
||||||
sqlite.db_close(d.db)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer sqlite.free(data)
|
||||||
|
|
||||||
|
sqlite_data := data[:sz]
|
||||||
encrypted, enc_ok := encrypt(sqlite_data, d.cfg.Keys[:])
|
encrypted, enc_ok := encrypt(sqlite_data, d.cfg.Keys[:])
|
||||||
delete(sqlite_data)
|
|
||||||
if !enc_ok {
|
if !enc_ok {
|
||||||
fmt.println("Error: encryption failed")
|
fmt.println("Error: encryption failed")
|
||||||
sqlite.db_close(d.db)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data_path := data_encrypted_path(d.cfg.config_path)
|
data_path := data_path(d.cfg.config_path)
|
||||||
envr_d := envr_dir(d.cfg.config_path)
|
envr_d := envr_dir(d.cfg.config_path)
|
||||||
os.mkdir_all(envr_d)
|
os.mkdir_all(envr_d)
|
||||||
|
|
||||||
@@ -128,13 +127,11 @@ db_close :: proc(d: ^Db) {
|
|||||||
delete(encrypted)
|
delete(encrypted)
|
||||||
if write_err != nil {
|
if write_err != nil {
|
||||||
fmt.printf("Error writing encrypted database: %v\n", write_err)
|
fmt.printf("Error writing encrypted database: %v\n", write_err)
|
||||||
sqlite.db_close(d.db)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
d.changed = false
|
d.changed = false
|
||||||
}
|
}
|
||||||
sqlite.db_close(d.db)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Caller is responsible for calling:
|
// Caller is responsible for calling:
|
||||||
@@ -192,22 +189,12 @@ db_list :: proc(d: ^Db, allocator := context.allocator) -> (results: [dynamic]En
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
db_vacuum_to_file :: proc(db: ^rawptr, path: string) -> bool {
|
|
||||||
b: strings.Builder
|
|
||||||
strings.builder_init(&b)
|
|
||||||
defer strings.builder_destroy(&b)
|
|
||||||
fmt.sbprintf(&b, "VACUUM INTO '%s'", path)
|
|
||||||
rc := sqlite.db_exec(db, to_cstring(&b), nil, nil, nil)
|
|
||||||
if rc != sqlite.OK {
|
|
||||||
fmt.printf("Error vacuuming database: %s\n", sqlite.db_errmsg(db))
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
db_restore_from_encrypted :: proc(db: ^rawptr, cfg: Config) -> bool {
|
db_restore_from_encrypted :: proc(db: ^rawptr, cfg: Config) -> bool {
|
||||||
data_path := data_encrypted_path(cfg.config_path)
|
encrypted_data, read_err := os.read_entire_file_from_path(
|
||||||
encrypted_data, read_err := os.read_entire_file_from_path(data_path, context.temp_allocator)
|
data_path(cfg.config_path),
|
||||||
|
context.allocator,
|
||||||
|
)
|
||||||
|
defer delete(encrypted_data)
|
||||||
if read_err != nil {
|
if read_err != nil {
|
||||||
fmt.printf("Error reading encrypted database: %v\n", read_err)
|
fmt.printf("Error reading encrypted database: %v\n", read_err)
|
||||||
return false
|
return false
|
||||||
@@ -220,50 +207,32 @@ db_restore_from_encrypted :: proc(db: ^rawptr, cfg: Config) -> bool {
|
|||||||
}
|
}
|
||||||
defer delete(plaintext)
|
defer delete(plaintext)
|
||||||
|
|
||||||
tmp_path := make_temp_path()
|
n := i64(len(plaintext))
|
||||||
write_err := os.write_entire_file(tmp_path, plaintext)
|
buf := sqlite.malloc64(n)
|
||||||
if write_err != nil {
|
if buf == nil {
|
||||||
fmt.printf("Error writing temp database: %v\n", write_err)
|
fmt.println("Error: failed to allocate buffer for deserialization")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
defer os.remove(tmp_path)
|
copy(buf[:len(plaintext)], plaintext)
|
||||||
|
|
||||||
if !db_attach_and_copy(db, tmp_path) {
|
rc := sqlite.deserialize(
|
||||||
return false
|
db,
|
||||||
}
|
"main",
|
||||||
|
buf,
|
||||||
return true
|
n,
|
||||||
}
|
n,
|
||||||
|
sqlite.DESERIALIZE_FREEONCLOSE | sqlite.DESERIALIZE_RESIZEABLE,
|
||||||
db_attach_and_copy :: proc(mem_db: ^rawptr, src_path: string) -> bool {
|
|
||||||
b: strings.Builder
|
|
||||||
strings.builder_init(&b)
|
|
||||||
defer strings.builder_destroy(&b)
|
|
||||||
fmt.sbprintf(&b, "ATTACH DATABASE '%s' AS source", src_path)
|
|
||||||
|
|
||||||
rc := sqlite.db_exec(mem_db, to_cstring(&b), nil, nil, nil)
|
|
||||||
if rc != sqlite.OK {
|
|
||||||
fmt.printf("Error attaching database: %s\n", sqlite.db_errmsg(mem_db))
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = sqlite.db_exec(
|
|
||||||
mem_db,
|
|
||||||
"INSERT INTO main.envr_env_files SELECT * FROM source.envr_env_files",
|
|
||||||
nil,
|
|
||||||
nil,
|
|
||||||
nil,
|
|
||||||
)
|
)
|
||||||
if rc != sqlite.OK {
|
if rc != sqlite.OK {
|
||||||
fmt.printf("Error copying data: %s\n", sqlite.db_errmsg(mem_db))
|
sqlite.free(buf)
|
||||||
sqlite.db_exec(mem_db, "DETACH DATABASE source", nil, nil, nil)
|
fmt.printf("Error deserializing database: %s\n", sqlite.db_errmsg(db))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlite.db_exec(mem_db, "DETACH DATABASE source", nil, nil, nil)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
get_git_remotes :: proc(dir: string) -> [dynamic]string {
|
get_git_remotes :: proc(dir: string) -> [dynamic]string {
|
||||||
remotes: [dynamic]string
|
remotes: [dynamic]string
|
||||||
remote_set: map[string]bool
|
remote_set: map[string]bool
|
||||||
@@ -382,18 +351,34 @@ db_insert :: proc(d: ^Db, file: EnvFile) -> bool {
|
|||||||
cpath := to_cstring(file.Path)
|
cpath := to_cstring(file.Path)
|
||||||
defer delete(cpath)
|
defer delete(cpath)
|
||||||
rc = sqlite.bind_text(stmt, 1, cpath, -1, nil)
|
rc = sqlite.bind_text(stmt, 1, cpath, -1, nil)
|
||||||
|
if rc != sqlite.OK {
|
||||||
|
fmt.printf("Error binding path: %s\n", sqlite.db_errmsg(d.db))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
cremotes := to_cstring(string(remotes_json))
|
cremotes := to_cstring(string(remotes_json))
|
||||||
defer delete(cremotes)
|
defer delete(cremotes)
|
||||||
rc = sqlite.bind_text(stmt, 2, cremotes, -1, nil)
|
rc = sqlite.bind_text(stmt, 2, cremotes, -1, nil)
|
||||||
|
if rc != sqlite.OK {
|
||||||
|
fmt.printf("Error binding remotes: %s\n", sqlite.db_errmsg(d.db))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
csha := to_cstring(file.Sha256)
|
csha := to_cstring(file.Sha256)
|
||||||
defer delete(csha)
|
defer delete(csha)
|
||||||
rc = sqlite.bind_text(stmt, 3, csha, -1, nil)
|
rc = sqlite.bind_text(stmt, 3, csha, -1, nil)
|
||||||
|
if rc != sqlite.OK {
|
||||||
|
fmt.printf("Error binding sha256: %s\n", sqlite.db_errmsg(d.db))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
ccontents := to_cstring(file.contents)
|
ccontents := to_cstring(file.contents)
|
||||||
defer delete(ccontents)
|
defer delete(ccontents)
|
||||||
rc = sqlite.bind_text(stmt, 4, ccontents, -1, nil)
|
rc = sqlite.bind_text(stmt, 4, ccontents, -1, nil)
|
||||||
|
if rc != sqlite.OK {
|
||||||
|
fmt.printf("Error binding contents: %s\n", sqlite.db_errmsg(d.db))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
rc = sqlite.step(stmt)
|
rc = sqlite.step(stmt)
|
||||||
if rc != sqlite.DONE {
|
if rc != sqlite.DONE {
|
||||||
@@ -418,6 +403,10 @@ db_fetch :: proc(d: ^Db, path: string, allocator := context.allocator) -> (EnvFi
|
|||||||
cpath := to_cstring(path, allocator)
|
cpath := to_cstring(path, allocator)
|
||||||
defer delete(cpath, allocator)
|
defer delete(cpath, allocator)
|
||||||
rc = sqlite.bind_text(stmt, 1, cpath, -1, nil)
|
rc = sqlite.bind_text(stmt, 1, cpath, -1, nil)
|
||||||
|
if rc != sqlite.OK {
|
||||||
|
fmt.printf("Error binding path: %s\n", sqlite.db_errmsg(d.db))
|
||||||
|
return EnvFile{}, false
|
||||||
|
}
|
||||||
rc = sqlite.step(stmt)
|
rc = sqlite.step(stmt)
|
||||||
if rc == sqlite.DONE {
|
if rc == sqlite.DONE {
|
||||||
fmt.printf("No file found with path: %s\n", path)
|
fmt.printf("No file found with path: %s\n", path)
|
||||||
@@ -459,6 +448,10 @@ db_delete :: proc(d: ^Db, path: string) -> bool {
|
|||||||
cpath := to_cstring(path)
|
cpath := to_cstring(path)
|
||||||
defer delete(cpath)
|
defer delete(cpath)
|
||||||
rc = sqlite.bind_text(stmt, 1, cpath, -1, nil)
|
rc = sqlite.bind_text(stmt, 1, cpath, -1, nil)
|
||||||
|
if rc != sqlite.OK {
|
||||||
|
fmt.printf("Error binding path: %s\n", sqlite.db_errmsg(d.db))
|
||||||
|
return false
|
||||||
|
}
|
||||||
rc = sqlite.step(stmt)
|
rc = sqlite.step(stmt)
|
||||||
if rc != sqlite.DONE {
|
if rc != sqlite.DONE {
|
||||||
fmt.printf("Error deleting: %s\n", sqlite.db_errmsg(d.db))
|
fmt.printf("Error deleting: %s\n", sqlite.db_errmsg(d.db))
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ test_encrypt_write_read_decrypt :: proc(t: ^testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@(test)
|
@(test)
|
||||||
test_decrypt_then_attach_sqlite :: proc(t: ^testing.T) {
|
test_decrypt_then_deserialize_sqlite :: proc(t: ^testing.T) {
|
||||||
cfg := fixture_config()
|
cfg := fixture_config()
|
||||||
defer {
|
defer {
|
||||||
delete(cfg.Keys)
|
delete(cfg.Keys)
|
||||||
@@ -164,14 +164,6 @@ test_decrypt_then_attach_sqlite :: proc(t: ^testing.T) {
|
|||||||
}
|
}
|
||||||
defer delete(plaintext)
|
defer delete(plaintext)
|
||||||
|
|
||||||
tmp_db_path := fmt.tprintf("/tmp/envr-test-attach-%d.db", os.get_pid())
|
|
||||||
write_err := os.write_entire_file(tmp_db_path, plaintext)
|
|
||||||
testing.expectf(t, write_err == nil, "failed to write temp db: %v", write_err)
|
|
||||||
if write_err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer os.remove(tmp_db_path)
|
|
||||||
|
|
||||||
mem_db: ^rawptr
|
mem_db: ^rawptr
|
||||||
rc := sqlite.db_open(":memory:", &mem_db)
|
rc := sqlite.db_open(":memory:", &mem_db)
|
||||||
testing.expectf(t, rc == sqlite.OK, "failed to open in-memory db")
|
testing.expectf(t, rc == sqlite.OK, "failed to open in-memory db")
|
||||||
@@ -180,12 +172,25 @@ test_decrypt_then_attach_sqlite :: proc(t: ^testing.T) {
|
|||||||
}
|
}
|
||||||
defer sqlite.db_close(mem_db)
|
defer sqlite.db_close(mem_db)
|
||||||
|
|
||||||
create_sql: cstring = "CREATE TABLE IF NOT EXISTS envr_env_files (path TEXT PRIMARY KEY NOT NULL, remotes TEXT, sha256 TEXT NOT NULL, contents TEXT NOT NULL)"
|
n := i64(len(plaintext))
|
||||||
rc = sqlite.db_exec(mem_db, create_sql, nil, nil, nil)
|
buf := sqlite.malloc64(n)
|
||||||
testing.expect(t, rc == sqlite.OK, "failed to create table")
|
testing.expect(t, buf != nil, "malloc64 should succeed")
|
||||||
|
if buf == nil do return
|
||||||
|
copy(buf[:len(plaintext)], plaintext)
|
||||||
|
|
||||||
attach_ok := db_attach_and_copy(mem_db, tmp_db_path)
|
rc = sqlite.deserialize(
|
||||||
testing.expect(t, attach_ok, "failed to attach and copy")
|
mem_db,
|
||||||
|
"main",
|
||||||
|
buf,
|
||||||
|
n,
|
||||||
|
n,
|
||||||
|
sqlite.DESERIALIZE_FREEONCLOSE | sqlite.DESERIALIZE_RESIZEABLE,
|
||||||
|
)
|
||||||
|
testing.expect(t, rc == sqlite.OK, "deserialize should succeed")
|
||||||
|
if rc != sqlite.OK {
|
||||||
|
sqlite.free(buf)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
sql: cstring = "SELECT path FROM envr_env_files"
|
sql: cstring = "SELECT path FROM envr_env_files"
|
||||||
stmt: ^rawptr
|
stmt: ^rawptr
|
||||||
|
|||||||
21
db_test.odin
21
db_test.odin
@@ -215,7 +215,7 @@ test_db_delete_sets_changed :: proc(t: ^testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@(test)
|
@(test)
|
||||||
test_db_vacuum_to_file :: proc(t: ^testing.T) {
|
test_db_serialize :: proc(t: ^testing.T) {
|
||||||
d, ok := make_test_db()
|
d, ok := make_test_db()
|
||||||
testing.expect(t, ok, "failed to create test db")
|
testing.expect(t, ok, "failed to create test db")
|
||||||
if !ok do return
|
if !ok do return
|
||||||
@@ -225,20 +225,13 @@ test_db_vacuum_to_file :: proc(t: ^testing.T) {
|
|||||||
defer delete(f.Remotes)
|
defer delete(f.Remotes)
|
||||||
db_insert(&d, f)
|
db_insert(&d, f)
|
||||||
|
|
||||||
vacuum_path := fmt.tprintf("/tmp/envr-test-vacuum-%d.db", os.get_pid())
|
sz: i64
|
||||||
defer os.remove(vacuum_path)
|
data := sqlite.serialize(d.db, "main", &sz, 0)
|
||||||
|
testing.expect(t, data != nil, "serialize should return non-nil")
|
||||||
|
if data == nil do return
|
||||||
|
defer sqlite.free(data)
|
||||||
|
|
||||||
testing.expect(t, db_vacuum_to_file(d.db, vacuum_path), "vacuum should succeed")
|
testing.expect(t, sz > 0, "serialized size should be > 0")
|
||||||
|
|
||||||
info, stat_err := os.stat(vacuum_path, context.allocator)
|
|
||||||
defer os.file_info_delete(info, context.allocator)
|
|
||||||
testing.expect(t, stat_err == nil, "vacuumed file should exist")
|
|
||||||
|
|
||||||
data, read_err := os.read_entire_file_from_path(vacuum_path, context.allocator)
|
|
||||||
testing.expect(t, read_err == nil, "should read vacuumed file")
|
|
||||||
defer delete(data)
|
|
||||||
|
|
||||||
testing.expect(t, len(data) > 0, "vacuumed file should be non-empty")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@(test)
|
@(test)
|
||||||
|
|||||||
28
flake.nix
28
flake.nix
@@ -11,11 +11,12 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
outputs =
|
outputs =
|
||||||
inputs@{ flake-parts
|
inputs@{
|
||||||
, nixpkgs
|
flake-parts,
|
||||||
, nixpkgs-unstable
|
nixpkgs,
|
||||||
, self
|
nixpkgs-unstable,
|
||||||
, treefmt-nix
|
self,
|
||||||
|
treefmt-nix,
|
||||||
}:
|
}:
|
||||||
flake-parts.lib.mkFlake { inherit inputs; } {
|
flake-parts.lib.mkFlake { inherit inputs; } {
|
||||||
imports = [
|
imports = [
|
||||||
@@ -29,7 +30,18 @@
|
|||||||
];
|
];
|
||||||
|
|
||||||
perSystem =
|
perSystem =
|
||||||
{ pkgs, system, inputs', ... }: {
|
{
|
||||||
|
pkgs,
|
||||||
|
system,
|
||||||
|
inputs',
|
||||||
|
...
|
||||||
|
}:
|
||||||
|
let
|
||||||
|
mysqlite = pkgs.sqlite.overrideAttrs (old: {
|
||||||
|
configureFlags = (old.configureFlags or [ ]) ++ [ "--enable-deserialize" ];
|
||||||
|
});
|
||||||
|
in
|
||||||
|
{
|
||||||
_module.args.pkgs = import nixpkgs {
|
_module.args.pkgs = import nixpkgs {
|
||||||
inherit system;
|
inherit system;
|
||||||
config.allowUnfree = true;
|
config.allowUnfree = true;
|
||||||
@@ -64,7 +76,7 @@
|
|||||||
|
|
||||||
buildInputs = [
|
buildInputs = [
|
||||||
pkgs.libsodium
|
pkgs.libsodium
|
||||||
pkgs.sqlite
|
mysqlite
|
||||||
];
|
];
|
||||||
|
|
||||||
buildPhase = ''
|
buildPhase = ''
|
||||||
@@ -87,7 +99,7 @@
|
|||||||
nushell
|
nushell
|
||||||
|
|
||||||
libsodium
|
libsodium
|
||||||
sqlite
|
mysqlite
|
||||||
unstable.odin
|
unstable.odin
|
||||||
unstable.ols
|
unstable.ols
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import "core:fmt"
|
|||||||
import "core:os"
|
import "core:os"
|
||||||
|
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
|
defer free_all(context.temp_allocator)
|
||||||
|
|
||||||
cmd, ok := parse_args(os.args, os.to_writer(os.stdout), os.to_writer(os.stderr))
|
cmd, ok := parse_args(os.args, os.to_writer(os.stdout), os.to_writer(os.stderr))
|
||||||
defer bufio.writer_flush(cmd.out_buf)
|
defer bufio.writer_flush(cmd.out_buf)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
192
prompt.odin
192
prompt.odin
@@ -3,36 +3,9 @@ package main
|
|||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:sys/posix"
|
import "core:sys/posix"
|
||||||
|
|
||||||
Raw_State :: struct {
|
MultiSelect_Result :: enum {
|
||||||
original: posix.termios,
|
Confirm,
|
||||||
fd: posix.FD,
|
Cancel,
|
||||||
}
|
|
||||||
|
|
||||||
enable_raw_mode :: proc(fd: posix.FD) -> (Raw_State, bool) {
|
|
||||||
state: Raw_State
|
|
||||||
state.fd = fd
|
|
||||||
|
|
||||||
if posix.tcgetattr(fd, &state.original) != .OK {
|
|
||||||
return state, false
|
|
||||||
}
|
|
||||||
|
|
||||||
attr: posix.termios = state.original
|
|
||||||
attr.c_lflag -= {.ICANON, .ECHO, .ISIG, .IEXTEN}
|
|
||||||
attr.c_iflag -= {.IXON, .ICRNL, .BRKINT, .INPCK, .ISTRIP}
|
|
||||||
attr.c_oflag -= {.OPOST}
|
|
||||||
attr.c_cflag += {.CS8}
|
|
||||||
attr.c_cc[.VMIN] = 1
|
|
||||||
attr.c_cc[.VTIME] = 0
|
|
||||||
|
|
||||||
if posix.tcsetattr(fd, .TCSAFLUSH, &attr) != .OK {
|
|
||||||
return state, false
|
|
||||||
}
|
|
||||||
|
|
||||||
return state, true
|
|
||||||
}
|
|
||||||
|
|
||||||
disable_raw_mode :: proc(state: ^Raw_State) {
|
|
||||||
posix.tcsetattr(state.fd, .TCSAFLUSH, &state.original)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Key :: enum {
|
Key :: enum {
|
||||||
@@ -44,71 +17,9 @@ Key :: enum {
|
|||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
read_key :: proc() -> Key {
|
Raw_State :: struct {
|
||||||
buf: [3]u8
|
original: posix.termios,
|
||||||
|
fd: posix.FD,
|
||||||
n := posix.read(posix.STDIN_FILENO, &buf[0], 1)
|
|
||||||
if n <= 0 {
|
|
||||||
return .Unknown
|
|
||||||
}
|
|
||||||
|
|
||||||
switch buf[0] {
|
|
||||||
case ' ':
|
|
||||||
return .Space
|
|
||||||
case '\n', '\r':
|
|
||||||
return .Enter
|
|
||||||
case 0x03:
|
|
||||||
return .Escape
|
|
||||||
case 0x1b:
|
|
||||||
tv: posix.timeval
|
|
||||||
tv.tv_sec = 0
|
|
||||||
tv.tv_usec = posix.suseconds_t(100000)
|
|
||||||
|
|
||||||
set: posix.fd_set
|
|
||||||
posix.FD_ZERO(&set)
|
|
||||||
posix.FD_SET(posix.STDIN_FILENO, &set)
|
|
||||||
|
|
||||||
ready := posix.select(1, &set, nil, nil, &tv)
|
|
||||||
if ready <= 0 {
|
|
||||||
return .Escape
|
|
||||||
}
|
|
||||||
|
|
||||||
n2 := posix.read(posix.STDIN_FILENO, &buf[1], 1)
|
|
||||||
if n2 <= 0 || buf[1] != '[' {
|
|
||||||
return .Escape
|
|
||||||
}
|
|
||||||
|
|
||||||
posix.FD_ZERO(&set)
|
|
||||||
posix.FD_SET(posix.STDIN_FILENO, &set)
|
|
||||||
tv.tv_sec = 0
|
|
||||||
tv.tv_usec = posix.suseconds_t(100000)
|
|
||||||
|
|
||||||
ready = posix.select(1, &set, nil, nil, &tv)
|
|
||||||
if ready <= 0 {
|
|
||||||
return .Escape
|
|
||||||
}
|
|
||||||
|
|
||||||
n3 := posix.read(posix.STDIN_FILENO, &buf[2], 1)
|
|
||||||
if n3 <= 0 {
|
|
||||||
return .Escape
|
|
||||||
}
|
|
||||||
|
|
||||||
switch buf[2] {
|
|
||||||
case 'A':
|
|
||||||
return .Up
|
|
||||||
case 'B':
|
|
||||||
return .Down
|
|
||||||
case:
|
|
||||||
return .Escape
|
|
||||||
}
|
|
||||||
case:
|
|
||||||
return .Unknown
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
MultiSelect_Result :: enum {
|
|
||||||
Confirm,
|
|
||||||
Cancel,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MAX_VISIBLE :: 7
|
MAX_VISIBLE :: 7
|
||||||
@@ -125,7 +36,7 @@ multi_select :: proc(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
selected = make([dynamic]bool, len(options))
|
selected = make([dynamic]bool, 0, len(options))
|
||||||
cursor: int = 0
|
cursor: int = 0
|
||||||
scroll_offset: int = 0
|
scroll_offset: int = 0
|
||||||
|
|
||||||
@@ -199,3 +110,92 @@ render_options :: proc(
|
|||||||
return end - scroll_offset
|
return end - scroll_offset
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enable_raw_mode :: proc(fd: posix.FD) -> (Raw_State, bool) {
|
||||||
|
state: Raw_State
|
||||||
|
state.fd = fd
|
||||||
|
|
||||||
|
if posix.tcgetattr(fd, &state.original) != .OK {
|
||||||
|
return state, false
|
||||||
|
}
|
||||||
|
|
||||||
|
attr: posix.termios = state.original
|
||||||
|
attr.c_lflag -= {.ICANON, .ECHO, .ISIG, .IEXTEN}
|
||||||
|
attr.c_iflag -= {.IXON, .ICRNL, .BRKINT, .INPCK, .ISTRIP}
|
||||||
|
attr.c_oflag -= {.OPOST}
|
||||||
|
attr.c_cflag += {.CS8}
|
||||||
|
attr.c_cc[.VMIN] = 1
|
||||||
|
attr.c_cc[.VTIME] = 0
|
||||||
|
|
||||||
|
if posix.tcsetattr(fd, .TCSAFLUSH, &attr) != .OK {
|
||||||
|
return state, false
|
||||||
|
}
|
||||||
|
|
||||||
|
return state, true
|
||||||
|
}
|
||||||
|
|
||||||
|
disable_raw_mode :: proc(state: ^Raw_State) {
|
||||||
|
posix.tcsetattr(state.fd, .TCSAFLUSH, &state.original)
|
||||||
|
}
|
||||||
|
|
||||||
|
read_key :: proc() -> Key {
|
||||||
|
buf: [3]u8
|
||||||
|
|
||||||
|
n := posix.read(posix.STDIN_FILENO, &buf[0], 1)
|
||||||
|
if n <= 0 {
|
||||||
|
return .Unknown
|
||||||
|
}
|
||||||
|
|
||||||
|
switch buf[0] {
|
||||||
|
case ' ':
|
||||||
|
return .Space
|
||||||
|
case '\n', '\r':
|
||||||
|
return .Enter
|
||||||
|
case 0x03:
|
||||||
|
return .Escape
|
||||||
|
case 0x1b:
|
||||||
|
tv: posix.timeval
|
||||||
|
tv.tv_sec = 0
|
||||||
|
tv.tv_usec = posix.suseconds_t(100000)
|
||||||
|
|
||||||
|
set: posix.fd_set
|
||||||
|
posix.FD_ZERO(&set)
|
||||||
|
posix.FD_SET(posix.STDIN_FILENO, &set)
|
||||||
|
|
||||||
|
ready := posix.select(1, &set, nil, nil, &tv)
|
||||||
|
if ready <= 0 {
|
||||||
|
return .Escape
|
||||||
|
}
|
||||||
|
|
||||||
|
n2 := posix.read(posix.STDIN_FILENO, &buf[1], 1)
|
||||||
|
if n2 <= 0 || buf[1] != '[' {
|
||||||
|
return .Escape
|
||||||
|
}
|
||||||
|
|
||||||
|
posix.FD_ZERO(&set)
|
||||||
|
posix.FD_SET(posix.STDIN_FILENO, &set)
|
||||||
|
tv.tv_sec = 0
|
||||||
|
tv.tv_usec = posix.suseconds_t(100000)
|
||||||
|
|
||||||
|
ready = posix.select(1, &set, nil, nil, &tv)
|
||||||
|
if ready <= 0 {
|
||||||
|
return .Escape
|
||||||
|
}
|
||||||
|
|
||||||
|
n3 := posix.read(posix.STDIN_FILENO, &buf[2], 1)
|
||||||
|
if n3 <= 0 {
|
||||||
|
return .Escape
|
||||||
|
}
|
||||||
|
|
||||||
|
switch buf[2] {
|
||||||
|
case 'A':
|
||||||
|
return .Up
|
||||||
|
case 'B':
|
||||||
|
return .Down
|
||||||
|
case:
|
||||||
|
return .Escape
|
||||||
|
}
|
||||||
|
case:
|
||||||
|
return .Unknown
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ OK :: 0
|
|||||||
ROW :: 100
|
ROW :: 100
|
||||||
DONE :: 101
|
DONE :: 101
|
||||||
|
|
||||||
|
DESERIALIZE_FREEONCLOSE :: 1
|
||||||
|
DESERIALIZE_RESIZEABLE :: 2
|
||||||
|
|
||||||
foreign lib {
|
foreign lib {
|
||||||
@(link_name="sqlite3_open")
|
@(link_name="sqlite3_open")
|
||||||
db_open :: proc(filename: cstring, ppDb: ^^rawptr) -> c.int ---
|
db_open :: proc(filename: cstring, ppDb: ^^rawptr) -> c.int ---
|
||||||
@@ -31,4 +34,12 @@ foreign lib {
|
|||||||
bind_text :: proc(stmt: ^rawptr, idx: c.int, val: cstring, n: c.int, destructor: rawptr) -> c.int ---
|
bind_text :: proc(stmt: ^rawptr, idx: c.int, val: cstring, n: c.int, destructor: rawptr) -> c.int ---
|
||||||
@(link_name="sqlite3_changes")
|
@(link_name="sqlite3_changes")
|
||||||
changes :: proc(db: ^rawptr) -> c.int ---
|
changes :: proc(db: ^rawptr) -> c.int ---
|
||||||
|
@(link_name="sqlite3_serialize")
|
||||||
|
serialize :: proc(db: ^rawptr, zSchema: cstring, piSize: ^i64, mFlags: u32) -> [^]u8 ---
|
||||||
|
@(link_name="sqlite3_deserialize")
|
||||||
|
deserialize :: proc(db: ^rawptr, zSchema: cstring, pData: [^]u8, szDb: i64, szBuf: i64, mFlags: u32) -> c.int ---
|
||||||
|
@(link_name="sqlite3_malloc64")
|
||||||
|
malloc64 :: proc(n: i64) -> [^]u8 ---
|
||||||
|
@(link_name="sqlite3_free")
|
||||||
|
free :: proc(p: rawptr) ---
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
0.2.0
|
0.3.0
|
||||||
|
|||||||
Reference in New Issue
Block a user