Skip to content

Commit

Permalink
Merge pull request #7 from Mr-Sunglasses/code-cleanup
Browse files Browse the repository at this point in the history
Code cleanup and version bump
  • Loading branch information
Mr-Sunglasses committed Dec 8, 2023
2 parents 93f6e9f + e8b6d50 commit eeaa33f
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 80 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]" }]
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/bitssh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
import tomli as tomllib


__version__ = "2.0.0"
__version__ = "2.5.0"
23 changes: 11 additions & 12 deletions src/bitssh/__main__.py
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()
17 changes: 17 additions & 0 deletions src/bitssh/argument_parser.py
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
42 changes: 0 additions & 42 deletions src/bitssh/core.py

This file was deleted.

29 changes: 29 additions & 0 deletions src/bitssh/prompt.py
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)
20 changes: 20 additions & 0 deletions src/bitssh/ui.py
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)
67 changes: 44 additions & 23 deletions src/bitssh/utils.py
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

0 comments on commit eeaa33f

Please sign in to comment.