diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-09-01 17:37:24 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-09-01 17:37:24 -0400 |
| commit | 12345a85d9c7d7a56ddf323247a4bdf347021b73 (patch) | |
| tree | 93bafb42c1ec2c22cc2858936034901c39cbca82 /lib/commands/commands.tm | |
| parent | c778c8822f1c8acf981e26f7b860a384c94cff6f (diff) | |
| parent | adc2d81b5683e611c5f3289be6157d4519a60632 (diff) | |
Merge branch 'main' into optional-list-indexing
Diffstat (limited to 'lib/commands/commands.tm')
| -rw-r--r-- | lib/commands/commands.tm | 90 |
1 files changed, 0 insertions, 90 deletions
diff --git a/lib/commands/commands.tm b/lib/commands/commands.tm deleted file mode 100644 index 8a131042..00000000 --- a/lib/commands/commands.tm +++ /dev/null @@ -1,90 +0,0 @@ -# Functions for running system commands - -use ./commands.c - -extern run_command : func(exe:Text, args:[Text], env:{Text=Text}, input:[Byte]?, output:&[Byte]?, error:&[Byte]? -> Int32) -extern command_by_line : func(exe:Text, args:[Text], env:{Text=Text} -> func(->Text?)?) - -enum ExitType(Exited(status:Int32), Signaled(signal:Int32), Failed) - func succeeded(e:ExitType -> Bool) - when e is Exited(status) return (status == 0) - else return no - - func or_fail(e:ExitType, message:Text?=none) - if not e.succeeded() - fail(message or "Program failed: $e") - -struct ProgramResult(output:[Byte], errors:[Byte], exit_type:ExitType) - func or_fail(r:ProgramResult, message:Text?=none -> ProgramResult) - when r.exit_type is Exited(status) - if status != 0 - fail(message or "Program failed: $r") - else fail(message or "Program failed: $r") - return r - - func output_text(r:ProgramResult, trim_newline=yes -> Text?) - when r.exit_type is Exited(status) - if status == 0 - if text := Text.from_bytes(r.output) - if trim_newline - text = text.without_suffix("\n") - return text - else return none - return none - - func error_text(r:ProgramResult -> Text?) - when r.exit_type is Exited(status) - if status == 0 - return Text.from_bytes(r.errors) - else return none - return none - - func succeeded(r:ProgramResult -> Bool) - when r.exit_type is Exited(status) - return (status == 0) - else - return no - -struct Command(command:Text, args:[Text]=[], env:{Text=Text}={}) - func from_path(path:Path, args:[Text]=[], env:{Text=Text}={} -> Command) - return Command(Text(path), args, env) - - func result(command:Command, input="", input_bytes:[Byte]=[] -> ProgramResult) - if input.length > 0 - (&input_bytes).insert_all(input.bytes()) - - output : [Byte] - errors : [Byte] - status := run_command(command.command, command.args, command.env, input_bytes, &output, &errors) - - if C_code:Bool(WIFEXITED(@status)) - return ProgramResult(output, errors, ExitType.Exited(C_code:Int32(WEXITSTATUS(@status)))) - - if C_code:Bool(WIFSIGNALED(@status)) - return ProgramResult(output, errors, ExitType.Signaled(C_code:Int32(WTERMSIG(@status)))) - - return ProgramResult(output, errors, ExitType.Failed) - - func run(command:Command, -> ExitType) - status := run_command(command.command, command.args, command.env, none, none, none) - - if C_code:Bool(WIFEXITED(@status)) - return ExitType.Exited(C_code:Int32(WEXITSTATUS(@status))) - - if C_code:Bool(WIFSIGNALED(@status)) - return ExitType.Signaled(C_code:Int32(WTERMSIG(@status))) - - return ExitType.Failed - - func get_output(command:Command, input="", trim_newline=yes -> Text?) - return command.result(input=input).output_text(trim_newline=trim_newline) - - func get_output_bytes(command:Command, input="", input_bytes:[Byte]=[] -> [Byte]?) - result := command.result(input=input, input_bytes=input_bytes) - when result.exit_type is Exited(status) - if status == 0 return result.output - return none - else return none - - func by_line(command:Command -> func(->Text?)?) - return command_by_line(command.command, command.args, command.env) |
