11use std:: env;
2- use std:: ffi:: OsStr ;
32use std:: fs;
43use std:: io;
54use std:: path:: Path ;
65use std:: path:: PathBuf ;
76
8- #[ cfg( feature = "completions" ) ]
9- use clap:: CommandFactory ;
107use clap:: { Parser , Subcommand } ;
11- #[ cfg( feature = "completions" ) ]
12- use clap_complete:: { generate, shells:: Shell } ;
138use clap_verbosity_flag:: { Verbosity , WarnLevel } ;
9+ #[ cfg( feature = "completions" ) ]
10+ use :: {
11+ clap:: CommandFactory ,
12+ clap_complete:: { generate, shells:: Shell } ,
13+ } ;
1414
1515mod init;
1616mod 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-
6152fn 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 \t Run `{} --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