dvcli 0.4.0

CLI client for querying Gradle build information from Develocity
Documentation
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Configuration loading and merging for the CLI.
//!
//! Configuration is loaded from multiple sources with the following priority:
//! 1. CLI arguments (highest)
//! 2. Environment variables
//! 3. Config file (~/.develocity/config.toml)
//! 4. Built-in defaults (lowest)

use crate::error::{Error, Result};
use crate::models::TestOutcome;
use serde::Deserialize;
use std::path::{Path, PathBuf};

/// The default config file path relative to the user's home directory.
const DEFAULT_CONFIG_DIR: &str = ".develocity";
const DEFAULT_CONFIG_FILE: &str = "config.toml";

/// Configuration loaded from the TOML config file.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct ConfigFile {
    /// Develocity server URL.
    pub server: Option<String>,
    /// Access token for authentication.
    pub token: Option<String>,
    /// Default output format.
    pub output_format: Option<String>,
    /// Default verbose setting.
    pub verbose: Option<bool>,
    /// Default timeout in seconds.
    pub timeout: Option<u64>,
}

impl ConfigFile {
    /// Load configuration from the default config file path.
    pub fn load_default() -> Result<Self> {
        let path = Self::default_config_path();
        if path.exists() {
            Self::load(&path)
        } else {
            Ok(Self::default())
        }
    }

    /// Load configuration from a specific file path.
    pub fn load(path: &Path) -> Result<Self> {
        let content = std::fs::read_to_string(path).map_err(|e| Error::ConfigRead {
            path: path.display().to_string(),
            source: e,
        })?;

        toml::from_str(&content).map_err(|e| Error::ConfigParse {
            path: path.display().to_string(),
            source: e,
        })
    }

    /// Returns the default config file path (~/.develocity/config.toml).
    pub fn default_config_path() -> PathBuf {
        dirs::home_dir()
            .unwrap_or_else(|| PathBuf::from("."))
            .join(DEFAULT_CONFIG_DIR)
            .join(DEFAULT_CONFIG_FILE)
    }
}

/// Resolved configuration after merging all sources.
#[derive(Debug, Clone)]
pub struct Config {
    /// Develocity server URL.
    pub server: String,
    /// Access token for authentication.
    pub token: String,
    /// Output format.
    pub output_format: OutputFormat,
    /// What data to include in the output.
    pub include: IncludeOptions,
    /// Whether to show verbose output (stacktraces, etc.).
    pub verbose: bool,
    /// Request timeout in seconds.
    pub timeout: u64,
    /// Filter test results by these outcomes (server-side). Empty means all.
    pub test_outcomes: Vec<TestOutcome>,
}

/// Output format for the CLI.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum OutputFormat {
    /// JSON output for machine consumption.
    Json,
    /// Human-readable colored terminal output.
    #[default]
    Human,
}

impl std::str::FromStr for OutputFormat {
    type Err = String;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "json" => Ok(OutputFormat::Json),
            "human" => Ok(OutputFormat::Human),
            other => Err(format!(
                "Invalid output format '{}'. Valid options: json, human",
                other
            )),
        }
    }
}

impl std::fmt::Display for OutputFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            OutputFormat::Json => write!(f, "json"),
            OutputFormat::Human => write!(f, "human"),
        }
    }
}

/// Options for what data to include in the output.
#[derive(Debug, Clone, Default)]
pub struct IncludeOptions {
    /// Include build result information.
    pub result: bool,
    /// Include deprecation information.
    pub deprecations: bool,
    /// Include failure information.
    pub failures: bool,
    /// Include test execution results.
    pub tests: bool,
    /// Include task execution / build cache performance data.
    pub task_execution: bool,
    /// Include network activity data.
    pub network_activity: bool,
    /// Include resolved dependencies.
    pub dependencies: bool,
}

impl IncludeOptions {
    /// Create options that include everything.
    pub fn all() -> Self {
        Self {
            result: true,
            deprecations: true,
            failures: true,
            tests: true,
            task_execution: true,
            network_activity: true,
            dependencies: true,
        }
    }

    /// Parse include options from a comma-separated string.
    pub fn parse(s: &str) -> std::result::Result<Self, String> {
        let s = s.trim().to_lowercase();

        if s == "all" {
            return Ok(Self::all());
        }

        let mut opts = Self::default();

        for part in s.split(',') {
            match part.trim() {
                "result" => opts.result = true,
                "deprecations" => opts.deprecations = true,
                "failures" => opts.failures = true,
                "tests" => opts.tests = true,
                "task-execution" => opts.task_execution = true,
                "network-activity" => opts.network_activity = true,
                "dependencies" => opts.dependencies = true,
                "all" => return Ok(Self::all()),
                other if !other.is_empty() => {
                    return Err(format!(
                        "Invalid include option '{}'. Valid options: result, deprecations, failures, tests, task-execution, network-activity, dependencies, all",
                        other
                    ));
                }
                _ => {}
            }
        }

        // If nothing was specified, include everything
        if !opts.result
            && !opts.deprecations
            && !opts.failures
            && !opts.tests
            && !opts.task_execution
            && !opts.network_activity
            && !opts.dependencies
        {
            return Ok(Self::all());
        }

        Ok(opts)
    }

    /// Returns true if any option is enabled.
    pub fn any(&self) -> bool {
        self.result
            || self.deprecations
            || self.failures
            || self.tests
            || self.task_execution
            || self.network_activity
            || self.dependencies
    }
}

/// Builder for creating a resolved Config from multiple sources.
#[derive(Debug, Default)]
pub struct ConfigBuilder {
    server: Option<String>,
    token: Option<String>,
    output_format: Option<OutputFormat>,
    include: Option<IncludeOptions>,
    verbose: Option<bool>,
    timeout: Option<u64>,
    config_file_path: Option<PathBuf>,
    test_outcomes: Vec<TestOutcome>,
}

impl ConfigBuilder {
    /// Create a new config builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the server URL (from CLI arg).
    pub fn server(mut self, server: Option<String>) -> Self {
        if server.is_some() {
            self.server = server;
        }
        self
    }

    /// Set the access token (from CLI arg).
    pub fn token(mut self, token: Option<String>) -> Self {
        if token.is_some() {
            self.token = token;
        }
        self
    }

    /// Set the output format (from CLI arg).
    pub fn output_format(mut self, format: Option<OutputFormat>) -> Self {
        if format.is_some() {
            self.output_format = format;
        }
        self
    }

    /// Set the include options (from CLI arg).
    pub fn include(mut self, include: Option<IncludeOptions>) -> Self {
        if include.is_some() {
            self.include = include;
        }
        self
    }

    /// Set verbose mode (from CLI arg).
    pub fn verbose(mut self, verbose: bool) -> Self {
        if verbose {
            self.verbose = Some(true);
        }
        self
    }

    /// Set timeout (from CLI arg).
    pub fn timeout(mut self, timeout: Option<u64>) -> Self {
        if timeout.is_some() {
            self.timeout = timeout;
        }
        self
    }

    /// Set the config file path.
    pub fn config_file(mut self, path: Option<PathBuf>) -> Self {
        self.config_file_path = path;
        self
    }

    /// Set the test outcome filters.
    pub fn test_outcomes(mut self, outcomes: Vec<TestOutcome>) -> Self {
        self.test_outcomes = outcomes;
        self
    }

    /// Build the resolved configuration by merging all sources.
    pub fn build(self) -> Result<Config> {
        // Load config file
        let config_file = match &self.config_file_path {
            Some(path) => ConfigFile::load(path)?,
            None => ConfigFile::load_default()?,
        };

        // Resolve server: CLI > env > config file
        let server = self
            .server
            .or_else(|| std::env::var("DEVELOCITY_SERVER").ok())
            .or(config_file.server)
            .ok_or(Error::MissingServer)?;

        // Resolve token: CLI > env > config file
        let token = self
            .token
            .or_else(|| std::env::var("DEVELOCITY_API_KEY").ok())
            .or(config_file.token)
            .ok_or(Error::MissingToken)?;

        // Resolve output format: CLI > config file > default
        let output_format = self.output_format.unwrap_or_else(|| {
            config_file
                .output_format
                .and_then(|s| s.parse().ok())
                .unwrap_or_default()
        });

        // Resolve include options: CLI > default (all)
        let include = self.include.unwrap_or_else(IncludeOptions::all);

        // Resolve verbose: CLI > config file > default (false)
        let verbose = self
            .verbose
            .unwrap_or_else(|| config_file.verbose.unwrap_or(false));

        // Resolve timeout: CLI > config file > default (30)
        let timeout = self
            .timeout
            .unwrap_or_else(|| config_file.timeout.unwrap_or(30));

        Ok(Config {
            server,
            token,
            output_format,
            include,
            verbose,
            timeout,
            test_outcomes: self.test_outcomes,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_include_options_parse_all() {
        let opts = IncludeOptions::parse("all").unwrap();
        assert!(opts.result);
        assert!(opts.deprecations);
        assert!(opts.failures);
        assert!(opts.tests);
        assert!(opts.task_execution);
        assert!(opts.network_activity);
        assert!(opts.dependencies);
    }

    #[test]
    fn test_include_options_parse_single() {
        let opts = IncludeOptions::parse("result").unwrap();
        assert!(opts.result);
        assert!(!opts.deprecations);
        assert!(!opts.failures);
        assert!(!opts.tests);
        assert!(!opts.task_execution);
    }

    #[test]
    fn test_include_options_parse_multiple() {
        let opts = IncludeOptions::parse("result,failures").unwrap();
        assert!(opts.result);
        assert!(!opts.deprecations);
        assert!(opts.failures);
        assert!(!opts.tests);
        assert!(!opts.task_execution);
    }

    #[test]
    fn test_include_options_parse_tests() {
        let opts = IncludeOptions::parse("tests").unwrap();
        assert!(!opts.result);
        assert!(!opts.deprecations);
        assert!(!opts.failures);
        assert!(opts.tests);
        assert!(!opts.task_execution);
    }

    #[test]
    fn test_include_options_parse_multiple_with_tests() {
        let opts = IncludeOptions::parse("result,tests").unwrap();
        assert!(opts.result);
        assert!(!opts.deprecations);
        assert!(!opts.failures);
        assert!(opts.tests);
        assert!(!opts.task_execution);
    }

    #[test]
    fn test_include_options_parse_task_execution() {
        let opts = IncludeOptions::parse("task-execution").unwrap();
        assert!(!opts.result);
        assert!(!opts.deprecations);
        assert!(!opts.failures);
        assert!(!opts.tests);
        assert!(opts.task_execution);
    }

    #[test]
    fn test_include_options_parse_network_activity() {
        let opts = IncludeOptions::parse("network-activity").unwrap();
        assert!(!opts.result);
        assert!(!opts.deprecations);
        assert!(!opts.failures);
        assert!(!opts.tests);
        assert!(!opts.task_execution);
        assert!(opts.network_activity);
    }

    #[test]
    fn test_include_options_parse_dependencies() {
        let opts = IncludeOptions::parse("dependencies").unwrap();
        assert!(!opts.result);
        assert!(!opts.deprecations);
        assert!(!opts.failures);
        assert!(!opts.tests);
        assert!(!opts.task_execution);
        assert!(!opts.network_activity);
        assert!(opts.dependencies);
    }

    #[test]
    fn test_include_options_parse_empty() {
        let opts = IncludeOptions::parse("").unwrap();
        // Empty means all
        assert!(opts.result);
        assert!(opts.deprecations);
        assert!(opts.failures);
        assert!(opts.tests);
        assert!(opts.task_execution);
        assert!(opts.network_activity);
        assert!(opts.dependencies);
    }

    #[test]
    fn test_include_options_parse_invalid() {
        let result = IncludeOptions::parse("invalid");
        assert!(result.is_err());
    }

    #[test]
    fn test_output_format_parse() {
        assert_eq!("json".parse::<OutputFormat>().unwrap(), OutputFormat::Json);
        assert_eq!("JSON".parse::<OutputFormat>().unwrap(), OutputFormat::Json);
        assert_eq!(
            "human".parse::<OutputFormat>().unwrap(),
            OutputFormat::Human
        );
        assert!("invalid".parse::<OutputFormat>().is_err());
    }
}