summaryrefslogtreecommitdiffstats
path: root/eos_downloader/cli/info/commands.py
blob: 64097a192b77df9f127c75f65512037f61b7bba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
# coding: utf-8 -*-
# pylint: disable=no-value-for-parameter
# pylint: disable=too-many-arguments
# pylint: disable=line-too-long
# pylint: disable=redefined-builtin
# flake8: noqa E501

"""
Commands for ARDL CLI to list data.
"""

import sys
from typing import Union

import click
from loguru import logger
from rich.console import Console
from rich.pretty import pprint

import eos_downloader.eos
from eos_downloader.models.version import BASE_VERSION_STR, RTYPE_FEATURE, RTYPES


@click.command(no_args_is_help=True)
@click.pass_context
@click.option(
    "--latest",
    "-l",
    is_flag=True,
    type=click.BOOL,
    default=False,
    help="Get latest version in given branch. If --branch is not use, get the latest branch with specific release type",
)
@click.option(
    "--release-type",
    "-rtype",
    type=click.Choice(RTYPES, case_sensitive=False),
    default=RTYPE_FEATURE,
    help="EOS release type to search",
)
@click.option(
    "--branch",
    "-b",
    type=click.STRING,
    default=None,
    help="EOS Branch to list releases",
)
@click.option(
    "--verbose",
    "-v",
    is_flag=True,
    type=click.BOOL,
    default=False,
    help="Human readable output. Default is none to use output in script)",
)
@click.option(
    "--log-level",
    "--log",
    help="Logging level of the command",
    default="warning",
    type=click.Choice(
        ["debug", "info", "warning", "error", "critical"], case_sensitive=False
    ),
)
def eos_versions(
    ctx: click.Context,
    log_level: str,
    branch: Union[str, None] = None,
    release_type: str = RTYPE_FEATURE,
    latest: bool = False,
    verbose: bool = False,
) -> None:
    # pylint: disable = too-many-branches
    """
    List Available EOS version on Arista.com website.

    Comes with some filters to get latest release (F or M) as well as branch filtering

      - To get latest M release available (without any branch): ardl info eos-versions --latest -rtype m

      - To get latest F release available: ardl info eos-versions --latest -rtype F
    """
    console = Console()
    # Get from Context
    token = ctx.obj["token"]

    logger.remove()
    if log_level is not None:
        logger.add("eos-downloader.log", rotation="10 MB", level=log_level.upper())

    my_download = eos_downloader.eos.EOSDownloader(
        image="unset",
        software="EOS",
        version="unset",
        token=token,
        hash_method="sha512sum",
    )

    auth = my_download.authenticate()
    if verbose and auth:
        console.print("✅ Authenticated on arista.com")

    if release_type is not None:
        release_type = release_type.upper()

    if latest:
        if branch is None:
            branch = str(my_download.latest_branch(rtype=release_type).branch)
        latest_version = my_download.latest_eos(branch, rtype=release_type)
        if str(latest_version) == BASE_VERSION_STR:
            console.print(
                f"[red]Error[/red], cannot find any version in {branch} for {release_type} release type"
            )
            sys.exit(1)
        if verbose:
            console.print(
                f"Branch {branch} has been selected with release type {release_type}"
            )
            if branch is not None:
                console.print(f"Latest release for {branch}: {latest_version}")
            else:
                console.print(f"Latest EOS release: {latest_version}")
        else:
            console.print(f"{ latest_version }")
    else:
        versions = my_download.get_eos_versions(branch=branch, rtype=release_type)
        if verbose:
            console.print(
                f'List of available versions for {branch if branch is not None else "all branches"}'
            )
            for version in versions:
                console.print(f"  → {str(version)}")
        else:
            pprint([str(version) for version in versions])