@@ -9,9 +9,13 @@ mod list;
9
9
mod pixi_lock;
10
10
mod read_remote;
11
11
12
+ use std:: { io, io:: Write } ;
13
+
14
+ use cli:: { Cli , CondaDenyCliConfig } ;
12
15
use colored:: Colorize ;
16
+ use conda_deny_config:: CondaDenyTomlConfig ;
13
17
use license_info:: LicenseInfo ;
14
- use license_whitelist:: IgnorePackage ;
18
+ use license_whitelist:: { get_license_information_from_toml_config , IgnorePackage } ;
15
19
16
20
use anyhow:: { Context , Result } ;
17
21
use log:: debug;
@@ -61,7 +65,184 @@ pub fn fetch_license_infos(config: &CondaDenyCheckConfig) -> Result<LicenseInfos
61
65
}
62
66
}
63
67
64
- pub fn list ( config : & CondaDenyListConfig ) -> Result < ( ) > {
68
+ pub fn get_config_options (
69
+ config : Option < String > ,
70
+ cli_config : CondaDenyCliConfig ,
71
+ ) -> Result < CondaDenyConfig > {
72
+ let toml_config = if let Some ( config_path) = config {
73
+ CondaDenyTomlConfig :: from_path ( config_path. as_str ( ) )
74
+ . with_context ( || format ! ( "Failed to parse config file {}" , config_path) ) ?
75
+ } else {
76
+ match CondaDenyTomlConfig :: from_path ( "pixi.toml" )
77
+ . with_context ( || "Failed to parse config file pixi.toml" )
78
+ {
79
+ Ok ( config) => {
80
+ debug ! ( "Successfully loaded config from pixi.toml" ) ;
81
+ config
82
+ }
83
+ Err ( e) => {
84
+ debug ! (
85
+ "Error parsing config file: pixi.toml: {}. Attempting to use pyproject.toml instead..." ,
86
+ e
87
+ ) ;
88
+ CondaDenyTomlConfig :: from_path ( "pyproject.toml" )
89
+ . context ( e)
90
+ . with_context ( || "Failed to parse config file pyproject.toml" ) ?
91
+ }
92
+ }
93
+ } ;
94
+
95
+ debug ! ( "Parsed TOML config: {:?}" , toml_config) ;
96
+
97
+ let config = match cli_config {
98
+ CondaDenyCliConfig :: Check {
99
+ lockfile,
100
+ prefix,
101
+ platform,
102
+ environment,
103
+ include_safe,
104
+ osi,
105
+ ignore_pypi,
106
+ } => {
107
+ // cli overrides toml configuration
108
+ let lockfile = lockfile. unwrap_or ( toml_config. get_lockfile_spec ( ) ) ;
109
+ let prefix = prefix. unwrap_or_default ( ) ;
110
+ if lockfile. is_empty ( ) && prefix. is_empty ( ) {
111
+ return Err ( anyhow:: anyhow!( "No lockfiles or conda prefixes provided" ) ) ;
112
+ }
113
+
114
+ let platform = if platform. is_some ( ) {
115
+ platform
116
+ } else {
117
+ toml_config. get_platform_spec ( )
118
+ } ;
119
+ if platform. is_some ( ) && !prefix. is_empty ( ) {
120
+ return Err ( anyhow:: anyhow!(
121
+ "Cannot specify platforms and conda prefixes at the same time"
122
+ ) ) ;
123
+ }
124
+
125
+ let environment = if environment. is_some ( ) {
126
+ environment
127
+ } else {
128
+ toml_config. get_environment_spec ( )
129
+ } ;
130
+ if environment. is_some ( ) && !prefix. is_empty ( ) {
131
+ return Err ( anyhow:: anyhow!(
132
+ "Cannot specify environments and conda prefixes at the same time"
133
+ ) ) ;
134
+ }
135
+
136
+ let license_whitelist = toml_config. get_license_whitelists ( ) ;
137
+
138
+ // defaults to false
139
+ let osi = if osi. is_some ( ) {
140
+ osi
141
+ } else {
142
+ toml_config. get_osi ( )
143
+ }
144
+ . unwrap_or ( false ) ;
145
+ if osi && !license_whitelist. is_empty ( ) {
146
+ return Err ( anyhow:: anyhow!(
147
+ "Cannot use OSI mode and license whitelists at the same time"
148
+ ) ) ;
149
+ }
150
+
151
+ if !osi && license_whitelist. is_empty ( ) {
152
+ return Err ( anyhow:: anyhow!( "No license whitelist provided" ) ) ;
153
+ }
154
+
155
+ // defaults to false
156
+ let ignore_pypi = if ignore_pypi. is_some ( ) {
157
+ ignore_pypi
158
+ } else {
159
+ toml_config. get_ignore_pypi ( )
160
+ }
161
+ . unwrap_or ( false ) ;
162
+
163
+ let ( safe_licenses, ignore_packages) =
164
+ get_license_information_from_toml_config ( toml_config) ?;
165
+
166
+ CondaDenyConfig :: Check ( CondaDenyCheckConfig {
167
+ prefix,
168
+ lockfile,
169
+ platform,
170
+ environment,
171
+ include_safe,
172
+ osi,
173
+ ignore_pypi,
174
+ safe_licenses,
175
+ ignore_packages,
176
+ } )
177
+ }
178
+ CondaDenyCliConfig :: List {
179
+ lockfile,
180
+ prefix,
181
+ platform,
182
+ environment,
183
+ } => {
184
+ // todo: refactor with check
185
+ // cli overrides toml configuration
186
+ let lockfile = lockfile. unwrap_or ( toml_config. get_lockfile_spec ( ) ) ;
187
+ let prefix = prefix. unwrap_or_default ( ) ;
188
+ if lockfile. is_empty ( ) && prefix. is_empty ( ) {
189
+ return Err ( anyhow:: anyhow!( "No lockfiles or conda prefixes provided" ) ) ;
190
+ }
191
+
192
+ let platform = if platform. is_some ( ) {
193
+ platform
194
+ } else {
195
+ toml_config. get_platform_spec ( )
196
+ } ;
197
+ if platform. is_some ( ) && !prefix. is_empty ( ) {
198
+ return Err ( anyhow:: anyhow!(
199
+ "Cannot specify platforms and conda prefixes at the same time"
200
+ ) ) ;
201
+ }
202
+
203
+ let environment = if environment. is_some ( ) {
204
+ environment
205
+ } else {
206
+ toml_config. get_environment_spec ( )
207
+ } ;
208
+ if environment. is_some ( ) && !prefix. is_empty ( ) {
209
+ return Err ( anyhow:: anyhow!(
210
+ "Cannot specify environments and conda prefixes at the same time"
211
+ ) ) ;
212
+ }
213
+
214
+ CondaDenyConfig :: List ( CondaDenyListConfig {
215
+ prefix,
216
+ lockfile,
217
+ platform,
218
+ environment,
219
+ } )
220
+ }
221
+ } ;
222
+
223
+ Ok ( config)
224
+ }
225
+
226
+ pub fn check < W : Write > ( check_config : CondaDenyCheckConfig , mut out : W ) -> Result < ( ) > {
227
+ let ( safe_dependencies, unsafe_dependencies) = check_license_infos ( & check_config) ?;
228
+
229
+ writeln ! (
230
+ out,
231
+ "{}" ,
232
+ format_check_output(
233
+ safe_dependencies,
234
+ unsafe_dependencies. clone( ) ,
235
+ check_config. include_safe,
236
+ )
237
+ ) ?;
238
+
239
+ if !unsafe_dependencies. is_empty ( ) {
240
+ std:: process:: exit ( 1 ) ;
241
+ } ;
242
+ Ok ( ( ) )
243
+ }
244
+
245
+ pub fn list < W : Write > ( config : & CondaDenyListConfig , mut out : W ) -> Result < ( ) > {
65
246
panic ! ( "TODO" ) ;
66
247
// let license_infos =
67
248
// fetch_license_infos(config).with_context(|| "Fetching license information failed.")?;
0 commit comments