dkdc open
What I use my own CLI tool for.
I like CLI tools and at some point decided to build my own. While Go and Rust are great options for writing CLI tools, I wind up switching back to Python due to its simplicity and ease of use (especially for AI-assisted code).
Regardless of the language, the repository my CLI code lives in, or where I’m working I consistently use one function nearly anytime I use my computer: dkdc open
.
history
I famously love Microsoft Edge as my web browser and famously hate having to sign into accounts to access content. I find managing bookmarks in a browser to be incredibly annoying, and trying to sync them across computers moreso. I could rant on.
I wanted to be able to quickly open webpages or applications from a terminal and update the list of things I open frequently.
implementation
I decided to use a config.toml
that looks like this:
[open.aliases]
i = "ibis"
z = "zulip"
s = "search"
e = "email"
c = "cal"
d = "drive"
g = "github"
y = "youtube"
gh = "github"
ym = "youtubemusic"
io = "dkdcio"
key = "keyboard"
read = "kindle"
music = "youtubemusic"
[open.things]
# websites
cal = "https://calendar.google.com"
ibis = "https://ibis-project.org"
dkdc = "https://dkdc.dev"
dkdcio = "https://dkdc.io"
chat = "https://chat.openai.com"
drive = "https://drive.google.com"
email = "https://mail.google.com"
search = "https://google.com"
github = "https://github.com"
keyboard = "https://configure.zsa.io/moonlander"
youtube = "https://youtube.com"
youtubemusic = "https://music.youtube.com"
kindle = "https://read.amazon.com"
audible = "https://audible.com"
##cloud
md = "https://app.motherduck.com"
gcp = "https://console.cloud.google.com"
azure = "https://portal.azure.com"
posit = "https://connect.posit.cloud"
openai = "https://platform.openai.com/docs/overview"
## apps
edge = "/Applications/Microsoft Edge.app"
zulip = "/Applications/Zulip.app"
spotify = "/Applications/Spotify.app"
The open.things
section defines all the things, and the open.aliases
are corresponding aliases.
The relevant code in the CLI is straightforward:
@app.command()
@app.command("o", hidden=True)
def open(
str = typer.Argument(None, help="thing to open"),
thing:
):"""
open thing
"""
import subprocess
from dkdc_util import get_config_toml
def open_it(thing: str) -> None:
"""
open a thing
"""
= get_config_toml()
config
if thing in config["open"]["aliases"]:
= config["open"]["things"][config["open"]["aliases"][thing]]
thing elif thing in config["open"]["things"]:
= config["open"]["things"][thing]
thing
print(f"opening {thing}...")
"open", thing])
subprocess.call([
def list_things() -> None:
"""
list things
"""
= get_config_toml()
config
= []
aliases = []
things
for alias, thing in config["open"]["aliases"].items():
aliases.append((alias, thing))
for thing in config["open"]["things"]:
"open"]["things"][thing]))
things.append((thing, config[
=lambda x: (len(x[0]), x[0]))
aliases.sort(key=lambda x: (len(x[0]), x[0]))
things.sort(key
= max([len(alias) for alias, _ in aliases])
alias_max = max([len(thing) for thing, _ in things])
thing_max
= "aliases:\n"
to_print for alias, thing in aliases:
+= f" - {alias.ljust(alias_max)} | {thing}\n"
to_print
+= "\n\nthings:\n"
to_print for thing, path in things:
+= f" - {thing.ljust(thing_max)} | {path}\n"
to_print
print(to_print)
if thing is None:
list_things()else:
open_it(thing)
I can then use dkdc open <thing|alias>
(or dkdc o <thing|alias>
) to open things. If I don’t provide a thing or alias, it will list all the things and aliases.
one more thing
There’s also a dkdc config
(or dkdc c
) command that will open the config.toml
file in my editor. I use this to quickly update the list of things I open frequently.
@app.command()
@app.command("c", hidden=True)
def config(
bool = typer.Option(False, "--vim", "-v", help="open with (n)vim"),
vim: bool = typer.Option(False, "--env", "-e", help="open .env file"),
env:
):"""
open config file
"""
import os
import subprocess
from dkdc_util import get_dkdc_dir
= "nvim" if vim else "code"
program = ".env" if env else "config.toml"
filename
= os.path.join(get_dkdc_dir(), filename)
filename
print(f"opening {filename} with {program}...")
f"{filename}"]) subprocess.call([program,