-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Mr-Sunglasses/code-cleanup
Code cleanup and version bump
- Loading branch information
Showing
8 changed files
with
124 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" | |
|
||
[project] | ||
name = "bitssh" | ||
version = "2.0.0" | ||
version = "2.5.0" | ||
description = "A command-line tool for managing SSH connections" | ||
readme = "README.md" | ||
authors = [{ name = "Kanishk Pachauri", email = "[email protected]" }] | ||
|
@@ -32,7 +32,7 @@ Docs = "https://github.com/Mr-Sunglasses/bitssh/blob/master/docs/docs.md" | |
bitssh = "bitssh.__main__:main" | ||
|
||
[tool.bumpver] | ||
current_version = "2.0.0" | ||
current_version = "2.5.0" | ||
version_pattern = "MAJOR.MINOR.PATCH" | ||
commit_message = "Bump version {old_version} -> {new_version}" | ||
commit = true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ | |
import tomli as tomllib | ||
|
||
|
||
__version__ = "2.0.0" | ||
__version__ = "2.5.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
import inquirer | ||
from inquirer.themes import GreenPassion | ||
import os | ||
from . import core | ||
from .prompt import Prompt | ||
from .ui import UI | ||
from .argument_parser import Config | ||
from bitssh import __version__ | ||
|
||
|
||
def main(): | ||
answers = inquirer.prompt(questions=core.questions, theme=GreenPassion()) | ||
cmd = answers["host"] | ||
cmd = cmd[6::] | ||
cmd = f"ssh {cmd}" | ||
core.console.print( | ||
"Please Wait While Your System is Connecting to the Remote Server", | ||
style="green", | ||
) | ||
os.system(cmd) | ||
config = Config() | ||
if config.version: | ||
print(f"bitssh {__version__}") | ||
else: | ||
UI.draw_table() | ||
Prompt.ask_host_prompt() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import argparse | ||
|
||
|
||
class Config: | ||
def __init__(self) -> None: | ||
parser = argparse.ArgumentParser( | ||
description="A New and Modern SSH connector written in Python." | ||
) | ||
parser.add_argument( | ||
"--version", | ||
action="store_true", | ||
default=False, | ||
help="Show the bitssh version.", | ||
) | ||
|
||
args, _ = parser.parse_known_args() | ||
self.version = args.version |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import inquirer | ||
from inquirer.themes import GreenPassion | ||
from .utils import ConfigPathUtility | ||
from .ui import UI | ||
import os | ||
|
||
|
||
class Prompt: | ||
@classmethod | ||
def ask_host_prompt(cls): | ||
HOST = ConfigPathUtility.get_config_file_host_data() | ||
questions = [ | ||
inquirer.List( | ||
name="host", | ||
message="Select the Host Given in the Above List", | ||
choices=HOST, | ||
), | ||
] | ||
|
||
answers = inquirer.prompt(questions=questions, theme=GreenPassion()) | ||
|
||
cmd = answers["host"] | ||
cmd = cmd[6::] | ||
cmd = f"ssh {cmd}" | ||
UI.console.print( | ||
"Please Wait While Your System is Connecting to the Remote Server", | ||
style="green", | ||
) | ||
os.system(cmd) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from rich.console import Console | ||
from rich.table import Table | ||
from .utils import ConfigPathUtility | ||
|
||
|
||
class UI: | ||
console = Console() | ||
|
||
@classmethod | ||
def draw_table(cls): | ||
table = Table(title="SSH Servers in Config File") | ||
table.add_column("Hostname", justify="left", style="cyan", no_wrap=True) | ||
table.add_column("Host", style="magenta") | ||
table.add_column("Port", justify="right", style="green") | ||
table.add_column("User", justify="right", style="yellow") | ||
|
||
for i in ConfigPathUtility.get_config_file_row_data(): | ||
table.add_row(i[0], i[1], i[2], i[3]) | ||
|
||
cls.console.print(table) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,53 @@ | ||
import os | ||
import re | ||
import pprint | ||
|
||
def get_config_path(): | ||
try: | ||
config_path = os.path.expanduser("~/.ssh/config") | ||
return config_path | ||
except FileNotFoundError: | ||
raise f"Config File Not Found" | ||
|
||
class ConfigPathUtility: | ||
config_file_path = os.path.expanduser("~/.ssh/config") | ||
if not os.path.exists(config_file_path): | ||
raise FileNotFoundError( | ||
"Config file is not Found in ~/.ssh/config Please See the Docs of bitssh." | ||
) | ||
|
||
def get_config_content(config_file): | ||
config = {} | ||
current_host = None | ||
@classmethod | ||
def get_config_content(cls): | ||
config = {} | ||
current_host = None | ||
|
||
with open(config_file, "r") as f: | ||
lines = f.readlines() | ||
with open(cls.config_file_path, "r") as f: | ||
lines = f.readlines() | ||
|
||
for line in lines: | ||
line = line.strip() | ||
for line in lines: | ||
line = line.strip() | ||
|
||
if re.match(r"^Host\s+", line): | ||
current_host = re.sub(r"^Host\s+", "", line) | ||
config[current_host] = {} | ||
else: | ||
match = re.match(r"^\s*([\w-]+)\s+(.*)", line) | ||
if match: | ||
key = match.group(1) | ||
value = match.group(2) | ||
config[current_host][key] = value | ||
if re.match(r"^Host\s+", line): | ||
current_host = re.sub(r"^Host\s+", "", line) | ||
config[current_host] = {} | ||
else: | ||
match = re.match(r"^\s*([\w-]+)\s+(.*)", line) | ||
if match: | ||
key = match.group(1) | ||
value = match.group(2) | ||
config[current_host][key] = value | ||
|
||
return config | ||
return config | ||
|
||
@classmethod | ||
def get_config_file_row_data(cls): | ||
config_content = cls.get_config_content() | ||
|
||
ROWS = [] | ||
|
||
for host, attributes in config_content.items(): | ||
row = (attributes["Hostname"], host, attributes["port"], attributes["User"]) | ||
ROWS.append(row) | ||
|
||
return ROWS | ||
|
||
@classmethod | ||
def get_config_file_host_data(cls): | ||
HOST = [] | ||
for hosts in cls.get_config_file_row_data(): | ||
HOST.append(f"Host: {hosts[1]}") | ||
return HOST |