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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use clap::{Parser, Subcommand};
use std::fmt::Debug;
use strum::IntoEnumIterator;

use crate::contract::Deploy;

#[derive(Parser, Clone, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Cli<C, S = EmptySubcommand>
where
    C: Deploy + Clone,
    S: Subcommand + Clone + Debug,
{
    #[command(subcommand)]
    pub command: Commands<C, S>,

    /// Add additional args to cargo build
    #[arg(long, required = false)]
    pub cargo_args: Vec<String>,
}

#[derive(Parser, Clone, Debug)]
#[clap(rename_all = "snake_case", infer_subcommands = true)]
pub enum Commands<C, S>
where
    C: Deploy + Clone,
    S: Subcommand + Clone + Debug,
{
    /// Rebuilds deploy
    #[command(visible_alias = "u")]
    Update,

    /// Initializes deploy, adding keys, chains, and envs
    Init,

    /// Builds the contracts
    #[command(visible_alias = "b")]
    Build {
        /// Name of the contract
        #[arg(short, long, use_value_delimiter=true, value_delimiter=',', default_values=get_all::<C>())]
        contracts: Vec<C>,
    },

    /// Modify chains
    #[command(arg_required_else_help = true)]
    Chain {
        /// Triggers dialogue to add a chain
        #[arg(short, long, exclusive = true)]
        add: bool,

        /// Triggers dialogue to delete a chain
        #[arg(short, long, exclusive = true)]
        delete: bool,
    },

    /// Modify keys
    #[command(arg_required_else_help = true)]
    Key {
        /// Triggers dialogue to add a key
        #[arg(short, long, exclusive = true)]
        add: bool,

        /// Triggers dialogue to delete a key
        #[arg(short, long, exclusive = true)]
        delete: bool,
    },

    /// Modify contracts
    #[command(arg_required_else_help = true)]
    Contract {
        /// Triggers dialogue to add a contract
        #[arg(short, long, exclusive = true)]
        add: bool,

        /// Triggers dialogue to delete a contract
        #[arg(short, long, exclusive = true)]
        delete: bool,
    },

    /// Builds, optimizes, stores, instantiates and sets configs.
    #[command(visible_alias = "d")]
    Deploy {
        /// Name of the contract
        #[arg(short, long, use_value_delimiter=true, value_delimiter=',', default_values=get_all::<C>(), value_parser=C::from_str)]
        contracts: Vec<C>,

        /// Deploys but does not recompile first
        #[arg(short, long, required = false)]
        no_build: bool,
    },

    /// Modify deployment environments
    Env {
        /// Triggers dialogue to add a deployment environment
        #[arg(short, long, exclusive = true)]
        add: bool,

        /// Triggers dialogue to delete deployment environment
        #[arg(short, long, exclusive = true)]
        delete: bool,

        /// Triggers dialogue to select an env to activate
        #[arg(short, long, exclusive = true)]
        select: bool,

        /// Prints the current active env id
        #[arg(short, long, exclusive = true)]
        id: bool,
    },

    /// Generates and imports schemas
    Schema {
        /// Name of the contract
        #[arg(short, long, use_value_delimiter=true, value_delimiter=',', default_values=get_all::<C>())]
        contracts: Vec<C>,
    },

    /// Stores code for the contracts
    StoreCode {
        /// Name of the contract
        #[arg(short, long, use_value_delimiter=true, value_delimiter=',', default_values=get_all::<C>())]
        contracts: Vec<C>,
    },

    /// Instantiates a contract using the preprogrammed messages
    #[command(visible_alias = "i")]
    Instantiate {
        /// Name of the contract
        #[arg(short, long, use_value_delimiter=true, value_delimiter=',', default_values=get_all::<C>())]
        contracts: Vec<C>,

        /// Interactive mode
        #[arg(short, long, required = false)]
        interactive: bool,
    },

    /// Migrates contracts
    #[command(visible_alias = "m")]
    Migrate {
        /// Name of the contract
        #[arg(short, long, use_value_delimiter=true, value_delimiter=',', default_values=get_all::<C>())]
        contracts: Vec<C>,

        /// Interactive mode
        #[arg(short, long, required = false)]
        interactive: bool,
    },

    /// Sets the config of a contract
    SetConfig {
        /// Name of the contract
        #[arg(short, long, use_value_delimiter=true, value_delimiter=',', default_values=get_all::<C>())]
        contracts: Vec<C>,
    },

    /// Executes a contract
    #[command(visible_alias = "x")]
    Execute { contract: C },

    /// Sends Cw20 tokens to a contract along with a payload
    Cw20Send { contract: C },

    /// Executes a Cw20 message
    Cw20Execute {},

    /// Queries a Cw20 contract
    Cw20Query {},

    /// Instantiate a Cw20 contract
    Cw20Instantiate {},

    /// Executes a contract with a custom payload
    ExecutePayload {
        #[arg(short, long)]
        contract: C,

        #[arg(short, long)]
        payload: String,
    },

    /// Executes a user defined command
    #[command(flatten)]
    Custom(S),

    /// Sends a query to a contract
    #[command(visible_alias = "q")]
    Query { contract: C },

    /// Sets up the smart contract env with executes
    SetUp {
        /// Name of the contract
        #[arg(short, long, use_value_delimiter=true, value_delimiter=',', default_values=get_all::<C>())]
        contracts: Vec<C>,
    },
}

fn get_all<C: Deploy + IntoEnumIterator>() -> Vec<String> {
    C::iter().map(|x| x.to_string()).collect()
}

#[derive(Subcommand, Clone, Debug)]
pub enum EmptySubcommand {}