@@ -8,14 +8,21 @@ use miette::Diagnostic;
88use serde:: Deserialize ;
99use serde_yml:: { Mapping , Value } ;
1010
11+ use crate :: git;
12+
13+ const DEFAULT_TAG_FORMAT : & str = "${gitTag:$gitShortCommit}${gitDirty:}" ;
14+
1115#[ derive( Debug , Deserialize , Clone ) ]
1216#[ serde( rename_all = "camelCase" ) ]
1317pub struct Config {
14- #[ serde( default ) ]
15- pub insecure_registries : Vec < String > ,
1618 pub build : HashMap < String , Build > ,
1719 #[ serde( default ) ]
1820 pub deploy : HashMap < String , Release > ,
21+ #[ serde( default ) ]
22+ pub insecure_registries : Vec < String > ,
23+ pub default_repo : Option < String > ,
24+ #[ serde( default ) ]
25+ pub tag_format : String ,
1926}
2027
2128#[ derive( Clone , Debug , Deserialize ) ]
@@ -91,6 +98,8 @@ pub enum ConfigError {
9198 Subst ( #[ from] subst:: Error ) ,
9299 #[ error( "failed to deserialize" ) ]
93100 Yaml ( #[ from] serde_yml:: Error ) ,
101+ #[ error( "failed to parse git status" ) ]
102+ Git ( #[ from] git:: GitError ) ,
94103 #[ error( "profile '{0}' does not exist" ) ]
95104 Profile ( String ) ,
96105}
@@ -116,25 +125,45 @@ fn template(vars: &HashMap<String, String>, config: Value) -> Result<Value, subs
116125 }
117126}
118127
128+ fn extract_git_vars ( state : git:: State ) -> HashMap < String , String > {
129+ let mut vars = HashMap :: new ( ) ;
130+
131+ vars. insert ( "gitShortCommit" . to_string ( ) , state. commit [ 0 ..6 ] . to_string ( ) ) ;
132+ vars. insert ( "gitCommit" . to_string ( ) , state. commit ) ;
133+ if let Some ( tag) = state. tag {
134+ vars. insert ( "gitTag" . to_string ( ) , tag) ;
135+ }
136+ if state. dirty {
137+ vars. insert ( "gitDirty" . to_string ( ) , "-dirty" . to_string ( ) ) ;
138+ }
139+
140+ vars
141+ }
142+
119143pub async fn load_from_path (
120144 profile : Option < & str > ,
121145 path : impl AsRef < Path > ,
122146) -> Result < Config , ConfigError > {
147+ let mut vars = extract_git_vars ( git:: state ( ) . await ?) ;
123148 let data = tokio:: fs:: read ( path) . await ?;
124149 let mut config = serde_yml:: from_slice :: < Value > ( & data) ?;
125150
126151 if let Some ( profile) = profile {
127- match config
128- . get_mut ( "profiles" )
129- . and_then ( |profiles| profiles. get_mut ( profile) )
130- {
131- Some ( profile) => {
132- let profile = serde_yml:: from_value :: < Profile > ( mem:: take ( profile) ) ?;
133- config = template ( & profile. vars , config) ?;
134- }
135- None => return Err ( ConfigError :: Profile ( profile. to_string ( ) ) ) ,
136- } ;
152+ let profile = serde_yml:: from_value :: < Profile > ( mem:: take (
153+ config
154+ . get_mut ( "profiles" )
155+ . and_then ( |profiles| profiles. get_mut ( profile) )
156+ . ok_or_else ( || ConfigError :: Profile ( profile. to_string ( ) ) ) ?,
157+ ) ) ?;
158+
159+ vars. extend ( profile. vars ) ;
160+ }
161+
162+ let mut config = serde_yml:: from_value :: < Config > ( template ( & vars, config) ?) ?;
163+
164+ if config. tag_format . is_empty ( ) {
165+ config. tag_format = subst:: substitute ( DEFAULT_TAG_FORMAT , & vars) ?;
137166 }
138167
139- Ok ( serde_yml :: from_value ( config) ? )
168+ Ok ( config)
140169}
0 commit comments