-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes: #167
- Loading branch information
Showing
6 changed files
with
120 additions
and
2 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
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,38 @@ | ||
import common | ||
|
||
const app_name = 'env' | ||
const app_description = 'run a program in a modified environment' | ||
|
||
struct EnvArg { | ||
// -0 | ||
nul_terminated bool | ||
|
||
// -u | ||
unsets []string | ||
|
||
// -i | ||
ignore bool | ||
|
||
// command and arguments | ||
cmd_args []string | ||
} | ||
|
||
pub fn new_args(args []string) !EnvArg { | ||
mut fp := common.flag_parser(args) | ||
fp.application(app_name) | ||
fp.description(app_description) | ||
fp.version(common.coreutils_version()) | ||
|
||
nul := fp.bool('null', `0`, false, 'end each output line with NUL, not newline') | ||
unsets := fp.string_multi('unset', `u`, 'remove variable from the environment') | ||
ignore := fp.bool('ignore-environment', `i`, false, 'start with an empty environment') | ||
|
||
cmd_args := fp.finalize() or { return err } | ||
|
||
return EnvArg{ | ||
nul_terminated: nul | ||
unsets: unsets | ||
ignore: ignore | ||
cmd_args: cmd_args | ||
} | ||
} |
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,15 @@ | ||
module main | ||
|
||
fn test_ignore() { | ||
args := new_args(['t', '-i'])! | ||
assert args.ignore == true | ||
} | ||
|
||
fn test_unset() { | ||
args := new_args(['t', '-i', '-u', 'ABC'])! | ||
assert 'ABC' in args.unsets | ||
|
||
multi_args := new_args(['t', '-i', '-u', 'ABC', '-u', 'EFG'])! | ||
assert 'ABC' in multi_args.unsets | ||
assert 'EFG' in multi_args.unsets | ||
} |
Empty file.
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,40 @@ | ||
// env - run a program in a modified environment | ||
import os | ||
import maps | ||
|
||
fn main() { | ||
args := new_args(os.args) or { | ||
eprintln(err) | ||
return | ||
} | ||
run(args) | ||
} | ||
|
||
pub fn run(args EnvArg) { | ||
mut envs := if args.ignore { | ||
map[string]string{} | ||
} else { | ||
os.environ() | ||
} | ||
|
||
for key in args.unsets { | ||
envs.delete(key) | ||
} | ||
|
||
if args.cmd_args.len == 0 { | ||
eol := if args.nul_terminated { '\0' } else { '\n' } | ||
for key, val in envs { | ||
print('${key}=${val}' + eol) | ||
} | ||
return | ||
} | ||
|
||
cmd_path := os.find_abs_path_of_executable(args.cmd_args[0]) or { | ||
eprintln(err) | ||
return | ||
} | ||
|
||
os.execve(cmd_path, args.cmd_args[1..], maps.to_array(envs, fn (k string, v string) string { | ||
return '${k}=${v}\0' | ||
})) or { eprintln(err) } | ||
} |
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,25 @@ | ||
import os | ||
|
||
fn test_help() { | ||
result := os.execute_or_panic('${os.quoted_path(@VEXE)} run . -h') | ||
assert result.exit_code == 0 | ||
assert result.output.contains('a modified environment') | ||
} | ||
|
||
fn test_same_env() { | ||
result := os.execute_or_panic('${os.quoted_path(@VEXE)} run . -0') | ||
assert result.exit_code == 0 | ||
|
||
env := os.execute_or_panic('env -0') | ||
assert env.exit_code == 0 | ||
assert env.output == result.output | ||
} | ||
|
||
fn test_same_unset() { | ||
result := os.execute_or_panic('${os.quoted_path(@VEXE)} run . -u PATH') | ||
assert result.exit_code == 0 | ||
|
||
env := os.execute_or_panic('env -u PATH') | ||
assert env.exit_code == 0 | ||
assert env.output == result.output | ||
} |