def run(ctx, step, commit, environment, watch, adhoc, args): """ Start an execution of a step. """ if step == '--help': # This is slightly weird, but it's because of the nested command thing click.echo(ctx.get_help(), color=ctx.color) ctx.exit() project = get_project(require=True) if adhoc: commit = create_adhoc_commit(project)['identifier'] config = project.get_config() step = match_prefix(config.steps, step) if not step: raise BadParameter( '{step} is not a known step (try one of {steps})'.format( step=step, steps=', '.join( click.style(t, bold=True) for t in sorted(config.steps)))) step = config.steps[step] rc = RunCommand(project, step, commit=commit, environment=environment, watch=watch) with rc.make_context(rc.name, list(args), parent=ctx) as ctx: return rc.invoke(ctx)
def match_pipeline(config: Config, pipeline_name: str) -> str: """ Take a pipeline name and try and match it to the configs pipelines. Returns the match if there is only one option. """ if pipeline_name in config.pipelines: return pipeline_name matching_pipelines = match_prefix(config.pipelines, pipeline_name, return_unique=False) if not matching_pipelines: raise click.BadParameter( '"{pipeline}" is not a known pipeline (try one of {pipelines})'. format(pipeline=pipeline_name, pipelines=', '.join( click.style(t, bold=True) for t in sorted(config.pipelines))), param_hint='pipeline') if len(matching_pipelines) > 1: raise click.BadParameter( '"{pipeline}" is ambiguous.\nIt matches {matches}.\nKnown pipelines are {pipelines}.' .format( pipeline=pipeline_name, matches=', '.join( click.style(t, bold=True) for t in sorted(matching_pipelines)), pipelines=', '.join( click.style(t, bold=True) for t in sorted(config.pipelines)), ), param_hint='pipeline') return str(matching_pipelines[0])
def get_command(self, ctx: Context, name: str) -> Optional[Union[Command, 'PluginCLI']]: # Dashes aren't valid in Python identifiers, so let's just replace them here. name = name.replace('-', '_') command_map: Dict[str, str] = self.command_to_canonical_map if name in command_map: return self._get_command(command_map[name]) matches = match_prefix(command_map.keys(), name, return_unique=False) if matches is None: matches = [] if len(matches) == 1: match = command_map[matches[0]] return self._get_command(match) if ' ' not in name: cmd = self._try_suffix_match(ctx, name) if cmd: return cmd if len(matches) > 1: ctx.fail('"{name}" matches {matches}; be more specific?'.format( name=name, matches=', '.join( click.style(match, bold=True) for match in sorted(matches)))) return None
def get_command(self, ctx, name): command_map = self.command_to_canonical_map if name in command_map: return self._get_command(command_map[name]) matches = match_prefix(command_map.keys(), name, return_unique=False) if not matches: return None if len(matches) > 1: ctx.fail('"{name}" matches {matches}; be more specific?'.format( name=name, matches=', '.join( click.style(match, bold=True) for match in sorted(matches)))) return return self._get_command(command_map[matches[0]])
def match_step(config, step): if step in config.steps: return step step_matches = match_prefix(config.steps, step, return_unique=False) if not step_matches: raise BadParameter( '"{step}" is not a known step (try one of {steps})'.format( step=step, steps=', '.join( click.style(t, bold=True) for t in sorted(config.steps))), param_hint='step') if len(step_matches) > 1: raise BadParameter( '"{step}" is ambiguous.\nIt matches {matches}.\nKnown steps are {steps}.' .format( step=step, matches=', '.join( click.style(t, bold=True) for t in sorted(step_matches)), steps=', '.join( click.style(t, bold=True) for t in sorted(config.steps)), ), param_hint='step') return step_matches[0]
def test_match_prefix(): assert match_prefix(['FOO', 'BAR'], 'foo') == 'FOO' assert match_prefix(['FOO', 'FEE'], 'f') is None assert match_prefix(['FOO', 'FEE'], 'f', return_unique=False) == ['FOO', 'FEE']