@@ -5,8 +5,12 @@ mod env;
55mod statik;
66mod vault;
77
8+ use std:: path:: PathBuf ;
9+
810pub use azure_key_vault:: * ;
911pub use env:: * ;
12+ use spin_common:: { env:: env_key, ui:: quoted_path} ;
13+ use spin_locked_app:: Variable ;
1014pub use statik:: * ;
1115pub use vault:: * ;
1216
@@ -38,6 +42,24 @@ pub fn runtime_config_from_toml(table: &impl GetTomlValue) -> anyhow::Result<Run
3842 Ok ( RuntimeConfig { providers } )
3943}
4044
45+ pub fn variable_provider_config_from_toml (
46+ table : & impl GetTomlValue ,
47+ ) -> anyhow:: Result < Vec < VariableProviderConfiguration > > {
48+ if let Some ( array) = table
49+ . get ( "variables_provider" )
50+ . or_else ( || table. get ( "config_provider" ) )
51+ {
52+ array
53+ . clone ( )
54+ . try_into :: < Vec < VariableProviderConfiguration > > ( )
55+ . map_err ( |e| anyhow:: anyhow!( "Failed to parse variable provider configuration: {}" , e) )
56+ } else {
57+ Ok ( vec ! [ VariableProviderConfiguration :: Env (
58+ EnvVariablesConfig :: default ( ) ,
59+ ) ] )
60+ }
61+ }
62+
4163/// A runtime configuration used in the Spin CLI for one type of variable provider.
4264#[ derive( Debug , Deserialize ) ]
4365#[ serde( rename_all = "snake_case" , tag = "type" ) ]
@@ -70,3 +92,38 @@ impl VariableProviderConfiguration {
7092 Ok ( provider)
7193 }
7294}
95+
96+ pub trait VariableSourcer {
97+ fn variable_env_checker (
98+ & self ,
99+ key : String ,
100+ val : Variable ,
101+ ) -> anyhow:: Result < ( String , Variable ) > ;
102+
103+ fn check (
104+ & self ,
105+ key : String ,
106+ mut val : Variable ,
107+ dotenv_path : Option < PathBuf > ,
108+ prefix : Option < String > ,
109+ ) -> anyhow:: Result < ( String , Variable ) > {
110+ if val. default . is_some ( ) {
111+ return Ok ( ( key, val) ) ;
112+ }
113+
114+ if let Some ( path) = dotenv_path {
115+ _ = std:: env:: set_current_dir ( path) ;
116+ }
117+
118+ match std:: env:: var ( env_key ( prefix, & key) ) {
119+ Ok ( v) => {
120+ val. default = Some ( v) ;
121+ Ok ( ( key, val) )
122+ }
123+ Err ( _) => Err ( anyhow:: anyhow!(
124+ "Variable data not provided for {}" ,
125+ quoted_path( key)
126+ ) ) ,
127+ }
128+ }
129+ }
0 commit comments