1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use anyhow::Result;

use super::super::cmd;
use super::super::errors;
use super::super::eval;
use super::super::model;
use super::super::query;

/// garden cmd <query> <command>...
pub fn main(app: &mut model::ApplicationContext) -> Result<()> {
    let mut query = String::new();
    let mut commands = Vec::new();
    let mut arguments = Vec::new();
    parse_args(&mut app.options, &mut query, &mut commands, &mut arguments);

    let quiet = app.options.quiet;
    let verbose = app.options.verbose;
    let keep_going = app.options.keep_going;
    let exit_status = cmd(
        app, quiet, verbose, keep_going, &query, &commands, &arguments,
    )?;
    cmd::result_from_exit_status(exit_status).map_err(|err| err.into())
}

/// Parse "cmd" arguments.
fn parse_args(
    options: &mut model::CommandOptions,
    query: &mut String,
    commands: &mut Vec<String>,
    arguments: &mut Vec<String>,
) {
    let mut commands_and_args: Vec<String> = Vec::new();
    {
        let mut ap = argparse::ArgumentParser::new();
        ap.silence_double_dash(false);
        ap.set_description("garden cmd - run custom commands over gardens");

        ap.refer(&mut options.keep_going).add_option(
            &["-k", "--keep-going"],
            argparse::StoreTrue,
            "continue to the next tree when errors occur",
        );

        ap.refer(query).required().add_argument(
            "query",
            argparse::Store,
            "gardens/groups/trees to exec (tree query)",
        );

        ap.refer(&mut commands_and_args).required().add_argument(
            "commands",
            argparse::List,
            "commands to run over resolved trees",
        );

        options.args.insert(0, "garden cmd".into());
        cmd::parse_args(ap, options.args.to_vec());
    }

    if options.is_debug("cmd") {
        debug!("subcommand: cmd");
        debug!("query: {}", query);
        debug!("commands_and_args: {:?}", commands_and_args);
    }

    // Queries and arguments are separated by a double-dash "--" marker.
    cmd::split_on_dash(&commands_and_args, commands, arguments);

    if options.is_debug("cmd") {
        debug!("commands: {:?}", commands);
        debug!("arguments: {:?}", arguments);
    }
}

/// garden <command> <query>...
pub fn custom(app: &mut model::ApplicationContext, command: &str) -> Result<()> {
    let mut queries = Vec::new();
    let mut arguments = Vec::new();
    parse_args_custom(command, &mut app.options, &mut queries, &mut arguments);

    let quiet = app.options.quiet;
    let verbose = app.options.verbose;
    let keep_going = app.options.keep_going;
    cmds(
        app, quiet, verbose, keep_going, command, &queries, &arguments,
    )
    .map_err(|err| err)
}

/// Parse custom command arguments.
fn parse_args_custom(
    command: &str,
    options: &mut model::CommandOptions,
    queries: &mut Vec<String>,
    arguments: &mut Vec<String>,
) {
    let mut queries_and_arguments: Vec<String> = Vec::new();
    let mut ap = argparse::ArgumentParser::new();
    ap.silence_double_dash(false);
    ap.set_description("garden cmd - run custom commands over gardens");

    ap.refer(&mut options.keep_going).add_option(
        &["-k", "--keep-going"],
        argparse::StoreTrue,
        "continue to the next tree when errors occur",
    );

    ap.refer(&mut queries_and_arguments).add_argument(
        "queries",
        argparse::List,
        "gardens/groups/trees to exec (tree queries)",
    );

    options.args.insert(0, format!("garden {}", command));
    cmd::parse_args(ap, options.args.to_vec());

    if options.is_debug("cmd") {
        debug!("command: {}", command);
        debug!("queries_and_arguments: {:?}", queries_and_arguments);
    }

    // Queries and arguments are separated by a double-dash "--" marker.
    cmd::split_on_dash(&queries_and_arguments, queries, arguments);

    // Default to "." when no queries have been specified.
    if queries.is_empty() {
        queries.push(".".into());
    }

    if options.is_debug("cmd") {
        debug!("queries {:?}", queries);
        debug!("arguments: {:?}", arguments);
    }
}

/// Strategy: resolve the trees down to a set of tree indexes paired with an
/// an optional garden context.
///
/// If the names resolve to gardens, each garden is processed independently.
/// Trees that exist in multiple matching gardens will be processed multiple
/// times.
///
/// If the names resolve to trees, each tree is processed independently
/// with no garden context.

pub fn cmd(
    app: &mut model::ApplicationContext,
    quiet: bool,
    verbose: bool,
    keep_going: bool,
    query: &str,
    commands: &[String],
    arguments: &[String],
) -> Result<i32> {
    // Resolve the tree query into a vector of tree contexts.
    let contexts;
    let mut exit_status: i32 = errors::EX_OK;
    // Mutable scope for app.get_root_config_mut()
    {
        let config = app.get_root_config_mut();
        contexts = query::resolve_trees(config, query);
    }

    // Loop over each context, evaluate the tree environment,
    // and run the command.
    for context in &contexts {
        // Skip symlink trees.
        {
            let config = app.get_root_config();
            if config.trees[context.tree].is_symlink {
                continue;
            }
        }
        // Evaluate the tree environment
        let env;
        {
            env = eval::environment(app.get_root_config(), context);
        }

        // Run each command in the tree's context
        let path: String;
        {
            let config = app.get_root_config();
            let tree = &config.trees[context.tree];
            path = tree.path_as_ref()?.to_string();
            // Sparse gardens/missing trees are ok -> skip these entries.
            if !model::print_tree(&tree, verbose, quiet) {
                continue;
            }
        }

        // The "error" flag is set when a non-zero exit status is returned.
        let mut error = false;

        // Get the current executable name
        let current_exe = cmd::current_exe();

        // One invocation runs multiple commands
        for name in commands {
            // One command maps to multiple command sequences.
            // When the scope is tree, only the tree's commands
            // are included.  When the scope includes a gardens,
            // its matching commands are appended to the end.
            error = false;
            let cmd_seq_vec = eval::command(app, context, &name);
            app.get_root_config_mut().reset();

            for cmd_seq in &cmd_seq_vec {
                for cmd_str in cmd_seq {
                    let mut exec = subprocess::Exec::shell(&cmd_str)
                        .arg(&current_exe)
                        .args(arguments)
                        .cwd(&path);
                    // Update the command environment
                    for (k, v) in &env {
                        exec = exec.env(k, v);
                    }
                    let status = cmd::status(exec.join());
                    if status != errors::EX_OK {
                        exit_status = status as i32;
                        error = true;
                        break;
                    }
                }
                if error {
                    break;
                }
            }
            if error {
                break;
            }
        }

        if error && !keep_going {
            break;
        }
    }

    // Return the last non-zero exit status.
    Ok(exit_status)
}

/// Run cmd() over a Vec of tree queries
pub fn cmds(
    app: &mut model::ApplicationContext,
    quiet: bool,
    verbose: bool,
    keep_going: bool,
    command: &str,
    queries: &[String],
    arguments: &[String],
) -> Result<()> {
    let mut exit_status: i32 = 0;

    let mut commands: Vec<String> = Vec::new();
    commands.push(command.to_string());

    for query in queries {
        let status = cmd(
            app, quiet, verbose, keep_going, &query, &commands, arguments,
        )
        .unwrap_or(errors::EX_IOERR);
        if status != 0 {
            exit_status = status;
            if !keep_going {
                break;
            }
        }
    }

    // Return the last non-zero exit status.
    cmd::result_from_exit_status(exit_status).map_err(|err| err.into())
}