Skip to content

Commit

Permalink
feat: create gitlab base class to auth the instance and return if no …
Browse files Browse the repository at this point in the history
…cath erros
  • Loading branch information
lpmatos committed Jul 28, 2020
1 parent f1d32e9 commit 11eeca0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 57 deletions.
78 changes: 39 additions & 39 deletions gitlabrc/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,50 +23,50 @@ def _create_parser_object(self) -> ArgumentParser:

def _adding_arguments(self) -> NoReturn:
self._parser.add_argument("-u", "--url",
type = str,
dest = "url",
default = "https://gitlab.com",
metavar = "<url>",
help = "base URL of GitLab instance")
type = str,
dest = "url",
default = self._config.get_env("GITLAB_URL"),
metavar = "<url>",
help = "base URL of GitLab instance")
self._parser.add_argument("-t", "--token",
type = str,
dest = "token",
default = self._config.get_env("GITLAB_TOKEN"),
metavar = "<token>",
help = "token GitLab API")
type = str,
dest = "token",
default = self._config.get_env("GITLAB_TOKEN"),
metavar = "<token>",
help = "token GitLab API")
self._parser.add_argument("-n", "--namespace",
type = str,
dest = "namespace",
default = "",
metavar = "<namespace>",
help = "namespace in GitLab to clone all projects")
type = str,
dest = "namespace",
default = "",
metavar = "<namespace>",
help = "namespace in GitLab to clone all projects")
self._parser.add_argument("-p", "--path",
dest = "path",
default = self._config.get_env("PWD"),
metavar = "<path>",
help = "destination path to cloned projects")
dest = "path",
default = self._config.get_env("PWD"),
metavar = "<path>",
help = "destination path to cloned projects")
self._parser.add_argument("-m", "--method",
type = CloneMethod.parse,
dest = "method",
default = self._config.get_env("GITLAB_CLONE_METHOD", "http"),
metavar = "<method>",
choices = list(CloneMethod),
help = "method used in GitLabRC to cloning repositories (either <http> or <ssh>)")
type = CloneMethod.parse,
dest = "method",
default = self._config.get_env("GITLAB_CLONE_METHOD", "http"),
metavar = "<method>",
choices = list(CloneMethod),
help = "method used in GitLabRC to cloning repositories (either <http> or <ssh>)")
self._parser.add_argument("--disable-root",
action ="store_true",
dest = "noroot",
default = False,
help = "don't create root namepace folder in path")
action ="store_true",
dest = "noroot",
default = False,
help = "don't create root namepace folder in path")
self._parser.add_argument("--dry-run",
action = "store_true",
dest = "dryrun",
default = False,
help = "list all repositories without clone/fetch")
action = "store_true",
dest = "dryrun",
default = False,
help = "list all repositories without clone/fetch")
self._parser.add_argument("--tree",
action = "store_true",
dest = "tree",
default = False,
help = "list all repositories using anytree")
action = "store_true",
dest = "tree",
default = False,
help = "list all repositories using anytree")
self._parser.add_argument("--version",
action = "store_true",
help = "show version")
action = "store_true",
help = "show version")
22 changes: 22 additions & 0 deletions gitlabrc/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import Text, Type, Callable
from dataclasses import dataclass, field
from gitlab import Gitlab, config, exceptions

@dataclass
class GitLabBase:
url: Type[Text] = field(default="https://gitlab.com")
token: Type[Text] = field(repr=False, default_factory=Text)

@property
def client(self) -> Gitlab:
try:
instance = Gitlab(
self.url,
private_token=self.token
)
instance.auth()
except exceptions.GitlabAuthenticationError as error:
print(f"GitLab authenticantion error - {error}")
exit()
else:
return instance
23 changes: 5 additions & 18 deletions gitlabrc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
import re
import shutil
import time
import gitlab
import optparse
from art import *
import subprocess
from collections import defaultdict
from typing import NoReturn

from .arguments import Arguments
from .method import CloneMethod
from .process import Process
from . import __version__ as VERSION
from collections import defaultdict

from typing import NoReturn
from .base import GitLabBase

def pname():
return f"[gitlabrc - {str(os.getpid())}]"
Expand Down Expand Up @@ -50,25 +50,12 @@ def main():

def perform(options):
url, token, namespace = options.url, options.token, options.namespace
root = Node("", root_path="", url=url)
gl = gitlab.Gitlab(url, token)

if not url:
sys.stderr.write("\nError: we need gitlab url information\n\n")
exit(1)

if not token:
sys.stderr.write("\nError: we need gitlab token information\n\n")
exit(1)
gl = GitLabBase(url, token).client

if not os.path.isdir(options.path):
sys.stderr.write("\nError: destination path does not exist " + options.path + "\n\n")
exit(1)

if not namespace:
sys.stderr.write("\nError: we need gitlab namespace information\n\n")
exit(1)

git_path = shutil.which("git")
if git_path == "None":
sys.stderr.write("Error: git executable not installed or not in $PATH" + "\n")
Expand Down

0 comments on commit 11eeca0

Please sign in to comment.