def _set_creation(self) -> None: """Set creation option for stack.""" print(80 * "-") fzf = Pyfzf() fzf.append_fzf("RollbackOnFailure\n") fzf.append_fzf("TimeoutInMinutes\n") fzf.append_fzf("EnableTerminationProtection\n") selected_options: List[str] = list( fzf.execute_fzf( empty_allow=True, print_col=1, multi_select=True, header="select options to configure", ) ) for option in selected_options: result: str = "" if option == "RollbackOnFailure": fzf.fzf_string = "" fzf.append_fzf("True\n") fzf.append_fzf("False\n") result = str( fzf.execute_fzf( empty_allow=True, print_col=1, header="roll back on failue? (Default: True)", ) ) if result: self._extra_args["OnFailure"] = ( "ROLLBACK" if result == "True" else "DO_NOTHING" ) elif option == "TimeoutInMinutes": message = "Specify number of minutes before stack timeout (Default: no timeout): " timeout = input(message) if timeout: self._extra_args["TimeoutInMinutes"] = int(timeout) elif option == "EnableTerminationProtection": fzf.fzf_string = "" fzf.append_fzf("True\n") fzf.append_fzf("False\n") result = str( fzf.execute_fzf( empty_allow=True, print_col=1, header="enable termination protection? (Default: False)", ) ) if result: self._extra_args["EnableTerminationProtection"] = ( True if result == "True" else False )
def set_s3_path(self, download: bool = False) -> None: """Set 'path' of s3 to upload or download. s3 folders are not actually folder, found this path listing on https://github.com/boto/boto3/issues/134#issuecomment-116766812 This method would set the 'path' for s3 however the self.path_list cannot be used as the destination of upload immediately. This only set the path without handling different upload sceanario. Please use the get_s3_destination_key after set_s3_path to obtain the correct destination key :param download: if not download, add append option :type download: bool, optional :raises NoSelectionMade: when user did not make a bucket selection, exit """ selected_option = self._get_path_option(download=download) if selected_option == "input": self.path_list[0] = input("Input the path(newname or newpath/): ") elif selected_option == "root": # print("S3 file path is set to root") pass elif selected_option == "append" or selected_option == "interactively": paginator = self.client.get_paginator("list_objects") fzf = Pyfzf() parents = [] # interactively search down 'folders' in s3 while True: if len(parents) > 0: fzf.append_fzf("\033[34m../\033[0m\n") fzf.append_fzf("\033[33m./\033[0m\n") with Spinner.spin(message="Fetching s3 objects ..."): preview: str = "" for result in paginator.paginate( Bucket=self.bucket_name, Prefix=self.path_list[0], Delimiter="/", ): for prefix in result.get("CommonPrefixes", []): fzf.append_fzf("%s\n" % prefix.get("Prefix")) for content in result.get("Contents", []): preview += content.get("Key") preview += "^" # has to use tr to transform the string to new line during preview by fzf # not sure why, but if directly use \n, fzf preview interpret as a new command # TODO: findout why selected_path = str( fzf.execute_fzf( print_col=0, header= 'PWD: s3://%s/%s (select "./" will the current path)' % (self.bucket_name, self.path_list[0]), preview="echo %s | tr '^' '\n'" % preview.rstrip(), )) if not selected_path: raise NoSelectionMade if selected_path == "../": self.path_list[0] = parents.pop() elif selected_path == "./": break else: parents.append(self.path_list[0]) self.path_list[0] = selected_path # reset fzf string fzf.fzf_string = "" if selected_option == "append": print("Current PWD is s3://%s/%s" % (self.bucket_name, self.path_list[0])) new_path = input( "Input the new path to append(newname or newpath/): ") self.path_list[0] += new_path print("S3 file path is set to %s" % (self.path_list[0] if self.path_list[0] else "root"))