Skip to content

Config Management

❯ yoda config --help

 Usage: yoda config [OPTIONS] COMMAND [ARGS]...                                                                                                        

 Config management                                                                                                                                     

╭─ Options ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --help          Show this message and exit.                                                                                                         │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ get    Get value of a key set in yoda config                                                                                                        │
│ init   Initializes a sqlite db that will store the config                                                                                           │
│ list   List all key-value pairs in the config                                                                                                       │
│ set    Set a value for a key in config                                                                                                              │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

get_config_value(key)

Get value of a key set in yoda config

Source code in yodapa/core/config.py
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
@app.command(name="get")
def get_config_value(key: str):
    """Get value of a key set in yoda config"""
    conn = None
    try:
        conn = get_db_connection()
        cursor = conn.cursor()
        cursor.execute("SELECT value FROM configurations WHERE key = ?", (key,))
        row = cursor.fetchone()

        if row:
            value = row[0]
            print(f"[blue]{key}[/]: {value}")
        else:
            print(f"[red]Configuration key [white]'{key}'[red] not found.[/]")
    except sqlite3.OperationalError as e:
        if "no such table" in str(e):
            print("[red]Yoda config not initialized. Use [white]`yoda init`[red] to initialize[/]")
        else:
            print(f"An error occurred: {e}")
    finally:
        if conn:
            conn.close()

initialize_config()

Initializes a sqlite db that will store the config

Source code in yodapa/core/config.py
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
@app.command(name="init")
def initialize_config():
    """Initializes a sqlite db that will store the config"""
    if config_sqlite_file.exists():
        delete_old = typer.confirm("This file already exists. Do you want to continue?")

        if not delete_old:
            return

        # delete old file
        config_sqlite_file.unlink()

    conn = get_db_connection()

    cursor = conn.cursor()
    # create tables
    cursor.execute("""
        CREATE TABLE IF NOT EXISTS configurations (
            key TEXT PRIMARY KEY,
            value TEXT NOT NULL
        )
    """)

    cursor.execute("""
        CREATE TABLE IF NOT EXISTS plugins (
            name TEXT PRIMARY KEY,
            enabled BOOLEAN NOT NULL
        )
    """)

    cursor.execute("""
        CREATE TABLE IF NOT EXISTS url (
            name TEXT PRIMARY KEY,
            url TEXT NOT NULL
        )
    """)

    conn.commit()
    conn.close()

    # in addition to creating new tables, we must also load the available plugins
    _refresh_plugins()

list_configurations()

List all key-value pairs in the config

Source code in yodapa/core/config.py
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
@app.command(name="list")
def list_configurations():
    """List all key-value pairs in the config"""
    conn = None
    try:
        conn = get_db_connection()
        cursor = conn.cursor()
        cursor.execute("SELECT key, value FROM configurations")
        rows = cursor.fetchall()

        if not rows:
            print("[yellow] No configurations found.[/]")
            return

        table = Table(title="Configurations")
        table.add_column("Key", style="cyan", no_wrap=True)
        table.add_column("Value", style="magenta")

        for key, value in rows:
            table.add_row(key, value)

        print(table)

    except sqlite3.OperationalError as e:
        if "no such table" in str(e):
            print("[red]Yoda config not initialized. Use [white]`yoda init`[red] to initialize[/]")
        else:
            print(f"An error occurred: {e}")
    finally:
        if conn:
            conn.close()

set_config_value(key, value)

Set a value for a key in config

Source code in yodapa/core/config.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@app.command(name="set")
def set_config_value(key: str, value: str):
    """Set a value for a key in config"""
    conn = None
    try:
        conn = get_db_connection()
        cursor = conn.cursor()
        cursor.execute("""
                INSERT INTO configurations (key, value)
                VALUES (?, ?)
                ON CONFLICT(key) DO UPDATE SET value=excluded.value
            """, (key, value))
        conn.commit()
        print(f"[green]Configuration set:[/] {key} = {value}")
    except sqlite3.OperationalError as e:
        if "no such table" in str(e):
            print("[red]Yoda config not initialized. Use [white]`yoda init`[red] to initialize[/]")
        else:
            print(f"An error occurred: {e}")
    finally:
        if conn:
            conn.close()