microsoft/openvmm

Public

mirrored from https://github.com/microsoft/openvmmAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b8910f97c258ca39c8f2e535fbc241ea1e1be3ff

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

flowey/flowey_cli/src/cli/mod.rs

106lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! A set of CLI commands which are common to _all_ flowey implementations.
5
6use clap::Parser;
7use clap::Subcommand;
8use clap::ValueEnum;
9use flowey_core::node::FlowBackend;
10use flowey_core::pipeline::IntoPipeline;
11use serde::Deserialize;
12use serde::Serialize;
13use std::path::Path;
14
15pub mod debug;
16pub mod exec_snippet;
17pub mod pipeline;
18pub mod regen;
19pub mod var_db;
20
21#[derive(Parser)]
22#[clap(name = "flowey", about = "backend-agnostic declarative flow runner")]
23struct Cli<P: Subcommand> {
24 #[clap(subcommand)]
25 command: Commands<P>,
26}
27
28#[derive(Subcommand)]
29enum Commands<P: Subcommand> {
30 Pipeline(pipeline::Pipeline<P>),
31 Regen(regen::Regen),
32
33 #[clap(hide = true, alias = "e")]
34 ExecSnippet(exec_snippet::ExecSnippet),
35 #[clap(hide = true, alias = "v")]
36 VarDb(var_db::VarDb),
37
38 #[clap(subcommand)]
39 Debug(debug::DebugCommands),
40}
41
42pub fn cli_main<P: Subcommand + IntoPipeline>(
43 flowey_crate: &str,
44 repo_root: &Path,
45) -> anyhow::Result<()> {
46 let cli = Cli::<P>::parse();
47
48 // Check if the runtime variable DB includes a "FLOWEY_LOG" variable, and
49 // use that to override the log level.
50 //
51 // This mechanism is in-place because some YAML pipeline defn langs (*cough*
52 // ADO *cough*) don't let you set pipeline-level env vars which are
53 // automatically inherited by all shell contexts.
54 if let Commands::VarDb(var_db::VarDb { job_idx, .. })
55 | Commands::ExecSnippet(exec_snippet::ExecSnippet { job_idx, .. }) = &cli.command
56 {
57 let _ = try_inject_flowey_log(*job_idx);
58 }
59
60 ci_logger::init("FLOWEY_LOG").unwrap();
61
62 match cli.command {
63 Commands::Debug(cmd) => cmd.run(),
64 Commands::Pipeline(cmd) => cmd.run(flowey_crate, repo_root),
65 Commands::Regen(cmd) => cmd.run(repo_root),
66 Commands::ExecSnippet(cmd) => cmd.run(),
67 Commands::VarDb(cmd) => cmd.run(),
68 }
69}
70
71#[derive(Copy, Clone, ValueEnum, Serialize, Deserialize)]
72pub enum FlowBackendCli {
73 Local,
74 Ado,
75 Github,
76}
77
78impl From<FlowBackendCli> for FlowBackend {
79 fn from(v: FlowBackendCli) -> FlowBackend {
80 match v {
81 FlowBackendCli::Local => FlowBackend::Local,
82 FlowBackendCli::Ado => FlowBackend::Ado,
83 FlowBackendCli::Github => FlowBackend::Github,
84 }
85 }
86}
87
88fn try_inject_flowey_log(job_idx: usize) -> anyhow::Result<()> {
89 // skip if the env var is already set
90 if std::env::var("FLOWEY_LOG").is_err() {
91 let log_level = var_db::open_var_db(job_idx)?
92 .try_get_var("FLOWEY_LOG")
93 .map(|val| {
94 serde_json::from_slice::<String>(&val)
95 .expect("found FLOWEY_LOG in db, but it wasn't a json string!")
96 });
97
98 if let Some(log_level) = log_level {
99 // Yes, this is a hack... but I kinda don't want to go update the
100 // ci_logger crate right now
101 std::env::set_var("FLOWEY_LOG", log_level)
102 }
103 }
104
105 Ok(())
106}
107