@@ -21,11 +21,11 @@ pub struct ConfigCommand {
21
21
22
22
/// Ignore federation hints.
23
23
#[ clap( long) ]
24
- pub ignore_federation_hints : bool ,
24
+ pub ignore_federation_hints : Option < bool > ,
25
25
26
26
/// Auto accept federation hints.
27
27
#[ clap( long) ]
28
- pub auto_accept_federation_hints : bool ,
28
+ pub auto_accept_federation_hints : Option < bool > ,
29
29
30
30
/// Overwrite the existing configuration file.
31
31
#[ clap( long) ]
@@ -53,66 +53,112 @@ impl ConfigCommand {
53
53
. path
54
54
. map ( Ok )
55
55
. unwrap_or_else ( Config :: default_config_path) ?;
56
+ let cwd = std:: env:: current_dir ( ) . context ( "failed to determine current directory" ) ?;
56
57
57
- if ! self . overwrite && path. is_file ( ) {
58
- bail ! (
59
- "configuration file `{path}` already exists; use `--overwrite` to overwrite it " ,
58
+ if self . overwrite && path. is_file ( ) {
59
+ println ! (
60
+ "Overwriting configuration file: `{path}`" ,
60
61
path = path. display( )
61
62
) ;
62
63
}
63
64
64
- let home_url = & self
65
- . common
66
- . registry
67
- . clone ( )
68
- . map ( RegistryUrl :: new)
69
- . transpose ( ) ?
70
- . map ( |u| u. to_string ( ) ) ;
65
+ let mut changing_home_registry = false ;
66
+
67
+ let config = if self . overwrite {
68
+ let home_url = self
69
+ . common
70
+ . registry
71
+ . as_ref ( )
72
+ . map ( RegistryUrl :: new)
73
+ . transpose ( ) ?
74
+ . map ( |u| u. to_string ( ) )
75
+ . ok_or ( anyhow:: anyhow!(
76
+ "Please configure your home registry: warg config --registry <registry-url>"
77
+ ) ) ?;
78
+
79
+ changing_home_registry = true ;
80
+
81
+ Config {
82
+ home_url : Some ( home_url) ,
83
+ registries_dir : self . registries_dir . map ( |p| cwd. join ( p) ) ,
84
+ content_dir : self . content_dir . map ( |p| cwd. join ( p) ) ,
85
+ namespace_map_path : self . namespace_path . map ( |p| cwd. join ( p) ) ,
86
+ keys : self . common . read_config ( ) ?. keys ,
87
+ keyring_auth : false ,
88
+ ignore_federation_hints : self . ignore_federation_hints . unwrap_or_default ( ) ,
89
+ auto_accept_federation_hints : self . auto_accept_federation_hints . unwrap_or_default ( ) ,
90
+ disable_interactive : false ,
91
+ keyring_backend : self . keyring_backend ,
92
+ }
93
+ } else {
94
+ let mut config = self . common . read_config ( ) ?;
95
+ if self . common . registry . is_some ( ) {
96
+ let home_url = self
97
+ . common
98
+ . registry
99
+ . as_ref ( )
100
+ . map ( RegistryUrl :: new)
101
+ . transpose ( ) ?
102
+ . map ( |u| u. to_string ( ) ) ;
103
+ if home_url != config. home_url {
104
+ changing_home_registry = true ;
105
+ config. home_url = home_url;
106
+ }
107
+ }
108
+ if config. home_url . is_none ( ) {
109
+ bail ! ( "Please configure your home registry: warg config --registry <registry-url>" ) ;
110
+ }
111
+ if self . registries_dir . is_some ( ) {
112
+ config. registries_dir = self . registries_dir . map ( |p| cwd. join ( p) ) ;
113
+ }
114
+ if self . content_dir . is_some ( ) {
115
+ config. content_dir = self . content_dir . map ( |p| cwd. join ( p) ) ;
116
+ }
117
+ if self . namespace_path . is_some ( ) {
118
+ config. namespace_map_path = self . namespace_path . map ( |p| cwd. join ( p) ) ;
119
+ }
120
+ if let Some ( ignore_federation_hints) = self . ignore_federation_hints {
121
+ config. ignore_federation_hints = ignore_federation_hints;
122
+ }
123
+ if let Some ( auto_accept_federation_hints) = self . auto_accept_federation_hints {
124
+ config. auto_accept_federation_hints = auto_accept_federation_hints;
125
+ }
126
+ if self . keyring_backend . is_some ( ) {
127
+ config. keyring_backend = self . keyring_backend ;
128
+ }
129
+
130
+ config
131
+ } ;
71
132
72
133
// The paths specified on the command line are relative to the current
73
134
// directory.
74
135
//
75
136
// `write_to_file` will handle normalizing the paths to be relative to
76
137
// the configuration file's directory.
77
- let cwd = std:: env:: current_dir ( ) . context ( "failed to determine current directory" ) ?;
78
- let config = Config {
79
- home_url : home_url. clone ( ) ,
80
- registries_dir : self . registries_dir . map ( |p| cwd. join ( p) ) ,
81
- content_dir : self . content_dir . map ( |p| cwd. join ( p) ) ,
82
- namespace_map_path : self . namespace_path . map ( |p| cwd. join ( p) ) ,
83
- keys : self . common . read_config ( ) ?. keys ,
84
- keyring_auth : false ,
85
- ignore_federation_hints : self . ignore_federation_hints ,
86
- auto_accept_federation_hints : self . auto_accept_federation_hints ,
87
- disable_interactive : false ,
88
- keyring_backend : self . keyring_backend . clone ( ) ,
89
- } ;
90
-
91
138
config. write_to_file ( & path) ?;
92
139
93
140
// reset when changing home registry
94
- let client = self . common . create_client ( & config) ?;
95
- client. reset_namespaces ( ) . await ?;
96
- client. reset_registry ( ) . await ?;
141
+ if changing_home_registry {
142
+ let client = self . common . create_client ( & config) ?;
143
+ client. reset_namespaces ( ) . await ?;
144
+ client. reset_registry ( ) . await ?;
145
+ }
97
146
98
- println ! (
99
- "created warg configuration file `{path}`" ,
100
- path = path. display( ) ,
101
- ) ;
147
+ println ! ( "Set configuration file `{path}`" , path = path. display( ) , ) ;
102
148
103
149
Ok ( ( ) )
104
150
}
105
151
}
106
152
107
- fn keyring_backend_parser ( s : & str ) -> Result < String , String > {
153
+ pub ( crate ) fn keyring_backend_parser ( s : & str ) -> Result < String , String > {
108
154
if Keyring :: SUPPORTED_BACKENDS . contains ( & s) {
109
155
Ok ( s. to_string ( ) )
110
156
} else {
111
157
Err ( format ! ( "`{s}` is not a supported keyring backend." ) )
112
158
}
113
159
}
114
160
115
- fn keyring_backend_help ( ) -> clap:: builder:: StyledStr {
161
+ pub ( crate ) fn keyring_backend_help ( ) -> clap:: builder:: StyledStr {
116
162
use std:: fmt:: Write as _;
117
163
118
164
let mut help = String :: new ( ) ;
0 commit comments