|
| 1 | +""" |
| 2 | +Generates the services and commands for selection in SAM CLI generate-event |
| 3 | +""" |
| 4 | + |
| 5 | +import functools |
| 6 | +import click |
| 7 | + |
| 8 | +from samcli.cli.options import debug_option |
| 9 | +import samcli.commands.local.lib.generated_sample_events.events as events |
| 10 | + |
| 11 | + |
| 12 | +class ServiceCommand(click.MultiCommand): |
| 13 | + """ |
| 14 | + Top level command that defines the service provided |
| 15 | +
|
| 16 | + Methods |
| 17 | + ---------------- |
| 18 | + get_command(self, ctx, cmd_name): |
| 19 | + Get the subcommand(s) under a given service name. |
| 20 | + list_commands(self, ctx): |
| 21 | + List all of the subcommands |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__(self, events_lib, *args, **kwargs): |
| 25 | + """ |
| 26 | + Constructor for the ServiceCommand class |
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + events_lib: samcli.commands.local.lib.generated_sample_events.events |
| 31 | + The events library that allows for CLI population and substitution |
| 32 | + args: list |
| 33 | + any arguments passed in before kwargs |
| 34 | + kwargs: dict |
| 35 | + dictionary containing the keys/values used to construct the ServiceCommand |
| 36 | + """ |
| 37 | + |
| 38 | + super(ServiceCommand, self).__init__(*args, **kwargs) |
| 39 | + if not events_lib: |
| 40 | + raise ValueError("Events library is necessary to run this command") |
| 41 | + |
| 42 | + self.events_lib = events_lib |
| 43 | + self.all_cmds = self.events_lib.event_mapping |
| 44 | + |
| 45 | + def get_command(self, ctx, cmd_name): |
| 46 | + """ |
| 47 | + gets the subcommands under the service name |
| 48 | +
|
| 49 | + Parameters |
| 50 | + ---------- |
| 51 | + ctx : Context |
| 52 | + the context object passed into the method |
| 53 | + cmd_name : str |
| 54 | + the service name |
| 55 | + Returns |
| 56 | + ------- |
| 57 | + EventTypeSubCommand: |
| 58 | + returns subcommand if successful, None if not. |
| 59 | + """ |
| 60 | + |
| 61 | + if cmd_name not in self.all_cmds: |
| 62 | + return None |
| 63 | + return EventTypeSubCommand(self.events_lib, cmd_name, self.all_cmds[cmd_name]) |
| 64 | + |
| 65 | + def list_commands(self, ctx): |
| 66 | + """ |
| 67 | + lists the service commands available |
| 68 | +
|
| 69 | + Parameters |
| 70 | + ---------- |
| 71 | + ctx: Context |
| 72 | + the context object passed into the method |
| 73 | + Returns |
| 74 | + ------- |
| 75 | + list |
| 76 | + returns sorted list of the service commands available |
| 77 | + """ |
| 78 | + |
| 79 | + return sorted(self.all_cmds.keys()) |
| 80 | + |
| 81 | + |
| 82 | +class EventTypeSubCommand(click.MultiCommand): |
| 83 | + """ |
| 84 | + Class that describes the commands underneath a given service type |
| 85 | +
|
| 86 | + Methods |
| 87 | + ---------------- |
| 88 | + get_command(self, ctx, cmd_name): |
| 89 | + Get the subcommand(s) under a given service name. |
| 90 | + list_commands(self, ctx): |
| 91 | + List all of the subcommands |
| 92 | + """ |
| 93 | + |
| 94 | + TAGS = 'tags' |
| 95 | + |
| 96 | + def __init__(self, events_lib, top_level_cmd_name, subcmd_definition, *args, **kwargs): |
| 97 | + """ |
| 98 | + constructor for the EventTypeSubCommand class |
| 99 | +
|
| 100 | + Parameters |
| 101 | + ---------- |
| 102 | + events_lib: samcli.commands.local.lib.generated_sample_events.events |
| 103 | + The events library that allows for CLI population and substitution |
| 104 | + top_level_cmd_name: string |
| 105 | + the service name |
| 106 | + subcmd_definition: dict |
| 107 | + the subcommands and their values underneath the service command |
| 108 | + args: tuple |
| 109 | + any arguments passed in before kwargs |
| 110 | + kwargs: dict |
| 111 | + key/value pairs passed into the constructor |
| 112 | + """ |
| 113 | + |
| 114 | + super(EventTypeSubCommand, self).__init__(*args, **kwargs) |
| 115 | + self.top_level_cmd_name = top_level_cmd_name |
| 116 | + self.subcmd_definition = subcmd_definition |
| 117 | + self.events_lib = events_lib |
| 118 | + |
| 119 | + def get_command(self, ctx, cmd_name): |
| 120 | + |
| 121 | + """ |
| 122 | + gets the Click Commands underneath a service name |
| 123 | +
|
| 124 | + Parameters |
| 125 | + ---------- |
| 126 | + ctx: Context |
| 127 | + context object passed in |
| 128 | + cmd_name: string |
| 129 | + the service name |
| 130 | + Returns |
| 131 | + ------- |
| 132 | + cmd: Click.Command |
| 133 | + the Click Commands that can be called from the CLI |
| 134 | + """ |
| 135 | + |
| 136 | + if cmd_name not in self.subcmd_definition: |
| 137 | + return None |
| 138 | + parameters = [] |
| 139 | + for param_name in self.subcmd_definition[cmd_name][self.TAGS].keys(): |
| 140 | + default = self.subcmd_definition[cmd_name][self.TAGS][param_name]["default"] |
| 141 | + parameters.append(click.Option( |
| 142 | + ["--{}".format(param_name)], |
| 143 | + default=default, |
| 144 | + help="Specify the {} name you'd like, otherwise the default = {}".format(param_name, default) |
| 145 | + )) |
| 146 | + |
| 147 | + command_callback = functools.partial(self.cmd_implementation, |
| 148 | + self.events_lib, |
| 149 | + self.top_level_cmd_name, |
| 150 | + cmd_name) |
| 151 | + cmd = click.Command(name=cmd_name, |
| 152 | + help=self.subcmd_definition[cmd_name]["help"], |
| 153 | + params=parameters, |
| 154 | + callback=command_callback) |
| 155 | + |
| 156 | + cmd = debug_option(cmd) |
| 157 | + return cmd |
| 158 | + |
| 159 | + def list_commands(self, ctx): |
| 160 | + """ |
| 161 | + lists the commands underneath a particular event |
| 162 | +
|
| 163 | + Parameters |
| 164 | + ---------- |
| 165 | + ctx: Context |
| 166 | + the context object passed in |
| 167 | + Returns |
| 168 | + ------- |
| 169 | + the sorted list of commands under a service |
| 170 | + """ |
| 171 | + return sorted(self.subcmd_definition.keys()) |
| 172 | + |
| 173 | + def cmd_implementation(self, events_lib, top_level_cmd_name, subcmd_name, *args, **kwargs): |
| 174 | + """ |
| 175 | + calls for value substitution in the event json and returns the |
| 176 | + customized json as a string |
| 177 | +
|
| 178 | + Parameters |
| 179 | + ---------- |
| 180 | + events_lib |
| 181 | + top_level_cmd_name: string |
| 182 | + the name of the service |
| 183 | + subcmd_name: string |
| 184 | + the name of the event under the service |
| 185 | + args: tuple |
| 186 | + any arguments passed in before kwargs |
| 187 | + kwargs: dict |
| 188 | + the keys and values for substitution in the json |
| 189 | + Returns |
| 190 | + ------- |
| 191 | + event: string |
| 192 | + returns the customized event json as a string |
| 193 | + """ |
| 194 | + event = events_lib.generate_event(top_level_cmd_name, subcmd_name, kwargs) |
| 195 | + click.echo(event) |
| 196 | + return event |
| 197 | + |
| 198 | + |
| 199 | +class GenerateEventCommand(ServiceCommand): |
| 200 | + """ |
| 201 | + Class that brings ServiceCommand and EventTypeSubCommand into one for easy execution |
| 202 | + """ |
| 203 | + |
| 204 | + def __init__(self, *args, **kwargs): |
| 205 | + """ |
| 206 | + Constructor for GenerateEventCommand class that brings together |
| 207 | + ServiceCommand and EventTypeSubCommand into one class |
| 208 | +
|
| 209 | + Parameters |
| 210 | + ---------- |
| 211 | + args: tuple |
| 212 | + any arguments passed in before kwargs |
| 213 | + kwargs: dict |
| 214 | + commands, subcommands, and parameters for generate-event |
| 215 | + """ |
| 216 | + super(GenerateEventCommand, self).__init__(events.Events(), *args, **kwargs) |
0 commit comments