|
| 1 | +// Copyright 2020-2024 The Jujutsu Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use crate::cli_util::CommandHelper; |
| 16 | +use crate::command_error::user_error; |
| 17 | +use crate::command_error::CommandError; |
| 18 | +use crate::ui::Ui; |
| 19 | + |
| 20 | +/// Execute an external command via jj |
| 21 | +/// |
| 22 | +/// This is useful for arbitrary aliases. |
| 23 | +/// |
| 24 | +/// !! WARNING !! |
| 25 | +/// |
| 26 | +/// The following technique just provides a convenient syntax for running |
| 27 | +/// arbitrary code on your system. Using it irresponsibly may cause damage |
| 28 | +/// ranging from breaking the behavior of `jj op undo` to wiping your file |
| 29 | +/// system. Exercise the same amount of caution while writing these aliases as |
| 30 | +/// you would when typing commands into the terminal! |
| 31 | +/// |
| 32 | +/// Let's assume you have a script called "my-jj-script" in you $PATH and you |
| 33 | +/// would like to execute it as "jj my-script". You would add the following line |
| 34 | +/// to your configuration file to achieve that: |
| 35 | +/// |
| 36 | +/// ```toml |
| 37 | +/// [aliases] |
| 38 | +/// my-script = ["util", "exec", "--", "my-jj-script"] |
| 39 | +/// # ^^^^ |
| 40 | +/// # This makes sure that flags are passed to your script instead of parsed by jj. |
| 41 | +/// ``` |
| 42 | +/// |
| 43 | +/// If you don't want to manage your script as a separate file, you can even |
| 44 | +/// inline it into your config file: |
| 45 | +/// |
| 46 | +/// ```toml |
| 47 | +/// [aliases] |
| 48 | +/// my-inline-script = ["util", "exec", "--", "bash", "-c", """ |
| 49 | +/// #!/usr/bin/env bash |
| 50 | +/// set -euo pipefail |
| 51 | +/// echo "Look Ma, everything in one file!" |
| 52 | +/// echo "args: $@" |
| 53 | +/// """, ""] |
| 54 | +/// # ^^ |
| 55 | +/// # This last empty string will become "$0" in bash, so your actual arguments |
| 56 | +/// # are all included in "$@" and start at "$1" as expected. |
| 57 | +/// ``` |
| 58 | +#[derive(clap::Args, Clone, Debug)] |
| 59 | +#[command(verbatim_doc_comment)] |
| 60 | +pub(crate) struct UtilExecArgs { |
| 61 | + /// External command to execute |
| 62 | + command: String, |
| 63 | + /// Arguments to pass to the external command |
| 64 | + args: Vec<String>, |
| 65 | +} |
| 66 | + |
| 67 | +pub fn cmd_util_exec( |
| 68 | + _ui: &mut Ui, |
| 69 | + _command: &CommandHelper, |
| 70 | + args: &UtilExecArgs, |
| 71 | +) -> Result<(), CommandError> { |
| 72 | + let status = std::process::Command::new(&args.command) |
| 73 | + .args(&args.args) |
| 74 | + .status() |
| 75 | + .map_err(user_error)?; |
| 76 | + if !status.success() { |
| 77 | + let error_msg = if let Some(exit_code) = status.code() { |
| 78 | + format!("External command exited with {exit_code}") |
| 79 | + } else { |
| 80 | + "External command was terminated by signal".into() |
| 81 | + }; |
| 82 | + return Err(user_error(error_msg)); |
| 83 | + } |
| 84 | + Ok(()) |
| 85 | +} |
0 commit comments