mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 10:38:33 -04:00
Compare commits
1 Commits
427a67dcb4
...
a28b8d10bc
| Author | SHA1 | Date | |
|---|---|---|---|
| a28b8d10bc |
127
cmd_check.odin
127
cmd_check.odin
@@ -6,74 +6,79 @@ import "core:path/filepath"
|
||||
import "core:strings"
|
||||
|
||||
cmd_check :: proc(cmd: ^Command) {
|
||||
check_path: string
|
||||
if len(cmd.args) > 0 {
|
||||
check_path = cmd.args[0]
|
||||
} else {
|
||||
cwd, cwd_err := os.get_working_directory(context.allocator)
|
||||
if cwd_err != nil {
|
||||
fmt.printf("Error getting current directory: %v\n", cwd_err)
|
||||
return
|
||||
}
|
||||
check_path = cwd
|
||||
}
|
||||
feats := check_features()
|
||||
|
||||
abs_path: string
|
||||
if filepath.is_abs(check_path) {
|
||||
abs_path = check_path
|
||||
} else {
|
||||
resolved, abs_err := filepath.abs(check_path)
|
||||
if abs_err != nil {
|
||||
fmt.printf("Error getting absolute path: %v\n", abs_err)
|
||||
return
|
||||
}
|
||||
abs_path = resolved
|
||||
}
|
||||
check_path: string
|
||||
if len(cmd.args) > 0 {
|
||||
check_path = cmd.args[0]
|
||||
} else {
|
||||
cwd, cwd_err := os.get_working_directory(context.allocator)
|
||||
if cwd_err != nil {
|
||||
fmt.printf("Error getting current directory: %v\n", cwd_err)
|
||||
return
|
||||
}
|
||||
check_path = cwd
|
||||
}
|
||||
|
||||
db, db_ok := db_open()
|
||||
if !db_ok {
|
||||
return
|
||||
}
|
||||
defer db_close(&db)
|
||||
abs_path: string
|
||||
if filepath.is_abs(check_path) {
|
||||
abs_path = check_path
|
||||
} else {
|
||||
resolved, abs_err := filepath.abs(check_path)
|
||||
if abs_err != nil {
|
||||
fmt.printf("Error getting absolute path: %v\n", abs_err)
|
||||
return
|
||||
}
|
||||
abs_path = resolved
|
||||
}
|
||||
|
||||
is_dir := os.is_directory(abs_path)
|
||||
db, db_ok := db_open()
|
||||
if !db_ok {
|
||||
return
|
||||
}
|
||||
defer db_close(&db)
|
||||
|
||||
files_in_path: [dynamic]string
|
||||
is_dir := os.is_directory(abs_path)
|
||||
|
||||
if is_dir {
|
||||
if !can_scan() {
|
||||
fmt.println("Error: please install fd to use the check command (https://github.com/sharkdp/fd)")
|
||||
return
|
||||
}
|
||||
files_in_path: [dynamic]string
|
||||
|
||||
scanned, scan_ok := scan_path(abs_path, db.cfg)
|
||||
if !scan_ok {
|
||||
fmt.println("Error scanning directory for .env files")
|
||||
return
|
||||
}
|
||||
files_in_path = scanned
|
||||
} else {
|
||||
append(&files_in_path, abs_path)
|
||||
}
|
||||
if is_dir {
|
||||
if cant_scan(feats) {
|
||||
fmt.println(
|
||||
"Error: please install fd to use the check command (https://github.com/sharkdp/fd)",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
db_files, list_ok := db_list(&db)
|
||||
if !list_ok {
|
||||
return
|
||||
}
|
||||
scanned, scan_ok := scan_path(abs_path, db.cfg)
|
||||
if !scan_ok {
|
||||
fmt.println("Error scanning directory for .env files")
|
||||
return
|
||||
}
|
||||
files_in_path = scanned
|
||||
} else {
|
||||
append(&files_in_path, abs_path)
|
||||
}
|
||||
|
||||
not_backed := find_unbacked(files_in_path[:], db_files[:])
|
||||
db_files, list_ok := db_list(&db)
|
||||
if !list_ok {
|
||||
return
|
||||
}
|
||||
|
||||
if len(not_backed) == 0 {
|
||||
if len(files_in_path) == 0 {
|
||||
fmt.println("No .env files found in the specified directory.")
|
||||
} else {
|
||||
fmt.println("✓ All .env files in the directory are backed up.")
|
||||
}
|
||||
} else {
|
||||
fmt.printf("Found %d .env file(s) that are not backed up:\n", len(not_backed))
|
||||
for file in not_backed {
|
||||
fmt.printf(" %s\n", file)
|
||||
}
|
||||
fmt.println("\nRun 'envr sync' to back up these files.")
|
||||
}
|
||||
not_backed := find_unbacked(files_in_path[:], db_files[:])
|
||||
|
||||
if len(not_backed) == 0 {
|
||||
if len(files_in_path) == 0 {
|
||||
fmt.println("No .env files found in the specified directory.")
|
||||
} else {
|
||||
fmt.println("✓ All .env files in the directory are backed up.")
|
||||
}
|
||||
} else {
|
||||
fmt.printf("Found %d .env file(s) that are not backed up:\n", len(not_backed))
|
||||
for file in not_backed {
|
||||
fmt.printf(" %s\n", file)
|
||||
}
|
||||
fmt.println("\nRun 'envr sync' to back up these files.")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,39 +5,44 @@ import "core:testing"
|
||||
|
||||
@(test)
|
||||
test_find_unbacked_finds_missing :: proc(t: ^testing.T) {
|
||||
local := []string{"/a/.env", "/b/.env", "/c/.env"}
|
||||
db := []EnvFile{{Path = "/a/.env"}, {Path = "/b/.env"}}
|
||||
local := []string{"/a/.env", "/b/.env", "/c/.env"}
|
||||
db := []EnvFile{{Path = "/a/.env"}, {Path = "/b/.env"}}
|
||||
|
||||
result := find_unbacked(local, db[:])
|
||||
testing.expect(t, len(result) == 1, fmt.aprintf("expected 1 unbacked, got %d", len(result)))
|
||||
if len(result) > 0 {
|
||||
testing.expect(t, result[0] == "/c/.env", fmt.aprintf("expected /c/.env, got %s", result[0]))
|
||||
}
|
||||
result := find_unbacked(local, db[:])
|
||||
testing.expect(t, len(result) == 1, fmt.aprintf("expected 1 unbacked, got %d", len(result)))
|
||||
if len(result) > 0 {
|
||||
testing.expect(
|
||||
t,
|
||||
result[0] == "/c/.env",
|
||||
fmt.aprintf("expected /c/.env, got %s", result[0]),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_find_unbacked_all_backed :: proc(t: ^testing.T) {
|
||||
local := []string{"/a/.env", "/b/.env"}
|
||||
db := []EnvFile{{Path = "/a/.env"}, {Path = "/b/.env"}}
|
||||
local := []string{"/a/.env", "/b/.env"}
|
||||
db := []EnvFile{{Path = "/a/.env"}, {Path = "/b/.env"}}
|
||||
|
||||
result := find_unbacked(local, db[:])
|
||||
testing.expect(t, len(result) == 0, fmt.aprintf("expected 0 unbacked, got %d", len(result)))
|
||||
result := find_unbacked(local, db[:])
|
||||
testing.expect(t, len(result) == 0, fmt.aprintf("expected 0 unbacked, got %d", len(result)))
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_find_unbacked_no_local :: proc(t: ^testing.T) {
|
||||
local: []string
|
||||
db := []EnvFile{{Path = "/a/.env"}}
|
||||
local: []string
|
||||
db := []EnvFile{{Path = "/a/.env"}}
|
||||
|
||||
result := find_unbacked(local, db[:])
|
||||
testing.expect(t, len(result) == 0, fmt.aprintf("expected 0 unbacked, got %d", len(result)))
|
||||
result := find_unbacked(local, db[:])
|
||||
testing.expect(t, len(result) == 0, fmt.aprintf("expected 0 unbacked, got %d", len(result)))
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_find_unbacked_none_backed :: proc(t: ^testing.T) {
|
||||
local := []string{"/a/.env", "/b/.env"}
|
||||
db: []EnvFile
|
||||
local := []string{"/a/.env", "/b/.env"}
|
||||
db: []EnvFile
|
||||
|
||||
result := find_unbacked(local, db[:])
|
||||
testing.expect(t, len(result) == 2, fmt.aprintf("expected 2 unbacked, got %d", len(result)))
|
||||
result := find_unbacked(local, db[:])
|
||||
testing.expect(t, len(result) == 2, fmt.aprintf("expected 2 unbacked, got %d", len(result)))
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@ import "core:encoding/json"
|
||||
import "core:fmt"
|
||||
|
||||
cmd_scan :: proc(cmd: ^Command) {
|
||||
if !can_scan() {
|
||||
feats := check_features()
|
||||
if cant_scan(feats) {
|
||||
fmt.println(
|
||||
"Error: please install fd to use the scan command (https://github.com/sharkdp/fd)",
|
||||
)
|
||||
|
||||
108
scan.odin
108
scan.odin
@@ -2,23 +2,49 @@ package main
|
||||
|
||||
import "core:fmt"
|
||||
import "core:os"
|
||||
import "core:path/filepath"
|
||||
import "core:strings"
|
||||
import "core:sync"
|
||||
|
||||
fd_counter: sync.Atomic_Mutex
|
||||
fd_seq: int
|
||||
|
||||
next_fd_tmp_path :: proc() -> string {
|
||||
sync.atomic_mutex_lock(&fd_counter)
|
||||
n := fd_seq
|
||||
fd_seq += 1
|
||||
sync.atomic_mutex_unlock(&fd_counter)
|
||||
return fmt.aprintf("/tmp/envr-fd-%d-%d", os.get_pid(), n)
|
||||
// Caller is responsible for freeing paths
|
||||
scan_path :: proc(search_path: string, cfg: Config) -> (paths: [dynamic]string, ok: bool) {
|
||||
if is_tty() {
|
||||
fmt.printf("Searching for all files in \"%s\"...\n", search_path)
|
||||
}
|
||||
all_files, all_ok := run_fd(build_fd_args(search_path, cfg, true))
|
||||
if !all_ok {
|
||||
return
|
||||
}
|
||||
|
||||
if is_tty() {
|
||||
fmt.printf("Search for unignored fies in \"%s\"...\n", search_path)
|
||||
}
|
||||
unignored_files, unignored_ok := run_fd(build_fd_args(search_path, cfg, false))
|
||||
if !unignored_ok {
|
||||
return
|
||||
}
|
||||
|
||||
unignored_set := make(map[string]bool, len(unignored_files), context.temp_allocator)
|
||||
for file in unignored_files {
|
||||
unignored_set[file] = true
|
||||
}
|
||||
|
||||
for file in all_files {
|
||||
if !(file in unignored_set) {
|
||||
append(&paths, file)
|
||||
}
|
||||
}
|
||||
|
||||
ok = true
|
||||
return
|
||||
}
|
||||
|
||||
@(private = "file")
|
||||
build_fd_args :: proc(search_path: string, cfg: Config, include_ignored: bool) -> []string {
|
||||
args := make([dynamic]string, 0, 3 + 2 * len(cfg.ScanConfig.Exclude) + 2)
|
||||
args_len := 3 + 2 * len(cfg.ScanConfig.Exclude) + 2
|
||||
args := make([dynamic]string, 0, args_len, context.temp_allocator)
|
||||
append(&args, "fd")
|
||||
append(&args, "-a")
|
||||
append(&args, cfg.ScanConfig.Matcher)
|
||||
@@ -38,7 +64,7 @@ build_fd_args :: proc(search_path: string, cfg: Config, include_ignored: bool) -
|
||||
return args[:]
|
||||
}
|
||||
|
||||
run_fd :: proc(args: []string) -> (lines: [dynamic]string, ok: bool) {
|
||||
run_fd :: proc(args: []string) -> (lines: []string, ok: bool) {
|
||||
tmp_path := next_fd_tmp_path()
|
||||
tmp_file, tmp_err := os.open(tmp_path, os.O_CREATE | os.O_WRONLY | os.O_TRUNC)
|
||||
if tmp_err != nil {
|
||||
@@ -64,7 +90,7 @@ run_fd :: proc(args: []string) -> (lines: [dynamic]string, ok: bool) {
|
||||
return
|
||||
}
|
||||
|
||||
data, read_err := os.read_entire_file_from_path(tmp_path, context.allocator)
|
||||
data, read_err := os.read_entire_file_from_path(tmp_path, context.temp_allocator)
|
||||
os.remove(tmp_path)
|
||||
if read_err != nil {
|
||||
return
|
||||
@@ -77,69 +103,43 @@ run_fd :: proc(args: []string) -> (lines: [dynamic]string, ok: bool) {
|
||||
return
|
||||
}
|
||||
|
||||
raw_lines := strings.split(output, "\n")
|
||||
raw_lines := strings.split(output, "\n", context.temp_allocator)
|
||||
result := make([dynamic]string, 0, len(raw_lines), context.temp_allocator)
|
||||
for line in raw_lines {
|
||||
trimmed, _ := strings.clone(strings.trim_space(line))
|
||||
trimmed := strings.trim_space(line)
|
||||
if len(trimmed) > 0 {
|
||||
append(&lines, trimmed)
|
||||
append(&result, trimmed)
|
||||
}
|
||||
}
|
||||
|
||||
ok = true
|
||||
return
|
||||
return result[:], true
|
||||
}
|
||||
|
||||
scan_path :: proc(search_path: string, cfg: Config) -> (paths: [dynamic]string, ok: bool) {
|
||||
if is_tty() {
|
||||
fmt.printf("Searching for all files in \"%s\"...\n", search_path)
|
||||
}
|
||||
all_args := build_fd_args(search_path, cfg, true)
|
||||
all_files, all_ok := run_fd(all_args)
|
||||
if !all_ok {
|
||||
return
|
||||
}
|
||||
|
||||
if is_tty() {
|
||||
fmt.printf("Search for unignored fies in \"%s\"...\n", search_path)
|
||||
}
|
||||
unignored_args := build_fd_args(search_path, cfg, false)
|
||||
unignored_files, unignored_ok := run_fd(unignored_args)
|
||||
if !unignored_ok {
|
||||
return
|
||||
}
|
||||
|
||||
unignored_set: map[string]bool
|
||||
for file in unignored_files {
|
||||
unignored_set[file] = true
|
||||
}
|
||||
|
||||
for file in all_files {
|
||||
if !(file in unignored_set) {
|
||||
append(&paths, file)
|
||||
}
|
||||
}
|
||||
|
||||
ok = true
|
||||
return
|
||||
@(private = "file")
|
||||
next_fd_tmp_path :: proc() -> string {
|
||||
sync.atomic_mutex_lock(&fd_counter)
|
||||
n := fd_seq
|
||||
fd_seq += 1
|
||||
sync.atomic_mutex_unlock(&fd_counter)
|
||||
return fmt.aprintf("/tmp/envr-fd-%d-%d", os.get_pid(), n, allocator = context.temp_allocator)
|
||||
}
|
||||
|
||||
can_scan :: proc() -> bool {
|
||||
feats := check_features()
|
||||
return Feature.Fd in feats
|
||||
cant_scan :: proc(feats: AvailableFeatures) -> bool {
|
||||
return Feature.Fd not_in feats
|
||||
}
|
||||
|
||||
find_unbacked :: proc(local_files: []string, db_files: []EnvFile) -> [dynamic]string {
|
||||
backed_set: map[string]bool
|
||||
find_unbacked :: proc(local_files: []string, db_files: []EnvFile) -> []string {
|
||||
backed_set := make(map[string]bool, len(db_files), context.temp_allocator)
|
||||
for file in db_files {
|
||||
backed_set[file.Path] = true
|
||||
}
|
||||
|
||||
unbacked: [dynamic]string
|
||||
unbacked := make([dynamic]string, 0, len(db_files) / 2, context.temp_allocator)
|
||||
for file in local_files {
|
||||
if !(file in backed_set) {
|
||||
append(&unbacked, file)
|
||||
}
|
||||
}
|
||||
return unbacked
|
||||
return unbacked[:]
|
||||
}
|
||||
|
||||
|
||||
129
scan_test.odin
129
scan_test.odin
@@ -7,88 +7,81 @@ import "core:testing"
|
||||
|
||||
@(test)
|
||||
test_scan_path_finds_gitignored_env_files :: proc(t: ^testing.T) {
|
||||
if !can_scan() {
|
||||
return
|
||||
}
|
||||
feats := check_features()
|
||||
testing.expect(t, cant_scan(feats) == false)
|
||||
|
||||
base := fmt.aprintf("/tmp/envr-scan-test-%d", os.get_pid())
|
||||
os.mkdir_all(base)
|
||||
defer os.remove_all(base)
|
||||
base := fmt.aprintf("/tmp/envr-scan-test-%d", os.get_pid())
|
||||
os.mkdir_all(base)
|
||||
defer os.remove_all(base)
|
||||
|
||||
git_init := os.Process_Desc{
|
||||
command = []string{"git", "-c", "advice.defaultBranchName=false", "init"},
|
||||
working_dir = base,
|
||||
stdout = os.stderr,
|
||||
stderr = os.stderr,
|
||||
}
|
||||
p, err := os.process_start(git_init)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, wait_err := os.process_wait(p)
|
||||
if wait_err != nil {
|
||||
return
|
||||
}
|
||||
git_init := os.Process_Desc {
|
||||
command = []string{"git", "-c", "advice.defaultBranchName=false", "init"},
|
||||
working_dir = base,
|
||||
stdout = os.stderr,
|
||||
stderr = os.stderr,
|
||||
}
|
||||
p, err := os.process_start(git_init)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, wait_err := os.process_wait(p)
|
||||
if wait_err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
gitignore_path := fmt.aprintf("%s/.gitignore", base)
|
||||
_ = os.write_entire_file(gitignore_path, ".env*\n")
|
||||
gitignore_path := fmt.aprintf("%s/.gitignore", base)
|
||||
_ = os.write_entire_file(gitignore_path, ".env*\n")
|
||||
|
||||
_ = os.write_entire_file(fmt.aprintf("%s/.env", base), "SECRET=1")
|
||||
_ = os.write_entire_file(fmt.aprintf("%s/.env.testing", base), "TEST=1")
|
||||
_ = os.write_entire_file(fmt.aprintf("%s/config.yaml", base), "key: value")
|
||||
_ = os.write_entire_file(fmt.aprintf("%s/.env", base), "SECRET=1")
|
||||
_ = os.write_entire_file(fmt.aprintf("%s/.env.testing", base), "TEST=1")
|
||||
_ = os.write_entire_file(fmt.aprintf("%s/config.yaml", base), "key: value")
|
||||
|
||||
cfg := Config{
|
||||
ScanConfig = ScanConfig{
|
||||
Matcher = "\\.env",
|
||||
Exclude = []string{},
|
||||
Include = []string{},
|
||||
},
|
||||
}
|
||||
cfg := Config {
|
||||
ScanConfig = ScanConfig{Matcher = "\\.env", Exclude = []string{}, Include = []string{}},
|
||||
}
|
||||
|
||||
results, ok := scan_path(base, cfg)
|
||||
testing.expect(t, ok, "scan_path should succeed")
|
||||
results, ok := scan_path(base, cfg)
|
||||
defer delete(results)
|
||||
testing.expect(t, ok, "scan_path should succeed")
|
||||
|
||||
found_env := false
|
||||
found_testing := false
|
||||
found_config := false
|
||||
found_env := false
|
||||
found_testing := false
|
||||
found_config := false
|
||||
|
||||
for path in results {
|
||||
_, filename := filepath.split(path)
|
||||
if filename == ".env" {
|
||||
found_env = true
|
||||
}
|
||||
if filename == ".env.testing" {
|
||||
found_testing = true
|
||||
}
|
||||
if filename == "config.yaml" {
|
||||
found_config = true
|
||||
}
|
||||
}
|
||||
for path in results {
|
||||
_, filename := filepath.split(path)
|
||||
if filename == ".env" {
|
||||
found_env = true
|
||||
}
|
||||
if filename == ".env.testing" {
|
||||
found_testing = true
|
||||
}
|
||||
if filename == "config.yaml" {
|
||||
found_config = true
|
||||
}
|
||||
}
|
||||
|
||||
testing.expect(t, found_env, "should find .env (gitignored)")
|
||||
testing.expect(t, found_testing, "should find .env.testing (gitignored)")
|
||||
testing.expect(t, !found_config, "should NOT find config.yaml (not gitignored)")
|
||||
testing.expect(t, found_env, "should find .env (gitignored)")
|
||||
testing.expect(t, found_testing, "should find .env.testing (gitignored)")
|
||||
testing.expect(t, !found_config, "should NOT find config.yaml (not gitignored)")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_scan_path_empty_dir :: proc(t: ^testing.T) {
|
||||
if !can_scan() {
|
||||
return
|
||||
}
|
||||
feats := check_features()
|
||||
testing.expect(t, cant_scan(feats) == false)
|
||||
|
||||
base := fmt.aprintf("/tmp/envr-scan-empty-%d", os.get_pid())
|
||||
os.mkdir_all(base)
|
||||
defer os.remove_all(base)
|
||||
base := fmt.aprintf("/tmp/envr-scan-empty-%d", os.get_pid())
|
||||
os.mkdir_all(base)
|
||||
defer os.remove_all(base)
|
||||
|
||||
cfg := Config{
|
||||
ScanConfig = ScanConfig{
|
||||
Matcher = "\\.env",
|
||||
Exclude = []string{},
|
||||
Include = []string{},
|
||||
},
|
||||
}
|
||||
cfg := Config {
|
||||
ScanConfig = ScanConfig{Matcher = "\\.env", Exclude = []string{}, Include = []string{}},
|
||||
}
|
||||
|
||||
results, ok := scan_path(base, cfg)
|
||||
testing.expect(t, ok, "scan_path should succeed")
|
||||
testing.expect(t, len(results) == 0, fmt.aprintf("expected 0 results, got %d", len(results)))
|
||||
results, ok := scan_path(base, cfg)
|
||||
defer delete(results)
|
||||
testing.expect(t, ok, "scan_path should succeed")
|
||||
testing.expect(t, len(results) == 0, fmt.aprintf("expected 0 results, got %d", len(results)))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user