microsoft/openvmm

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c8f70de480b5afd238530bd9f422db6753a653b2

Branches

Tags

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

Clone

HTTPS

Download ZIP

flowey/flowey_cli/src/cli/mod.rs

105lines · modecode

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