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)
def list(rows): """Print all teams.""" util.run_sql(f""" SELECT tid, uid FROM teams ORDER BY tid, uid DESC """, rows=rows)
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")
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)
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)
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)
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)
def view(): """Print the current configuration""" util.run_sql(f"SELECT * FROM settings", rows=100)
def stmt(rows, query): """Run a single SQL query on the database.""" util.run_sql(" ".join(query), rows=rows)
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)
def view(): """Print all settings""" util.run_sql(f"SELECT * FROM settings", rows=100)