@@ -101,8 +101,12 @@ enum Commands {
101101#[ derive( Args , Debug ) ]
102102struct ConfigArgs {
103103 /// The default registry domain to use. Overrides configuration file(s).
104- #[ arg( long = "default" , value_name = "DEFAULT" ) ]
105- default : Option < Registry > ,
104+ #[ arg( long = "default-registry" , value_name = "DEFAULT_REGISTRY" ) ]
105+ default_registry : Option < Registry > ,
106+
107+ /// Opens editor defined in the `$EDITOR` environment variable.
108+ #[ arg( long, short, action) ]
109+ edit : bool ,
106110
107111 #[ command( flatten) ]
108112 common : Common ,
@@ -118,20 +122,39 @@ impl ConfigArgs {
118122 . ok_or ( anyhow:: anyhow!( "global config path not available" ) ) ?
119123 } ;
120124
125+ if self . edit {
126+ let editor = std:: env:: var ( "EDITOR" ) . or ( Err ( anyhow:: anyhow!(
127+ "failed to read `$EDITOR` environment variable"
128+ ) ) ) ?;
129+
130+ // create file if it doesn't exist
131+ if !path. is_file ( ) {
132+ Config :: default ( ) . to_file ( & path) . await ?;
133+ }
134+
135+ // launch editor
136+ std:: process:: Command :: new ( editor)
137+ . arg ( & path)
138+ . status ( )
139+ . context ( "failed to launch editor" ) ?;
140+ }
141+
121142 // read file or use default config (not empty config)
122143 let mut config = match tokio:: fs:: read_to_string ( & path) . await {
123144 Ok ( contents) => Config :: from_toml ( & contents) ?,
124145 Err ( err) if err. kind ( ) == std:: io:: ErrorKind :: NotFound => Config :: default ( ) ,
125146 Err ( err) => return Err ( anyhow:: anyhow!( "error reading config file: {0}" , err) ) ,
126147 } ;
127148
128- if let Some ( default) = self . default {
129- // set default registry
130- config. set_default_registry ( Some ( default) ) ;
149+ if !self . edit {
150+ if let Some ( default) = self . default_registry {
151+ // set default registry
152+ config. set_default_registry ( Some ( default) ) ;
131153
132- // write config file
133- config. to_file ( & path) . await ?;
134- println ! ( "Updated config file: {path}" , path = path. display( ) ) ;
154+ // write config file
155+ config. to_file ( & path) . await ?;
156+ println ! ( "Updated config file: {path}" , path = path. display( ) ) ;
157+ }
135158 }
136159
137160 // print config
0 commit comments