Repository navigation
Expand file tree
/
Copy pathmain.rs
More file actions
163 lines (129 loc) · 4.37 KB
/
Copy pathmain.rs
File metadata and controls
163 lines (129 loc) · 4.37 KB
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
use std::io;
use commands::ComtryaCommand;
use clap::{Parser, Subcommand};
use comtrya_lib::contexts::build_contexts;
use comtrya_lib::contexts::Contexts;
use comtrya_lib::manifests;
use tracing::{error, Level};
#[allow(unused_imports)]
use tracing_subscriber::{fmt::writer::MakeWriterExt, layer::SubscriberExt, FmtSubscriber};
mod commands;
mod config;
use config::{load_config, Config};
#[derive(Parser, Debug)]
#[command(version, about, name="comtrya", long_about = None)]
struct GlobalArgs {
#[arg(short = 'd', long)]
pub manifest_directory: Option<String>,
/// Disable color printing
#[arg(long)]
pub no_color: bool,
/// Debug & tracing mode (-v, -vv)
#[arg(short, action = clap::ArgAction::Count)]
verbose: u8,
#[command(subcommand)]
command: Commands,
}
#[derive(Debug, Subcommand)]
enum Commands {
/// Apply manifests
#[clap(aliases = &["do", "run"])]
Apply(commands::Apply),
/// List manifests status (ALPHA)
Status(commands::Apply),
/// Print version information
Version(commands::Version),
/// List available contexts
Contexts(commands::Contexts),
/// Auto generate completions
///
/// for examples:
/// - bash: ```source <(comtrya gen-completions bash)```
/// - fish: ```comtrya gen-completions fish | source```
#[command(long_about, verbatim_doc_comment)]
GenCompletions(commands::GenCompletions),
}
#[derive(Debug)]
pub struct Runtime {
pub(crate) args: GlobalArgs,
pub(crate) config: Config,
pub(crate) contexts: Contexts,
}
pub(crate) fn execute(runtime: Runtime) -> anyhow::Result<()> {
match &runtime.args.command {
Commands::Apply(apply) => apply.execute(&runtime),
Commands::Status(apply) => apply.status(&runtime),
Commands::Version(version) => version.execute(&runtime),
Commands::Contexts(contexts) => c
8684
ontexts.execute(&runtime),
Commands::GenCompletions(gen_completions) => gen_completions.execute(&runtime),
}
}
fn configure_tracing(args: &GlobalArgs) {
let stdout_writer = match args.verbose {
0 => io::stdout.with_max_level(tracing::Level::INFO),
1 => io::stdout.with_max_level(tracing::Level::DEBUG),
_ => io::stdout.with_max_level(tracing::Level::TRACE),
};
let builder = FmtSubscriber::builder()
.with_max_level(Level::TRACE)
.with_ansi(!args.no_color)
.with_target(false)
.with_writer(stdout_writer)
.without_time();
#[cfg(target_os = "linux")]
if let Ok(layer) = tracing_journald::layer() {
tracing::subscriber::set_global_default(builder.finish().with(layer))
.expect("Unable to set a global subscriber");
return;
}
tracing::subscriber::set_global_default(builder.finish())
.expect("Unable to set a global subscriber");
}
fn main() -> anyhow::Result<()> {
let args = GlobalArgs::parse();
configure_tracing(&args);
let config = match load_config(&args) {
Ok(config) => config,
Err(error) => {
error!("{}", error.to_string());
panic!();
}
};
if !config.disable_update_check {
check_for_updates(args.no_color);
}
// Run Context Providers
let contexts = build_contexts(&config);
let runtime = Runtime {
args,
config,
contexts,
};
execute(runtime)?;
Ok(())
}
fn check_for_updates(no_color: bool) {
use colored::*;
use update_informer::{registry, Check};
if no_color {
control::set_override(false);
}
let pkg_name = env!("CARGO_PKG_NAME");
let pkg_version = env!("CARGO_PKG_VERSION");
let informer = update_informer::new(registry::Crates, pkg_name, pkg_version);
if let Some(new_version) = informer.check_version().ok().flatten() {
let msg = format!(
"A new version of {pkg_name} is available: v{pkg_version} -> {new_version}",
pkg_name = pkg_name.italic().cyan(),
new_version = new_version.to_string().green()
);
let release_url =
format!("https://github.com/{pkg_name}/{pkg_name}/releases/tag/{new_version}").blue();
let changelog = format!("Changelog: {release_url}",);
let cmd = format!(
"Run to update: {cmd}",
cmd = "curl -fsSL https://get.comtrya.dev | sh".green()
);
println!("\n{msg}\n{changelog}\n{cmd}");
}
}