Go program stores directory paths
Database Voodoo
A new directory is added to the database with dirInsert()
as of line 85. If the SQLite database does not yet exist (Listing 1 checks for the existence of the database file), line 76 builds a new one and creates a fresh dirs
table in it with the SQL create
command. The table's two columns, dir
and date
, are of the text
type. The fact that the directory path is a text string is not surprising, but SQLite also saves dates as text and compares them in string mode, which works because the timestamps are in the format YYYY-MM-DD HH::MM::SS, so later times also alphanumerically follow earlier ones.
We do not want SQLite to generate a new table row for existing paths, but simply refresh the existing entry's timestamp. This could be solved in SQL by a separate select query, followed by case logic, but in the SQLite dialect this is done more elegantly with the special REPLACE
function (line 86).
This works in a similar way to SQL's standard UPDATE
function, except it creates missing entries – but only if a unique index is defined for the corresponding table column. This explains why line 80 adds an index to the dir
column after the table definition, causing replace
in line 86 to create new entries and refresh old ones as needed.
The dirList()
function retrieves existing database entries. The select statement in line 52 sorts them in descending order according to insertion date (i.e., entries created shortly beforehand appear at the top of the selection list). The LIMIT 10
statement fetches a maximum of 10, but since the terminal list display scrolls down on request, you could just as easily leave this out.
The for
loop starting in line 59 uses rows.Next()
and rows.Scan()
to fetch the search query's next hits; the append
statement in line 63 appends them to the items
array slice. If the database is still unpopulated, line 67 inserts the user's home directory; otherwise, the displayed selection list would be empty and leave the user confused.
If two or more hits are found, line 69 steals the first one and removes it from the list, because this is the entry of the last directory visited (i.e., the current directory to which the user will probably not want to jump). The dbPath()
function starting in line 95 specifies the path to the SQLite file in which the data is stored, this is hard-coded in Listing 1 as ~/.cdbm.db
in the user's home directory.
Quite Wordy
It's worth noting that a Go program, which actually doesn't contain so much logic, needs a large number of lines. Go's relentlessly required, explicit error handling of each return value is partly to blame. An exception handler would be more compact for such a simple utility. The panicOnErr()
function starting in line 103, which checks an error value passed to it and immediately aborts the program by calling panic()
, helps to save lines. It is rumored that the next Go version will accommodate program authors with more compact mechanisms.
More Convenience
For hobbyists, the fun only starts here. For instance, the script could be extended to include a search function that only offers paths that match a search term entered at the command line. For example, the user could enter c usr
and only see paths containing the string usr
. And since all the user data are stored in an SQLite database whose schema can be easily extended, it makes sense to assign a counter to each stored path, which cdbm
increments by one each time a directory is visited.
This could shift frequently visited paths higher up in the selection list based on an algorithm, because users would not want to scroll a long way to access frequently used paths. And, who knows, maybe it's worth adding a dash of artificial intelligence? A self-learning directory butler would be attractive to my younger coworkers.
Infos
- Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/linux-magazine.com/228 @IE
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.