def main(args, file=sys.stdout):  # pylint: disable=redefined-builtin
    azlogging.configure_logging(args)
    logger.debug('Command arguments %s', args)

    if len(args) > 0 and args[0] == '--version':
        show_version_info_exit(file)

    azure_folder = get_config_dir()
    if not os.path.exists(azure_folder):
        os.makedirs(azure_folder)
    ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json'))
    CONFIG.load(os.path.join(azure_folder, 'az.json'))
    SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600)

    APPLICATION.initialize(Configuration())

    try:
        cmd_result = APPLICATION.execute(args)

        # Commands can return a dictionary/list of results
        # If they do, we print the results.
        if cmd_result and cmd_result.result is not None:
            from azure.cli.core._output import OutputProducer
            formatter = OutputProducer.get_formatter(APPLICATION.configuration.output_format)
            OutputProducer(formatter=formatter, file=file).out(cmd_result)

    except Exception as ex:  # pylint: disable=broad-except

        # TODO: include additional details of the exception in telemetry
        telemetry.set_exception(ex, 'outer-exception',
                                'Unexpected exception caught during application execution.')
        telemetry.set_failure()

        error_code = handle_exception(ex)
        return error_code
Beispiel #2
0
    def _special_cases(self, cmd, outside):
        break_flag = False
        continue_flag = False
        args = parse_quotes(cmd)
        cmd_stripped = cmd.strip()
        if cmd_stripped and cmd.split(' ', 1)[0].lower() == 'az':
            telemetry.track_ssg('az', cmd)
            cmd = ' '.join(cmd.split()[1:])
        if self.default_command:
            cmd = self.default_command + " " + cmd

        if cmd_stripped == "quit" or cmd_stripped == "exit":
            break_flag = True
        elif cmd_stripped == "clear-history":
            # clears the history, but only when you restart
            outside = True
            cmd = 'echo -n "" >' +\
                os.path.join(
                    SHELL_CONFIG_DIR(),
                    SHELL_CONFIGURATION.get_history())
        elif cmd_stripped == CLEAR_WORD:
            outside = True
            cmd = CLEAR_WORD
        if cmd_stripped:
            if cmd_stripped[0] == SELECT_SYMBOL['outside']:
                cmd = cmd_stripped[1:]
                outside = True
                if cmd.strip() and cmd.split()[0] == 'cd':
                    self.handle_cd(parse_quotes(cmd))
                    continue_flag = True
                telemetry.track_ssg('outside', '')

            elif cmd_stripped[0] == SELECT_SYMBOL['exit_code']:
                meaning = "Success" if self.last_exit == 0 else "Failure"

                print(meaning + ": " + str(self.last_exit), file=self.output)
                continue_flag = True
                telemetry.track_ssg('exit code', '')
            elif SELECT_SYMBOL['query'] in cmd_stripped and self.last and self.last.result:
                continue_flag = self.handle_jmespath_query(args)
                telemetry.track_ssg('query', '')

            elif args[0] == '--version' or args[0] == '-v':
                try:
                    continue_flag = True
                    show_version_info_exit(self.output)
                except SystemExit:
                    pass
            elif "|" in cmd or ">" in cmd:
                # anything I don't parse, send off
                outside = True
                cmd = "az " + cmd

            elif SELECT_SYMBOL['example'] in cmd:
                cmd, continue_flag = self.handle_example(cmd, continue_flag)
                telemetry.track_ssg('tutorial', cmd)
            elif len(cmd_stripped) > 2 and SELECT_SYMBOL['scope'] == cmd_stripped[0:2]:
                continue_flag, cmd = self.handle_scoping_input(continue_flag, cmd, cmd_stripped)

        return break_flag, continue_flag, outside, cmd
Beispiel #3
0
    def _special_cases(self, text, cmd, outside):
        break_flag = False
        continue_flag = False

        if text and len(text.split()) > 0 and text.split()[0].lower() == 'az':
            telemetry.track_ssg('az', text)
            cmd = ' '.join(text.split()[1:])
        if self.default_command:
            cmd = self.default_command + " " + cmd

        if text.strip() == "quit" or text.strip() == "exit":
            break_flag = True
        elif text.strip(
        ) == "clear-history":  # clears the history, but only when you restart
            outside = True
            cmd = 'echo -n "" >' +\
                os.path.join(
                    SHELL_CONFIG_DIR(),
                    SHELL_CONFIGURATION.get_history())
        elif text.strip() == CLEAR_WORD:
            outside = True
            cmd = CLEAR_WORD
        if '--version' in text:
            try:
                continue_flag = True
                show_version_info_exit(sys.stdout)
            except SystemExit:
                pass
        if text:
            if text[0] == SELECT_SYMBOL['outside']:
                cmd = text[1:]
                outside = True
                if cmd.strip() and cmd.split()[0] == 'cd':
                    handle_cd(parse_quotes(cmd))
                    continue_flag = True
                telemetry.track_ssg('outside', '')

            elif text[0] == SELECT_SYMBOL['exit_code']:
                meaning = "Success" if self.last_exit == 0 else "Failure"

                print(meaning + ": " + str(self.last_exit))
                continue_flag = True
                telemetry.track_ssg('exit code', '')

            elif text[0] == SELECT_SYMBOL['query']:  # query previous output
                continue_flag = self.handle_jmespath_query(text, continue_flag)

            elif "|" in text or ">" in text:  # anything I don't parse, send off
                outside = True
                cmd = "az " + cmd

            elif SELECT_SYMBOL['example'] in text:
                cmd, continue_flag = self.handle_example(cmd, continue_flag)
                telemetry.track_ssg('tutorial', text)

        continue_flag, cmd = self.handle_scoping_input(continue_flag, cmd,
                                                       text)

        return break_flag, continue_flag, outside, cmd
Beispiel #4
0
def main(args, output=sys.stdout, logging_stream=None):
    configure_logging(args, logging_stream)

    logger = get_az_logger(__name__)
    logger.debug('Command arguments %s', args)

    if args and (args[0] == '--version' or args[0] == '-v'):
        show_version_info_exit(output)

    azure_folder = get_config_dir()
    if not os.path.exists(azure_folder):
        os.makedirs(azure_folder)
    ACCOUNT.load(os.path.join(azure_folder, 'azureProfile.json'))
    CONFIG.load(os.path.join(azure_folder, 'az.json'))
    SESSION.load(os.path.join(azure_folder, 'az.sess'), max_age=3600)

    APPLICATION.initialize(Configuration())

    try:
        cmd_result = APPLICATION.execute(args)

        # Commands can return a dictionary/list of results
        # If they do, we print the results.
        if cmd_result and cmd_result.result is not None:
            from azure.cli.core._output import OutputProducer
            formatter = OutputProducer.get_formatter(
                APPLICATION.configuration.output_format)
            OutputProducer(formatter=formatter, file=output).out(cmd_result)

    except Exception as ex:  # pylint: disable=broad-except

        # TODO: include additional details of the exception in telemetry
        telemetry.set_exception(
            ex, 'outer-exception',
            'Unexpected exception caught during application execution.')
        telemetry.set_failure()

        error_code = handle_exception(ex)
        return error_code
Beispiel #5
0
    def _special_cases(self, text, cmd, outside):
        break_flag = False
        continue_flag = False
        args = parse_quotes(text)
        args_no_quotes = []
        text_stripped = text.strip()
        for arg in args:
            args_no_quotes.append(arg.strip("/'").strip('/"'))

        if text and len(text.split()) > 0 and text.split()[0].lower() == 'az':
            telemetry.track_ssg('az', text)
            cmd = ' '.join(text.split()[1:])
        if self.default_command:
            cmd = self.default_command + " " + cmd

        if text_stripped == "quit" or text_stripped == "exit":
            break_flag = True
        elif text_stripped == "clear-history":  # clears the history, but only when you restart
            outside = True
            cmd = 'echo -n "" >' +\
                os.path.join(
                    SHELL_CONFIG_DIR(),
                    SHELL_CONFIGURATION.get_history())
        elif text_stripped == CLEAR_WORD:
            outside = True
            cmd = CLEAR_WORD
        if text_stripped:
            if text_stripped[0] == SELECT_SYMBOL['outside']:
                cmd = text_stripped[1:]
                outside = True
                if cmd.strip() and cmd.split()[0] == 'cd':
                    self.handle_cd(parse_quotes(cmd))
                    continue_flag = True
                telemetry.track_ssg('outside', '')

            elif text_stripped[0] == SELECT_SYMBOL['exit_code']:
                meaning = "Success" if self.last_exit == 0 else "Failure"

                print(meaning + ": " + str(self.last_exit), file=self.output)
                continue_flag = True
                telemetry.track_ssg('exit code', '')
            elif validate_contains_query(
                    args_no_quotes,
                    SELECT_SYMBOL['query']) and self.last and self.last.result:
                continue_flag = self.handle_jmespath_query(
                    args_no_quotes, continue_flag)
                telemetry.track_ssg('query', '')

            elif args[0] == '--version' or args[0] == '-v':
                try:
                    continue_flag = True
                    show_version_info_exit(self.output)
                except SystemExit:
                    pass
            elif "|" in text or ">" in text:  # anything I don't parse, send off
                outside = True
                cmd = "az " + cmd

            elif SELECT_SYMBOL['example'] in text:
                cmd, continue_flag = self.handle_example(cmd, continue_flag)
                telemetry.track_ssg('tutorial', text)
            elif len(text_stripped
                     ) > 2 and SELECT_SYMBOL['scope'] == text_stripped[0:2]:
                continue_flag, cmd = self.handle_scoping_input(
                    continue_flag, cmd, text_stripped)

        return break_flag, continue_flag, outside, cmd