-
Notifications
You must be signed in to change notification settings - Fork 377
/
Copy pathmain.rs
182 lines (169 loc) · 6.76 KB
/
main.rs
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
use std::{os::unix::process::CommandExt, process::Command};
use clap::crate_version;
use devenv::{
cli::{Cli, Commands, ContainerCommand, InputsCommand, ProcessesCommand, TasksCommand},
config, log, Devenv,
};
use miette::Result;
use tracing::{info, warn};
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse_and_resolve_options();
let print_version = || {
println!(
"devenv {} ({})",
crate_version!(),
cli.global_options.system
);
Ok(())
};
let command = match cli.command {
None | Some(Commands::Version) => return print_version(),
Some(Commands::Direnvrc) => {
print!("{}", *devenv::DIRENVRC);
return Ok(());
}
Some(cmd) => cmd,
};
let level = if cli.global_options.verbose {
log::Level::Debug
} else if cli.global_options.quiet {
log::Level::Silent
} else {
log::Level::default()
};
log::init_tracing(level, cli.global_options.log_format);
let mut config = config::Config::load()?;
for input in cli.global_options.override_input.chunks_exact(2) {
config.add_input(&input[0].clone(), &input[1].clone(), &[]);
}
let mut options = devenv::DevenvOptions {
global_options: Some(cli.global_options),
config,
..Default::default()
};
// we let Drop delete the dir after all commands have ran
let _tmpdir = if let Commands::Test {
dont_override_dotfile,
} = command
{
let pwd = std::env::current_dir().expect("Failed to get current directory");
let tmpdir =
tempdir::TempDir::new_in(pwd, ".devenv").expect("Failed to create temporary directory");
if !dont_override_dotfile {
info!(
"Overriding .devenv to {}",
tmpdir.path().file_name().unwrap().to_str().unwrap()
);
options.devenv_dotfile = Some(tmpdir.path().to_path_buf());
}
Some(tmpdir)
} else {
None
};
let mut devenv = Devenv::new(options).await;
match command {
Commands::Shell { cmd, args } => devenv.shell(&cmd, &args, true).await,
Commands::Test { .. } => devenv.test().await,
Commands::Container {
registry,
copy,
docker_run,
copy_args,
name,
command,
} => {
devenv.container_name = name.clone();
match name {
None => {
if let Some(c) = command {
match c {
ContainerCommand::Build { name } => {
devenv.container_name = Some(name.clone());
let _ = devenv.container_build(&name).await?;
}
ContainerCommand::Copy { name } => {
devenv.container_name = Some(name.clone());
devenv
.container_copy(&name, ©_args, registry.as_deref())
.await?;
}
ContainerCommand::Run { name } => {
devenv.container_name = Some(name.clone());
devenv
.container_run(&name, ©_args, registry.as_deref())
.await?;
}
}
}
}
Some(name) => {
match (copy, docker_run) {
(true, false) => {
warn!("--copy flag is deprecated, use `devenv container copy` instead",);
devenv
.container_copy(&name, ©_args, registry.as_deref())
.await?;
}
(_, true) => {
warn!(
"--docker-run flag is deprecated, use `devenv container run` instead",
);
devenv
.container_run(&name, ©_args, registry.as_deref())
.await?;
}
_ => {
warn!("Calling without a subcommand is deprecated, use `devenv container build` instead");
let _ = devenv.container_build(&name).await?;
}
};
}
};
Ok(())
}
Commands::Init { target, template } => devenv.init(&target, &template).await,
Commands::Generate { .. } => match which::which("devenv-generate") {
Ok(devenv_generate) => {
let error = Command::new(devenv_generate)
.args(std::env::args().skip(1).filter(|arg| arg != "generate"))
.exec();
miette::bail!("failed to execute devenv-generate {error}");
}
Err(_) => {
miette::bail!(indoc::formatdoc! {"
devenv-generate was not found in PATH
It was moved to a separate binary due to https://github.com/cachix/devenv/issues/1733
"})
}
},
Commands::Search { name } => devenv.search(&name).await,
Commands::Gc {} => devenv.gc(),
Commands::Info {} => devenv.info().await,
Commands::Repl {} => devenv.repl().await,
Commands::Build { attributes } => devenv.build(&attributes).await,
Commands::Update { name } => devenv.update(&name).await,
Commands::Up { process, detach } => devenv.up(process.as_deref(), &detach, &detach).await,
Commands::Processes { command } => match command {
ProcessesCommand::Up { process, detach } => {
devenv.up(process.as_deref(), &detach, &detach).await
}
ProcessesCommand::Down {} => devenv.down(),
},
Commands::Tasks { command } => match command {
TasksCommand::Run { tasks } => devenv.tasks_run(tasks).await,
},
Commands::Inputs { command } => match command {
InputsCommand::Add { name, url, follows } => devenv.inputs_add(&name, &url, &follows),
},
// hidden
Commands::Assemble => devenv.assemble(false).await,
Commands::PrintDevEnv { json } => devenv.print_dev_env(json).await,
Commands::GenerateJSONSchema => {
config::write_json_schema();
Ok(())
}
Commands::Direnvrc => unreachable!(),
Commands::Version => unreachable!(),
}
}