Page 1 of 3

Identify if output goes to the terminal or is being redirected, in Golang May 16 2021

Good command-line tools are a pleasure to work with. A feature I’m always grateful for is when the developer took the time to provide an output that is human readable and an output that is easy to pass to another tool for additional processing. A neat trick to differentiate between these two cases is by having the application identify if the output is being “piped” to another program or not. If the process’s output is not being piped or redirected, we can assume that the user is looking at the results via a terminal. If that is not the case, our application could behave differently and show output that is easier to parse by another program. In this post, I’ll show you how to determine if the stdout of a program is being redirected or piped using the Go programming language.

Read More...

How to manage multiple AWS accounts for the AWS CLI on the same computer Mar 6 2021

Lately, I’ve had to work with multiple AWS accounts, and some of them are ephemeral. I don’t want to have them bloating my ~/.aws/credentials file. In this short post, I’ll show you how to manage multiple AWS accounts using the tool direnv.

Before getting to using direnv, let’s cover some basic concepts, so we all start from the same base.

AWS CLI tool

In normal circumstances, we use the following command to configure our AWS CLI:

Read More...

Building a text-based application using Swift and ncurses Aug 6 2020

The ncurses(3) library powers many popular text-based applications, for example, emacs(1) and htop(1). The use of ncurses is not required to build text-based applications. We could use escape sequences. And for small command-line tools escape sequences are enough, but sometimes it’s nice to rely on a library that handles edge cases. In this post, we’ll build a text-based clock that uses SwiftCursesTerm, a wrapper library I created for using ncurses in Swift.

Read More...

How to read passwords and sensitive data from the command-line using Swift Jul 17 2020

Shoulder surfing is a real threat. And we, as software developers, should strive to provide safety to our users. One way to mitigate the inadvertent exposure of sensitive data is related to how we handle the input of sensitive data in our applications. Every time the user inputs sensitive data, we should hide it from prying eyes. In this post, we’ll learn how to read passwords and passphrases on a command-line tool built using Swift.

Read More...

Understanding the Swift Argument Parser and working with STDIN Jul 7 2020

Operating systems have provided command-line interfaces for decades now, and all but the simplest command-line tools require argument parsing. Due to years of use and refinement, there are many expectations on how command-line tools should handle arguments. Because of these popular conventions, creating an argument parser is not as simple as we might think.

Creating a bespoke argument parser might not be where we would like to spend most of our time. The good news is that you don’t have to, Apple open-sourced the Swift Argument Parser (SAP). In this post, we’ll learn how the Swift Argument Parser works, and how to use it for handling STDIN for composable command-line tools.

Read More...

Using the script command to record a terminal session May 25 2020

When working on the command-line, I sometimes would like to record what I was doing to extract the exact message a script returned without having to rerun all the commands. Similarly, when I’m trying to report an error to a coworker and want to show precisely what I did. All of this can be achieved by using script(1). The script command allows us to record the terminal session, including output and input to a file for later analysis. It is handy, so in this note, I’ll show you how I usually use it.

Read More...

Command-line argument parsing using Swift Package Manager's TSCUtility module Nov 21 2019

If you’ve used the Swift Package Manager, you have interacted with its handy command-line tool. When creating command-line tools, we strive to provide an easy to use interface. One of the main characteristics of a good CLI tool is how it handles parameters. In this post, I’ll show you how to use Swift Package Manager’s TSCUtility module, and especially ArgumentParser to parse arguments for your swift command-line tools.

Let’s start by defining the common types of arguments we get in command-line tools:

Read More...

Creating a Launch Agent that provides an XPC service on macOS using Swift Oct 21 2019

Last week we discussed how to build XPC Services(the .xpc bundles) inside your macOS applications. This week we are going to explore how to provide XPC services that can be used from other applications or tools.

To make the XPC service available to other processes, we are going to create a launch agent. So let’s start by understanding how Launch Agents work.

* You can find the code for the Launch Agent in this GitHub repository

Read More...

XPC Services on macOS apps using Swift Oct 17 2019

We have access to many Inter-Process-Communication(IPC) mechanisms in macOS. One of the newest is XPC1. Before XPC a common way to use IPC, and provide services between processes, was through Sockets or Mach Messages (Using Mach Ports).

Nowadays, we are encouraged by Apple to use XPC in our applications and frameworks that provide services. Also, Apple, in their code, has updated many of its frameworks, daemons and applications to use XPC.

Read More...

Using Kernel Queues (kqueue) notifications in Swift Oct 9 2019

Many components of macOS trace their roots back to BSD. One key aspect inherited from BSD is the Kernel Event Notification mechanism know as Kernel Queues (kqueues for short).

In the past, before kqueue(2)1/ kevent(2)2, when we wanted to track events related to a socket, or if a file descriptor changed, we used a polling mechanism to test for readiness/events (maybe using select(2) or poll(2)). This polling techniques were inefficient and got replaced by the more efficient kqueue API. Instead of continually polling, we can now be notified about events by the kernel. Also, mechanisms like select(2) and poll(2) were restricted to work with file descriptors. kqueue increases the range of operating systems events to monitor not only for file descriptors but also elements like signals, timers, network devices, etc.

Read More...

$