Compare commits

..

8 Commits

2 changed files with 74 additions and 15 deletions

4
README.md Normal file
View File

@@ -0,0 +1,4 @@
## TODOS
- [ ] `envr sync` - Restore missing .env files, and update backed up ones.
- [ ] `envr scan` - Search for missing / moved .env files.

View File

@@ -1,12 +1,14 @@
#!/usr/bin/env nu #!/usr/bin/env nu
use std assert;
# Manage your .env files with ease # Manage your .env files with ease
def main [] { export def envr [] {
help main help envr
} }
# Import a .env file into envr # Import a .env file into envr
def "main import" [ export def "envr backup" [
file: path file: path
] { ] {
cd (dirname $file); cd (dirname $file);
@@ -31,10 +33,10 @@ def "main import" [
close db close db
$"file '($file)' imported successfull!" $"file '($file)' backed up!"
} }
const db_path = '/home/spencer/.envr/data.age' const db_path = '~/.envr/data.age'
# Create or load the database # Create or load the database
def "open db" [] { def "open db" [] {
@@ -43,7 +45,8 @@ def "open db" [] {
} else { } else {
# Open the db # Open the db
let dec = mktemp -p ~/.envr; let dec = mktemp -p ~/.envr;
age -d -i ((main config show).priv_key | path expand) $db_path | save -f $dec let priv_key = ((envr config show).priv_key | path expand);
age -d -i $priv_key ($db_path | path expand) | save -f $dec
stor import -f $dec stor import -f $dec
rm $dec rm $dec
} }
@@ -62,7 +65,7 @@ def "create-db" []: nothing -> any {
, contents text not null , contents text not null
);' );'
let pub_key = ((main config show).pub_key | path expand); let pub_key = ((envr config show).pub_key | path expand);
age -R $pub_key $dec | save -f $db_path age -R $pub_key $dec | save -f $db_path
stor import -f $dec stor import -f $dec
@@ -75,12 +78,60 @@ def "close db" [] {
stor export --file-name $dec; stor export --file-name $dec;
# Encrypt the file # Encrypt the file
let pub_key = ((main config show).pub_key | path expand); let pub_key = ((envr config show).pub_key | path expand);
age -R $pub_key $dec | save -f $db_path age -R $pub_key $dec | save -f $db_path
rm $dec rm $dec
} }
# Restore a .env file from backup.
export def "envr restore" [
path?: path # The path of the file to restore. Will be prompted if left blank.
]: nothing -> string {
let files = (files)
let $path = if ($path | is-empty) {
(
$files
| select path dir remotes
| input list -f "Please select a file to restore"
| get path
)
} else {
$path
}
let file = ($files | where path == $path | first);
assert ($file | is-not-empty) "File must be found"
let response = if (($path | path type) == 'file') {
if (open --raw $file.path | hash sha256 | $in == $file.sha256) {
# File matches
$'(ansi yellow)file is already up to date.(ansi reset)';
} else {
# File exists, but doesn't match
let continue = (
[No Yes]
| input list $"File '($path)' already exists, are you sure you want to overwrite it?"
| $in == 'Yes'
);
if ($continue) {
null
} else {
$'(ansi yellow)No action was taken(ansi reset)'
}
}
};
if ($response | is-empty) {
# File should be restored
$file.contents | save -f $path
return $'(ansi green)($path) restored!(ansi reset)'
} else {
return $response
}
}
# Supported config formats # Supported config formats
const available_formats = [ const available_formats = [
json json
@@ -92,7 +143,7 @@ const available_formats = [
] ]
# Create your initial config # Create your initial config
def "main config init" [ export def "envr config init" [
format?: string format?: string
#identity?: path #identity?: path
] { ] {
@@ -117,23 +168,27 @@ def "main config init" [
} }
# View your tracked files # View your tracked files
def "main list" [] { export def "envr list" [] {
(files | reject contents)
}
# List all the files in the database
def files [] {
( (
open db open db
| query db 'select * from envr_env_files' | query db 'select * from envr_env_files'
| update remotes { from json } | update remotes { from json }
| reject contents
) )
} }
# Update your env backups # Update your env backups
def "main sync" [] { export def "envr sync" [] {
'TODO:' 'TODO:'
} }
# Edit your config # Edit your config
def "main config edit" [] { export def "envr config edit" [] {
'TODO:' ^$env.EDITOR (config-file)
} }
def "config-file" []: [nothing -> path nothing -> nothing] { def "config-file" []: [nothing -> path nothing -> nothing] {
@@ -141,6 +196,6 @@ def "config-file" []: [nothing -> path nothing -> nothing] {
} }
# show your current config # show your current config
def "main config show" []: nothing -> record<source: path, priv_key: path, pub_key: path> { export def "envr config show" []: nothing -> record<source: path, priv_key: path, pub_key: path> {
open (config-file) open (config-file)
} }