1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use async_trait::async_trait;
use aws_sdk_eks as eks;
use clap::{Args, Subcommand};

use config::Config;
use error::RawsError;

mod addon;
mod cluster;
mod update;

type EksResult<T = Box<dyn show::Show>> = std::result::Result<T, eks::Error>;

#[async_trait]
pub trait Execute {
    async fn execute(self: Box<Self>, config: &Config) -> EksResult;
}

trait ClientExt {
    fn client(&self) -> eks::Client;
}

impl ClientExt for Config {
    fn client(&self) -> eks::Client {
        eks::Client::new(self.config())
    }
}

/// Amazon Elastic Kubernetes Service (Amazon EKS)
#[derive(Debug, Subcommand)]
pub enum Eks {
    CreateCluster(cluster::CreateCluster),
    DeleteCluster(cluster::DeleteCluster),
    ListClusters(cluster::ListClusters),
    DescribeCluster(cluster::DescribeCluster),
    ListUpdates(update::ListUpdates),
    DescribeUpdate(update::DescribeUpdate),
    ListAddons(addon::ListAddons),
    DescribeAddon(addon::DescribeAddon),
}

impl Eks {
    fn boxed(self) -> Box<dyn Execute> {
        match self {
            Self::CreateCluster(create_cluster) => Box::new(create_cluster),
            Self::DeleteCluster(delete_cluster) => Box::new(delete_cluster),
            Self::ListClusters(list_cluster) => Box::new(list_cluster),
            Self::DescribeCluster(describe_cluster) => Box::new(describe_cluster),
            Self::ListUpdates(list_updates) => Box::new(list_updates),
            Self::DescribeUpdate(describe_update) => Box::new(describe_update),
            Self::ListAddons(list_addons) => Box::new(list_addons),
            Self::DescribeAddon(describe_addon) => Box::new(describe_addon),
        }
    }

    pub async fn dispatch(self, config: Config) -> Result<(), RawsError<eks::Error>> {
        self.boxed()
            .execute(&config)
            .await
            .map(|output| config.show(output))?;
        Ok(())
    }
}