Skip to content

Commit d3e5206

Browse files
committed
main: Execute shell completions earlier.
This allows shell completions to be run without a devcontainer-index copy. Remove `program_name` function in favor of build time CARGO_BIN_NAME env.
1 parent ddb6ec9 commit d3e5206

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

src/main.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use std::env;
2-
use std::ffi::OsStr;
32
use std::fs;
43
use std::io;
54
use std::path::Path;
65
use std::path::PathBuf;
76

8-
#[cfg(feature = "completions")]
9-
use clap::CommandFactory;
107
use clap::{Parser, Subcommand};
11-
#[cfg(feature = "completions")]
12-
use clap_complete::{generate, shells::Shell};
138
use clap_verbosity_flag::{Verbosity, WarnLevel};
9+
#[cfg(feature = "completions")]
10+
use ::{
11+
clap::CommandFactory,
12+
clap_complete::{generate, shells::Shell},
13+
};
1414

1515
mod init;
1616
mod inspect;
@@ -49,15 +49,6 @@ enum Commands {
4949
Search(search::SearchArgs),
5050
}
5151

52-
fn program_name() -> io::Result<String> {
53-
log::debug!("program_name");
54-
let exe = env::current_exe()?;
55-
exe.file_name()
56-
.and_then(OsStr::to_str)
57-
.map(String::from)
58-
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Executable not a file path"))
59-
}
60-
6152
fn data_directory<P: AsRef<Path>>(namespace: P) -> io::Result<PathBuf> {
6253
log::debug!("data_directory");
6354
if let Some(path) = dirs::data_dir() {
@@ -78,8 +69,15 @@ fn main() -> Result<(), anyhow::Error> {
7869
.format_timestamp_millis()
7970
.init();
8071

81-
let prog_name = program_name()?;
82-
let data_dir = data_directory(&prog_name)?;
72+
const BIN_NAME: &str = env!("CARGO_BIN_NAME");
73+
74+
#[cfg(feature = "completions")]
75+
if let Some(Commands::Completions { shell }) = args.command {
76+
generate(shell, &mut Args::command_for_update(), BIN_NAME, &mut io::stdout());
77+
return Ok(());
78+
}
79+
80+
let data_dir = data_directory(BIN_NAME)?;
8381
let index_file = data_dir.join("devcontainer-index.json");
8482

8583
if args.pull_index {
@@ -97,17 +95,15 @@ fn main() -> Result<(), anyhow::Error> {
9795
// suggested user action
9896
log::error!(
9997
"Missing devcontainer-index.json.\n\n\tRun `{} --pull-index`.\n",
100-
prog_name
98+
BIN_NAME
10199
);
102100
}
103101

104102
let index = registry::read_devcontainer_index(index_file)?;
105103

106104
match command {
107105
#[cfg(feature = "completions")]
108-
Commands::Completions { shell } => {
109-
generate(shell, &mut Args::command_for_update(), &prog_name, &mut io::stdout());
110-
},
106+
Commands::Completions { .. } => unreachable!(),
111107
Commands::Init(args) => init::init(&index, args)?,
112108
Commands::Inspect(args) => inspect::inspect(&index, args)?,
113109
Commands::List(args) => list::list(&index, args),

0 commit comments

Comments
 (0)