Bringing Unix classics, tee and cat, to chat

3 min read Original article ↗

Patrick Van Stee

If you’re familiar with Unix I know you’ve used the cat command to read a file at the start of a pipeline. And, if you’ve ever typo’d something at the end of a pipeline and have had to rerun it only to wait another 3 hours for it to complete, you probably swear by tee. If you haven’t encountered it yet, take a second and read about the tee command. I’m sure it’ll save you some angry up-arrow keypresses in the future.

So, tee and cat. Useful right? But they were both built to deal with files, on a disk… which we don’t have in chat. So how is this useful as a chat command?

Since Cog was built to run commands in pipelines just like Unix, it’d still be nice to be able to save and retrieve intermediate pipeline output. But you’re right; we don’t have files in chat. However, Cog does have a Memory Service, a built-in key-value store for use by commands. So, instead, the tee and cat commands use that and take a key as an argument. Here’s a simple example to show you what that looks like:

@cog ec2:instance search "instance-state-name=running" | tee instances | table instance_id instance_type@cog cat instances | ec2:instance reboot $instance_id

Above we’re getting a list of currently running instances, saving that list with the key instances, and then reviewing them in a table. Once we’re sure everything is safe to reboot, we retrieve that list of instances to start our pipeline and reboot each instance.

Get Patrick Van Stee’s stories in your inbox

Join Medium for free to get updates from this writer.

Remember me for faster sign in

But, wait there’s more. We can also use cat to append or modify previously saved output. As an example, let’s say we want to build up a list of instances to reboot across multiple commands. We can list and store all instances from the us-east-1 region just as before. Then, we can list and append all instances from the us-west-1 region before storing the output under the instances key again.

@cog ec2:instance list --region us-east-1 | tee instances@cog ec2:instance list --region us-west-1 | cat --append instances | tee instances@cog cat instances | ec2:instance reboot $instance_id

Depending on how you need to construct your output data, cat has flags for --append, --merge, and --insert. Read more about how to use them and more examples by running @cog help cat and @cog help tee including tips on constructing rules for authorizing access teeing and cating specific keys.

Also, if you’ve found a clever use for these commands in one of your workflows, leave a comment here or tweet @OperableInc.