1
1
// Copyright (c) Microsoft Corporation.
2
2
// Licensed under the MIT License.
3
3
4
- use dsc_lib:: configure:: config_doc:: Configuration ;
5
4
use dsc_lib:: util:: parse_input_to_json;
6
5
use rust_i18n:: t;
7
6
use schemars:: JsonSchema ;
@@ -14,14 +13,30 @@ use tracing::{debug, info};
14
13
use crate :: util:: DSC_CONFIG_ROOT ;
15
14
16
15
#[ derive( Debug , Clone , PartialEq , Deserialize , Serialize , JsonSchema ) ]
17
- pub struct Include {
16
+ pub enum IncludeKind {
18
17
/// The path to the file to include. Path is relative to the file containing the include
19
18
/// and not allowed to reference parent directories. If a configuration document is used
20
19
/// instead of a file, then the path is relative to the current working directory.
21
20
#[ serde( rename = "configurationFile" ) ]
22
- pub configuration_file : String ,
21
+ ConfigurationFile ( String ) ,
22
+ #[ serde( rename = "configurationContent" ) ]
23
+ ConfigurationContent ( String ) ,
24
+ }
25
+
26
+ #[ derive( Debug , Clone , PartialEq , Deserialize , Serialize , JsonSchema ) ]
27
+ pub enum IncludeParametersKind {
23
28
#[ serde( rename = "parametersFile" ) ]
24
- pub parameters_file : Option < String > ,
29
+ ParametersFile ( String ) ,
30
+ #[ serde( rename = "parametersContent" ) ]
31
+ ParametersContent ( String ) ,
32
+ }
33
+
34
+ #[ derive( Debug , Clone , PartialEq , Deserialize , Serialize , JsonSchema ) ]
35
+ pub struct Include {
36
+ #[ serde( flatten) ]
37
+ pub configuration : IncludeKind ,
38
+ #[ serde( flatten) ]
39
+ pub parameters : Option < IncludeParametersKind > ,
25
40
}
26
41
27
42
/// Read the file specified in the Include input and return the content as a JSON string.
@@ -51,74 +66,83 @@ pub fn get_contents(input: &str) -> Result<(Option<String>, String), String> {
51
66
}
52
67
} ;
53
68
54
- let include_path = normalize_path ( Path :: new ( & include. configuration_file ) ) ?;
69
+ let config_json = match include. configuration {
70
+ IncludeKind :: ConfigurationFile ( file_path) => {
71
+ let include_path = normalize_path ( Path :: new ( & file_path) ) ?;
55
72
56
- // read the file specified in the Include input
57
- let mut buffer: Vec < u8 > = Vec :: new ( ) ;
58
- match File :: open ( & include_path) {
59
- Ok ( mut file) => {
60
- match file. read_to_end ( & mut buffer) {
61
- Ok ( _) => ( ) ,
73
+ // read the file specified in the Include input
74
+ let mut buffer: Vec < u8 > = Vec :: new ( ) ;
75
+ match File :: open ( & include_path) {
76
+ Ok ( mut file) => {
77
+ match file. read_to_end ( & mut buffer) {
78
+ Ok ( _) => ( ) ,
79
+ Err ( err) => {
80
+ return Err ( format ! ( "{} '{include_path:?}': {err}" , t!( "resolve.failedToReadFile" ) ) ) ;
81
+ }
82
+ }
83
+ } ,
62
84
Err ( err) => {
63
- return Err ( format ! ( "{} '{include_path:?}': {err}" , t!( "resolve.failedToReadFile " ) ) ) ;
85
+ return Err ( format ! ( "{} '{include_path:?}': {err}" , t!( "resolve.failedToOpenFile " ) ) ) ;
64
86
}
65
87
}
66
- } ,
67
- Err ( err) => {
68
- return Err ( format ! ( "{} '{include_path:?}': {err}" , t!( "resolve.failedToOpenFile" ) ) ) ;
69
- }
70
- }
71
- // convert the buffer to a string
72
- let include_content = match String :: from_utf8 ( buffer) {
73
- Ok ( input) => input,
74
- Err ( err) => {
75
- return Err ( format ! ( "{} '{include_path:?}': {err}" , t!( "resolve.invalidFileContent" ) ) ) ;
76
- }
77
- } ;
88
+ // convert the buffer to a string
89
+ let include_content = match String :: from_utf8 ( buffer) {
90
+ Ok ( input) => input,
91
+ Err ( err) => {
92
+ return Err ( format ! ( "{} '{include_path:?}': {err}" , t!( "resolve.invalidFileContent" ) ) ) ;
93
+ }
94
+ } ;
78
95
79
- // try to deserialize the Include content as YAML first
80
- let configuration: Configuration = match serde_yaml:: from_str ( & include_content) {
81
- Ok ( configuration) => configuration,
82
- Err ( _err) => {
83
- // if that fails, try to deserialize it as JSON
84
- match serde_json:: from_str ( & include_content) {
85
- Ok ( configuration) => configuration,
96
+ match parse_input_to_json ( & include_content) {
97
+ Ok ( json) => json,
86
98
Err ( err) => {
87
99
return Err ( format ! ( "{} '{include_path:?}': {err}" , t!( "resolve.invalidFile" ) ) ) ;
88
100
}
89
101
}
102
+ } ,
103
+ IncludeKind :: ConfigurationContent ( text) => {
104
+ match parse_input_to_json ( & text) {
105
+ Ok ( json) => json,
106
+ Err ( err) => {
107
+ return Err ( format ! ( "{}: {err}" , t!( "resolve.invalidFile" ) ) ) ;
108
+ }
109
+ }
90
110
}
91
111
} ;
92
112
93
- // serialize the Configuration as JSON
94
- let config_json = match serde_json:: to_string ( & configuration) {
95
- Ok ( json) => json,
96
- Err ( err) => {
97
- return Err ( format ! ( "JSON: {err}" ) ) ;
98
- }
99
- } ;
100
-
101
- let parameters = if let Some ( parameters_file) = include. parameters_file {
102
- // combine the path with DSC_CONFIG_ROOT
103
- let parameters_file = normalize_path ( Path :: new ( & parameters_file) ) ?;
104
- info ! ( "{} '{parameters_file:?}'" , t!( "resolve.resolvingParameters" ) ) ;
105
- match std:: fs:: read_to_string ( & parameters_file) {
106
- Ok ( parameters) => {
107
- let parameters_json = match parse_input_to_json ( & parameters) {
108
- Ok ( json) => json,
109
- Err ( err) => {
110
- return Err ( format ! ( "{} '{parameters_file:?}': {err}" , t!( "resolve.failedParseParametersFile" ) ) ) ;
111
- }
112
- } ;
113
- Some ( parameters_json)
114
- } ,
115
- Err ( err) => {
116
- return Err ( format ! ( "{} '{parameters_file:?}': {err}" , t!( "resolve.failedResolveParametersFile" ) ) ) ;
113
+ let parameters = match include. parameters {
114
+ Some ( IncludeParametersKind :: ParametersFile ( file_path) ) => {
115
+ // combine the path with DSC_CONFIG_ROOT
116
+ let parameters_file = normalize_path ( Path :: new ( & file_path) ) ?;
117
+ info ! ( "{} '{parameters_file:?}'" , t!( "resolve.resolvingParameters" ) ) ;
118
+ match std:: fs:: read_to_string ( & parameters_file) {
119
+ Ok ( parameters) => {
120
+ let parameters_json = match parse_input_to_json ( & parameters) {
121
+ Ok ( json) => json,
122
+ Err ( err) => {
123
+ return Err ( format ! ( "{} '{parameters_file:?}': {err}" , t!( "resolve.failedParseParametersFile" ) ) ) ;
124
+ }
125
+ } ;
126
+ Some ( parameters_json)
127
+ } ,
128
+ Err ( err) => {
129
+ return Err ( format ! ( "{} '{parameters_file:?}': {err}" , t!( "resolve.failedResolveParametersFile" ) ) ) ;
130
+ }
117
131
}
132
+ } ,
133
+ Some ( IncludeParametersKind :: ParametersContent ( text) ) => {
134
+ let parameters_json = match parse_input_to_json ( & text) {
135
+ Ok ( json) => json,
136
+ Err ( err) => {
137
+ return Err ( format ! ( "{}: {err}" , t!( "resolve.invalidParametersContent" ) ) ) ;
138
+ }
139
+ } ;
140
+ Some ( parameters_json)
141
+ } ,
142
+ None => {
143
+ debug ! ( "{}" , t!( "resolve.noParameters" ) ) ;
144
+ None
118
145
}
119
- } else {
120
- debug ! ( "{}" , t!( "resolve.noParametersFile" ) ) ;
121
- None
122
146
} ;
123
147
124
148
Ok ( ( parameters, config_json) )
0 commit comments