@@ -10,6 +10,7 @@ use wasm_pkg_client::{
1010 Client , PublishOpts ,
1111} ;
1212use wasm_pkg_common:: {
13+ self ,
1314 config:: { Config , RegistryMapping } ,
1415 package:: PackageSpec ,
1516 registry:: Registry ,
@@ -83,6 +84,8 @@ impl Common {
8384#[ derive( Subcommand , Debug ) ]
8485#[ allow( clippy:: large_enum_variant) ]
8586enum Commands {
87+ /// Set registry configuration
88+ Config ( ConfigArgs ) ,
8689 /// Download a package from a registry
8790 Get ( GetArgs ) ,
8891 /// Publish a package to a registry
@@ -95,6 +98,53 @@ enum Commands {
9598 Wit ( WitCommands ) ,
9699}
97100
101+ #[ derive( Args , Debug ) ]
102+ struct ConfigArgs {
103+ /// The default registry domain to use. Overrides configuration file(s).
104+ #[ arg( long = "default" , value_name = "DEFAULT" ) ]
105+ default : Option < Registry > ,
106+
107+ #[ command( flatten) ]
108+ common : Common ,
109+ }
110+
111+ impl ConfigArgs {
112+ pub async fn run ( self ) -> anyhow:: Result < ( ) > {
113+ // use config path provided, otherwise global config path
114+ let path = if let Some ( path) = self . common . config {
115+ path
116+ } else {
117+ Config :: global_config_path ( )
118+ . ok_or ( anyhow:: anyhow!( "global config path not available" ) ) ?
119+ } ;
120+
121+ // read file or use default config (not empty config)
122+ let mut config = match tokio:: fs:: read_to_string ( & path) . await {
123+ Ok ( contents) => Config :: from_toml ( & contents) ?,
124+ Err ( err) if err. kind ( ) == std:: io:: ErrorKind :: NotFound => Config :: default ( ) ,
125+ Err ( err) => return Err ( anyhow:: anyhow!( "error reading config file: {0}" , err) ) ,
126+ } ;
127+
128+ if let Some ( default) = self . default {
129+ // set default registry
130+ config. set_default_registry ( Some ( default) ) ;
131+
132+ // write config file
133+ config. to_file ( & path) . await ?;
134+ println ! ( "Updated config file: {path}" , path = path. display( ) ) ;
135+ }
136+
137+ // print config
138+ if let Some ( registry) = config. default_registry ( ) {
139+ println ! ( "Default registry: {}" , registry) ;
140+ } else {
141+ println ! ( "Default registry is not set" ) ;
142+ }
143+
144+ Ok ( ( ) )
145+ }
146+ }
147+
98148#[ derive( Args , Debug ) ]
99149struct GetArgs {
100150 /// Output path. If this ends with a '/', a filename based on the package
@@ -312,6 +362,7 @@ async fn main() -> anyhow::Result<()> {
312362 let cli = Cli :: parse ( ) ;
313363
314364 match cli. command {
365+ Commands :: Config ( args) => args. run ( ) . await ,
315366 Commands :: Get ( args) => args. run ( ) . await ,
316367 Commands :: Publish ( args) => args. run ( ) . await ,
317368 Commands :: Oci ( args) => args. run ( ) . await ,
0 commit comments