@@ -5,11 +5,7 @@ use args::{Args, SubCommand};
5
5
use atty:: Stream ;
6
6
use clap:: { CommandFactory , Parser } ;
7
7
use clap_complete:: generate;
8
- use dsc_lib:: configure:: config_doc:: Configuration ;
9
- use crate :: include:: Include ;
10
8
use std:: io:: { self , Read } ;
11
- use std:: fs:: File ;
12
- use std:: path:: Path ;
13
9
use std:: process:: exit;
14
10
use sysinfo:: { Process , ProcessExt , RefreshKind , System , SystemExt , get_current_pid, ProcessRefreshKind } ;
15
11
use tracing:: { error, info, warn, debug} ;
@@ -20,7 +16,7 @@ use crossterm::event;
20
16
use std:: env;
21
17
22
18
pub mod args;
23
- pub mod include ;
19
+ pub mod resolve ;
24
20
pub mod resource_command;
25
21
pub mod subcommand;
26
22
pub mod tablewriter;
@@ -40,7 +36,7 @@ fn main() {
40
36
41
37
debug ! ( "Running dsc {}" , env!( "CARGO_PKG_VERSION" ) ) ;
42
38
43
- let mut input = if atty:: is ( Stream :: Stdin ) {
39
+ let input = if atty:: is ( Stream :: Stdin ) {
44
40
None
45
41
} else {
46
42
info ! ( "Reading input from STDIN" ) ;
@@ -70,23 +66,19 @@ fn main() {
70
66
let mut cmd = Args :: command ( ) ;
71
67
generate ( shell, & mut cmd, "dsc" , & mut io:: stdout ( ) ) ;
72
68
} ,
73
- SubCommand :: Config { subcommand, parameters, parameters_file, as_group, as_include} => {
74
- if as_include {
75
- input = Some ( read_include_file ( & input) ) ;
76
- }
77
-
69
+ SubCommand :: Config { subcommand, parameters, parameters_file, as_group} => {
78
70
if let Some ( file_name) = parameters_file {
79
- info ! ( "Reading parameters from file {}" , file_name ) ;
80
- match std:: fs:: read_to_string ( file_name) {
81
- Ok ( parameters) => subcommand:: config ( & subcommand, & Some ( parameters) , & input, & as_group, & as_include ) ,
71
+ info ! ( "Reading parameters from file {file_name}" ) ;
72
+ match std:: fs:: read_to_string ( & file_name) {
73
+ Ok ( parameters) => subcommand:: config ( & subcommand, & Some ( parameters) , & input, & as_group) ,
82
74
Err ( err) => {
83
- error ! ( "Error: Failed to read parameters file: {err}" ) ;
75
+ error ! ( "Error: Failed to read parameters file '{file_name}' : {err}" ) ;
84
76
exit ( util:: EXIT_INVALID_INPUT ) ;
85
77
}
86
78
}
87
79
}
88
80
else {
89
- subcommand:: config ( & subcommand, & parameters, & input, & as_group, & as_include ) ;
81
+ subcommand:: config ( & subcommand, & parameters, & input, & as_group) ;
90
82
}
91
83
} ,
92
84
SubCommand :: Resource { subcommand } => {
@@ -108,96 +100,6 @@ fn main() {
108
100
exit ( util:: EXIT_SUCCESS ) ;
109
101
}
110
102
111
- fn read_include_file ( input : & Option < String > ) -> String {
112
- let Some ( include) = input else {
113
- error ! ( "Error: Include requires input from STDIN" ) ;
114
- exit ( util:: EXIT_INVALID_INPUT ) ;
115
- } ;
116
-
117
- // deserialize the Include input
118
- let include: Include = match serde_json:: from_str ( include) {
119
- Ok ( include) => include,
120
- Err ( err) => {
121
- error ! ( "Error: Failed to deserialize Include input: {err}" ) ;
122
- exit ( util:: EXIT_INVALID_INPUT ) ;
123
- }
124
- } ;
125
-
126
- let path = Path :: new ( & include. configuration_file ) ;
127
- if path. is_absolute ( ) {
128
- error ! ( "Error: Include path must be relative: {}" , include. configuration_file) ;
129
- exit ( util:: EXIT_INVALID_INPUT ) ;
130
- }
131
-
132
- // check that no components of the path are '..'
133
- if path. components ( ) . any ( |c| c == std:: path:: Component :: ParentDir ) {
134
- error ! ( "Error: Include path must not contain '..': {}" , include. configuration_file) ;
135
- exit ( util:: EXIT_INVALID_INPUT ) ;
136
- }
137
-
138
- // use DSC_CONFIG_ROOT env var as current directory
139
- let current_directory = match std:: env:: var ( "DSC_CONFIG_ROOT" ) {
140
- Ok ( current_directory) => current_directory,
141
- Err ( err) => {
142
- error ! ( "Error: Could not read DSC_CONFIG_ROOT env var: {err}" ) ;
143
- exit ( util:: EXIT_INVALID_INPUT ) ;
144
- }
145
- } ;
146
-
147
- // combine the current directory with the Include path
148
- let include_path = Path :: new ( & current_directory) . join ( & include. configuration_file ) ;
149
-
150
- // read the file specified in the Include input
151
- let mut buffer: Vec < u8 > = Vec :: new ( ) ;
152
- match File :: open ( & include_path) {
153
- Ok ( mut file) => {
154
- match file. read_to_end ( & mut buffer) {
155
- Ok ( _) => ( ) ,
156
- Err ( err) => {
157
- error ! ( "Error: Failed to read file '{include_path:?}': {err}" ) ;
158
- exit ( util:: EXIT_INVALID_INPUT ) ;
159
- }
160
- }
161
- } ,
162
- Err ( err) => {
163
- error ! ( "Error: Failed to included file '{include_path:?}': {err}" ) ;
164
- exit ( util:: EXIT_INVALID_INPUT ) ;
165
- }
166
- }
167
- // convert the buffer to a string
168
- let include_content = match String :: from_utf8 ( buffer) {
169
- Ok ( input) => input,
170
- Err ( err) => {
171
- error ! ( "Error: Invalid UTF-8 sequence in included file '{include_path:?}': {err}" ) ;
172
- exit ( util:: EXIT_INVALID_INPUT ) ;
173
- }
174
- } ;
175
-
176
- // try to deserialize the Include content as YAML first
177
- let configuration: Configuration = match serde_yaml:: from_str ( & include_content) {
178
- Ok ( configuration) => configuration,
179
- Err ( _err) => {
180
- // if that fails, try to deserialize it as JSON
181
- match serde_json:: from_str ( & include_content) {
182
- Ok ( configuration) => configuration,
183
- Err ( err) => {
184
- error ! ( "Error: Failed to read the configuration file '{include_path:?}' as YAML or JSON: {err}" ) ;
185
- exit ( util:: EXIT_INVALID_INPUT ) ;
186
- }
187
- }
188
- }
189
- } ;
190
-
191
- // serialize the Configuration as JSON
192
- match serde_json:: to_string ( & configuration) {
193
- Ok ( json) => json,
194
- Err ( err) => {
195
- error ! ( "Error: JSON Error: {err}" ) ;
196
- exit ( util:: EXIT_JSON_ERROR ) ;
197
- }
198
- }
199
- }
200
-
201
103
fn ctrlc_handler ( ) {
202
104
warn ! ( "Ctrl-C received" ) ;
203
105
0 commit comments