1
1
use std:: env;
2
+ use std:: fs:: File ;
3
+ use std:: io:: { BufRead , BufReader } ;
4
+ use std:: path:: PathBuf ;
5
+ use std:: str:: FromStr ;
6
+
7
+ use dirs:: home_dir;
2
8
3
9
use super :: { Context , Module , RootModuleConfig } ;
4
10
5
11
use crate :: configs:: aws:: AwsConfig ;
6
12
13
+ fn get_aws_region_from_config ( aws_profile : & Option < String > ) -> Option < String > {
14
+ let config_location = env:: var ( "AWS_CONFIG_FILE" )
15
+ . ok ( )
16
+ . and_then ( |path| PathBuf :: from_str ( & path) . ok ( ) )
17
+ . or_else ( || {
18
+ let mut home = home_dir ( ) ?;
19
+ home. push ( ".aws/config" ) ;
20
+ Some ( home)
21
+ } ) ?;
22
+
23
+ let file = File :: open ( & config_location) . ok ( ) ?;
24
+ let reader = BufReader :: new ( file) ;
25
+ let lines = reader. lines ( ) . filter_map ( Result :: ok) ;
26
+
27
+ let region_line = if let Some ( ref aws_profile) = aws_profile {
28
+ lines
29
+ . skip_while ( |line| line != & format ! ( "[profile {}]" , aws_profile) )
30
+ . skip ( 1 )
31
+ . take_while ( |line| !line. starts_with ( '[' ) )
32
+ . find ( |line| line. starts_with ( "region" ) )
33
+ } else {
34
+ lines
35
+ . skip_while ( |line| line != "[default]" )
36
+ . skip ( 1 )
37
+ . take_while ( |line| !line. starts_with ( '[' ) )
38
+ . find ( |line| line. starts_with ( "region" ) )
39
+ } ?;
40
+
41
+ let region = region_line. split ( '=' ) . nth ( 1 ) ?;
42
+ let region = region. trim ( ) ;
43
+
44
+ Some ( region. to_string ( ) )
45
+ }
46
+
47
+ fn get_aws_region ( ) -> Option < ( String , String ) > {
48
+ env:: var ( "AWS_DEFAULT_REGION" )
49
+ . ok ( )
50
+ . map ( |region| ( String :: new ( ) , region) )
51
+ . or_else ( || {
52
+ let aws_profile = env:: var ( "AWS_PROFILE" ) . ok ( ) ;
53
+ let aws_region = get_aws_region_from_config ( & aws_profile) ;
54
+
55
+ if aws_profile. is_none ( ) && aws_region. is_none ( ) {
56
+ None
57
+ } else {
58
+ Some ( (
59
+ aws_profile. unwrap_or_default ( ) ,
60
+ aws_region. unwrap_or_default ( ) ,
61
+ ) )
62
+ }
63
+ } )
64
+ . or_else ( || {
65
+ env:: var ( "AWS_REGION" )
66
+ . ok ( )
67
+ . map ( |region| ( String :: new ( ) , region) )
68
+ } )
69
+ }
70
+
7
71
pub fn module < ' a > ( context : & ' a Context ) -> Option < Module < ' a > > {
8
72
const AWS_PREFIX : & str = "on " ;
9
73
10
- let aws_profile = env :: var ( "AWS_PROFILE" ) . ok ( ) ?;
11
- if aws_profile. is_empty ( ) {
74
+ let ( aws_profile, aws_region ) = get_aws_region ( ) ?;
75
+ if aws_profile. is_empty ( ) && aws_region . is_empty ( ) {
12
76
return None ;
13
77
}
78
+ let aws_region = if aws_profile. is_empty ( ) || aws_region. is_empty ( ) {
79
+ aws_region
80
+ } else {
81
+ format ! ( "({})" , aws_region)
82
+ } ;
14
83
15
84
let mut module = context. new_module ( "aws" ) ;
16
85
let config: AwsConfig = AwsConfig :: try_load ( module. config ) ;
@@ -21,6 +90,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
21
90
22
91
module. create_segment ( "symbol" , & config. symbol ) ;
23
92
module. create_segment ( "profile" , & config. profile . with_value ( & aws_profile) ) ;
93
+ module. create_segment ( "region" , & config. profile . with_value ( & aws_region) ) ;
24
94
25
95
Some ( module)
26
96
}
0 commit comments