def handle_scoping_input(self, continue_flag, cmd, text): default_split = text.partition(SELECT_SYMBOL['scope'])[2].split() cmd = cmd.replace(SELECT_SYMBOL['scope'], '') if text and SELECT_SYMBOL['scope'] == text[0:2]: continue_flag = True if not default_split: self.default_command = "" set_scope("", add=False) print('unscoping all') return continue_flag, cmd while default_split: if not text: value = '' else: value = default_split[0] if self.default_command: tree_val = self.default_command + " " + value else: tree_val = value if in_tree(self.completer.command_tree, tree_val.strip()): self.set_scope(value) print("defaulting: " + value) cmd = cmd.replace(SELECT_SYMBOL['scope'], '') telemetry.track_ssg('scope command', value) elif SELECT_SYMBOL['unscope'] == default_split[0] and \ len(self.default_command.split()) > 0: value = self.default_command.split()[-1] self.default_command = ' ' + ' '.join( self.default_command.split()[:-1]) if not self.default_command.strip(): self.default_command = self.default_command.strip() set_scope(self.default_command, add=False) print('unscoping: ' + value) elif SELECT_SYMBOL['unscope'] not in text: print("Scope must be a valid command") default_split = default_split[1:] else: return continue_flag, cmd return continue_flag, cmd
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
def handle_jmespath_query(self, text, continue_flag): if self.last and self.last.result: if hasattr(self.last.result, '__dict__'): input_dict = dict(self.last.result) else: input_dict = self.last.result try: query_text = text.partition(SELECT_SYMBOL['query'])[2] result = "" if query_text: result = jmespath.search(query_text, input_dict) if isinstance(result, str): print(result) else: print(json.dumps(result, sort_keys=True, indent=2)) except jmespath.exceptions.ParseError: print("Invalid Query") continue_flag = True telemetry.track_ssg('query', text) return continue_flag