Esempio n. 1
0
def set(key, value):
    """Set a configuration key to a specific value

    Set KEY to VALUE.
    If multiple values are passed, they are treated as a list of strings.
    If a single value is passed that is not valid JSON, it is treated as a string.

    \b
    Examples:
        - r8 settings set host localhost
        - r8 settings set port 8000
        - r8 settings set static_dir /first/directory /second/directory
    """
    if len(value) == 1:
        value = value[0]
        try:
            value = json.loads(value)
        except json.JSONDecodeError:
            try:
                value = int(value)
            except ValueError:
                pass  # if the value is neither valid JSON nor an integer, we just treat it as a string.
    else:
        value = list(value)
    value = json.dumps(value)
    with r8.db:
        r8.db.execute(
            "INSERT OR REPLACE INTO settings (key, value) VALUES (?,?)",
            (key, value))
    util.run_sql(f"SELECT * FROM settings", rows=100)
Esempio n. 2
0
def list(rows):
    """Print all teams."""
    util.run_sql(f"""
    SELECT tid, uid FROM teams
    ORDER BY tid, uid DESC
    """,
                 rows=rows)
Esempio n. 3
0
def tables():
    """Print overview of all tables."""
    table_names = [
        x[0] for x in r8.db.execute(
            "SELECT name FROM sqlite_master WHERE type='table'").fetchall()
    ]
    for table in table_names:
        click.secho(f"[{table}]", fg="green")
        util.run_sql(f"SELECT * FROM {table} LIMIT 1")
Esempio n. 4
0
def list(rows, query):
    """
    Print the state of all created challenges.

    Accepts an optional query argument to limit the selection,
    e.g. "WHERE cid LIKE 'Basic%'".
    """
    util.run_sql(f"""
    SELECT cid, COUNT(uid) AS solved, t_start, t_stop, team FROM challenges
    LEFT JOIN flags USING (cid)
    LEFT JOIN submissions USING (fid)
    {" ".join(query)}
    GROUP BY cid
    ORDER BY challenges.rowid
    """, rows=rows)
Esempio n. 5
0
def list(rows, challenge):
    """Print all flags [for a given challenge]."""
    if challenge:
        where = "WHERE cid = ?"
        parameters = (challenge, )
    else:
        where = ""
        parameters = None
    util.run_sql(f"""
    SELECT cid, fid, COUNT(uid) AS submissions, max_submissions FROM flags
    LEFT JOIN submissions USING(fid)
    {where}
    GROUP BY fid
    ORDER BY cid, submissions DESC
    """,
                 parameters,
                 rows=rows)
Esempio n. 6
0
def set(key, value):
    """Update a setting"""
    if len(value) == 1:
        value = value[0]
        try:
            value = json.loads(value)
        except json.JSONDecodeError:
            try:
                value = int(value)
            except ValueError:
                pass  # if the value is neither valid JSON nor an integer, we just treat it as a string.
    else:
        value = list(value)
    value = json.dumps(value)
    with r8.db:
        r8.db.execute(
            "INSERT OR REPLACE INTO settings (key, value) VALUES (?,?)",
            (key, value))
    util.run_sql(f"SELECT * FROM settings", rows=100)
Esempio n. 7
0
def delete(key):
    """Delete a configuration key."""
    with r8.db:
        r8.db.execute("DELETE FROM settings WHERE key = ?", (key, ))
    util.run_sql(f"SELECT * FROM settings", rows=100)
Esempio n. 8
0
def view():
    """Print the current configuration"""
    util.run_sql(f"SELECT * FROM settings", rows=100)
Esempio n. 9
0
def stmt(rows, query):
    """Run a single SQL query on the database."""
    util.run_sql(" ".join(query), rows=rows)
Esempio n. 10
0
def delete(key):
    """Update a setting"""
    with r8.db:
        r8.db.execute("DELETE FROM settings WHERE key = ?", (key, ))
    util.run_sql(f"SELECT * FROM settings", rows=100)
Esempio n. 11
0
def view():
    """Print all settings"""
    util.run_sql(f"SELECT * FROM settings", rows=100)