raws_config/
lib.rs

1use aws_config::{BehaviorVersion, ConfigLoader, SdkConfig};
2use aws_types::app_name::AppName;
3use aws_types::region::Region;
4
5pub use output::Output;
6
7mod clients;
8mod output;
9
10#[derive(Debug)]
11pub struct Config {
12    debug: bool,
13    no_paginate: bool,
14    output: Output,
15    shared_config: SdkConfig,
16}
17
18impl Config {
19    pub async fn new(
20        debug: bool,
21        no_paginate: bool,
22        endpoint_url: Option<String>,
23        profile: Option<String>,
24        region: Option<String>,
25        output: Option<Output>,
26    ) -> Self {
27        let output = output.unwrap_or_default();
28        let app_name = AppName::new("raws").expect("valid app name");
29        let shared_config = aws_config::defaults(BehaviorVersion::latest())
30            .app_name(app_name)
31            .optionally_profile(profile)
32            .optionally_region(region)
33            .optionally_endpoint_url(endpoint_url)
34            .load()
35            .await;
36
37        Self {
38            debug,
39            no_paginate,
40            output,
41            shared_config,
42        }
43    }
44
45    pub fn config(&self) -> &SdkConfig {
46        &self.shared_config
47    }
48
49    pub fn no_paginate(&self) -> bool {
50        self.no_paginate
51    }
52
53    pub fn show(&self, object: Box<dyn show::Show>) {
54        let text = if self.debug {
55            object.debug()
56        } else {
57            self.output.output(object)
58        };
59        fmtools::println!({ text });
60    }
61}
62
63trait Optionally {
64    fn optionally_profile(self, profile: Option<String>) -> Self;
65    fn optionally_region(self, region: Option<String>) -> Self;
66    fn optionally_endpoint_url(self, endpoint_url: Option<String>) -> Self;
67}
68
69impl Optionally for ConfigLoader {
70    fn optionally_profile(self, profile: Option<String>) -> Self {
71        if let Some(profile) = profile {
72            self.profile_name(profile)
73        } else {
74            self
75        }
76    }
77
78    fn optionally_region(self, region: Option<String>) -> Self {
79        if let Some(region) = region.map(Region::new) {
80            self.region(region)
81        } else {
82            self
83        }
84    }
85
86    fn optionally_endpoint_url(self, endpoint_url: Option<String>) -> Self {
87        if let Some(endpoint_url) = endpoint_url {
88            self.endpoint_url(endpoint_url)
89        } else {
90            self
91        }
92    }
93}