惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

Martin Heinz's Blog

A Guide to Python's Weak References Using weakref Module Recent Docker BuildKit Features You're Missing Out On Modern Git Commands and Features You Should Be Using Everything You Can Do with Python's textwrap Module Monitoring Indoor Air Quality with Prometheus, Grafana and a CO2 Sensor Everything You Can Do with Python's bisect Module You Don't Need a Dedicated Cache Service - PostgreSQL as a Cache A Collection of Docker Images To Solve All Your Debugging Needs Weird Python "Features" That Might Catch You By Surprise Lessons Learned From Writing 100 Articles Debugging Crashes and Deadlocks in Python using PyStack Goodbye etcd, Hello PostgreSQL: Running Kubernetes with an SQL Database Remote Interactive Debugging of Python Applications Running in Kubernetes The Right Way to Run Shell Commands From Python Real Multithreading is Coming to Python - Learn How You Can Use It Now Python's Missing Batteries: Essential Libraries You're Missing Out On Kubernetes-Native Synthetic Monitoring with Kuberhealthy Reduce - The Power of a Single Python Function Why I Will Never Use Alpine Linux Ever Again Cgroups - Deep Dive into Resource Management in Kubernetes Dictionary Dispatch Pattern in Python Boost Your Python Application Performance using Continuous Profiling Lazy Evaluation Using Recursive Python Generators Python Magic Methods You Haven't Heard About Getting Started with Mastodon API in Python Backup-and-Restore of Containers with Kubernetes Checkpointing API Getting Started with Google APIs in Python Python CLI Tricks That Don't Require Any Code Whatsoever All The Ways To Introspect Python Objects at Runtime What is Python's "self" Argument, Anyway? Python List Comprehensions Are More Powerful Than You Might Think You Should Be Using Python's Walrus Operator - Here's Why Recipes and Tricks for Effective Structural Pattern Matching in Python It's Time to Say Goodbye to These Obsolete Python Libraries Advanced Features of Kubernetes' Horizontal Pod Autoscaler Data and System Visualization Tools That Will Boost Your Productivity Stop Messing with Kubernetes Finalizers Automate All the Boring Kubernetes Operations with Python End-to-End Monitoring with Grafana Cloud with Minimal Effort Bitly | bit.ly/3JLmSgA Bitly | bit.ly/3uETfbi Ultimate CI Pipeline for All of Your Python Projects Bitly | bit.ly/3M30D82 Bitly | bit.ly/3oMJ6qR Bitly | bit.ly/3IRD7IK Bitly | bit.ly/3A3B69t Profiling and Analyzing Performance of Python Programs Bitly | bit.ly/30uviIM Bitly | bit.ly/3E1X2mw Bitly | bit.ly/3Dv7JxP Bitly | bit.ly/3GG1BEz Bitly | bit.ly/3lLavs4 Bitly | bit.ly/39TqP3m Bitly | bit.ly/3A5Mpx8 Bitly | bit.ly/3kGwPl4 Bitly | bit.ly/3iHtulU Bitly | bit.ly/3xGjtKS Bitly | bit.ly/3h8DZg0 Bitly | bit.ly/2RQn1dG Bitly | bit.ly/3p2B5wW Bitly | bit.ly/3tULpb0 Bitly | bit.ly/2PHVudx Bitly | bit.ly/3uPtnb0 Bitly | bit.ly/3dg3QR9 Bitly | bit.ly/3qHtSkZ Bitly | bit.ly/3kIkTPr Bitly | bit.ly/3qlRAUN Bitly | bit.ly/3pCUJ26 Hardening Docker and Kubernetes with seccomp Bitly | bit.ly/34ZhIMt Bitly | bit.ly/3qSO7h0 Bitly | bit.ly/3muGLOk Bitly | bit.ly/35xN79v Bitly | bit.ly/3mLGshK Bitly | bit.ly/2IvkGQl Bitly | bit.ly/2Sk1KFK Bitly | bit.ly/3iCNIL6 Bitly | bit.ly/3beQPpy Saving Your Linux Machine from Certain Death New Features in Python 3.9 You Should Know About Deploy Any Python Project to Kubernetes Analyzing Docker Image Security Recursive SQL Queries with PostgreSQL Automating Every Aspect of Your Python Project Tour of Python Itertools Implementing 2D Physics in Javascript Ultimate Setup for Your Next Python Project Making Python Programs Blazingly Fast Security and Cryptography Mistakes You Are Probably Doing All The Time Going Serverless with OpenFaaS and Golang - Building Optimized Templates Going Serverless with OpenFaaS and Golang - The Ultimate Setup and Workflow Setting Up Swagger Docs for Golang API Building RESTful APIs in Golang Pytest Features, That You Need in Your (Testing) Life Setting up GitHub Package Registry with Docker and Golang Ultimate Setup for Your Next Golang Project Python Tips and Trick, You Haven't Already Seen, Part 2. Tricks for Postgres and Docker that will make your life easier Getting The Most Out of Reading Books - Reading The "Professional Way" Python Tips and Trick, You Haven't Already Seen
Make Your CLI Demos a Breeze with Zero Stress and Zero Mistakes
Martin · 2023-04-04 · via Martin Heinz's Blog

Running live demos can be stressful. You know what you want to say and show. You prepare the CLI commands you want to run to best showcase what you've built, but then you waste time typing long commands; you make typos; the commands fail or take way too long to complete. Maybe they depend on external system (network, APIs, cloud, ...) and of course it's not cooperating while you're running your live demo.

Here's how you can avoid all of the above by scripting, automating and recording your demos, so that you never have to stress about showcasing ever again...

Note: All the scripts and code are made available in this repository.

The Script

It's easy to forget some of the things that you want to show or say during showcase, so you might want to write a script - both in the "theatre" and "programming" sense of the word.

Just writing things down and following this script can make a showcase much smoother, but why not go a step further and make it an actual runnable script?

There are a couple tools you could use for this. We will talk about two - demo-magic and https://github.com/saschagrunert/demo.

The former is a simple, yet powerful set of bash functions and helpers which you can use to automate your demos/showcases as a shell script. To install it, run the following:


git clone https://github.com/paxtonhare/demo-magic.git
mkdir demo  # Create a working dir for showcase
cp demo-magic/demo-magic.sh demo/demo-magic.sh  # Copy the helpers

# Install "Pipe Viewer" for simulated typing
# MacOS:
brew install pv

# Ubuntu:
apt-get install pv

# Move to showcase directory and create your "script"
cd demo
touch sample.sh
chmod +x sample.sh

Now we just need to write our script, which will live in the demo/sample.sh file:


#!/bin/bash

# Include the "demo-magic" helpers
source demo-magic.sh

DEMO_PROMPT="${GREEN}➜ ${CYAN}\W ${COLOR_RESET}"
TYPE_SPEED=30

function comment() {
  cmd=$DEMO_COMMENT_COLOR$1$COLOR_RESET
  echo -en "$cmd"; echo ""
}

clear

comment "# Simple commands:"
pe 'ps aux | head'
pe 'ls -l'

comment "# Print and execute immediately"
pei 'cat some-file'
echo

comment "# Long running:"
pe 'docker build -t some-app .'

comment "# Error prone (dependant on external factors like network):"

pe 'curl --silent -X GET https://httpstat.us/418 -H "Accept: application/json" | jq .'

pe 'curl -i -X POST "https://httpbin.org/status/204" --data "{'some':'data'}"'

pe 'wget -q --show-progress -O some-file.txt https://raw.githubusercontent.com/octocat/Hello-World/master/README'

comment "# Hard to type:"

pe 'openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes -keyout example.key -out example.crt -subj "/CN=example.com" -addext "subjectAltName=DNS:example.com,DNS:www.example.net,IP:10.0.0.1" 2>/dev/null'

comment "#  Enter interactive mode..."
cmd  # Run 'ls -l | grep example' to show result of 'openssl ...'

Let's go over what's going on here - we start by including the demo-magic.sh so that we can use its helper functions (pe, pei and cmd). Next we configure the prompt and simulated typing speed, and we also add a helper function of our own - comment - which simply prints text without executing it, so that we can describe the commands we're showing.

Then comes the actual script - after clearing the terminal we run series of commands using pe and pei. Both of these print and execute the specified command, but the former waits for enter and latter executes immediately. If you want to include an interactive section in your demo, then you can also use cmd command, which lets you type out and run single command before proceeding with rest of the script.

And running the above example would result in something like this:

demo-magic

The other tool I mentioned is https://github.com/saschagrunert/demo, which uses Golang to build the script instead of shell (you decide whether that belongs into pros or cons column). Therefore, to use it we need to create a Golang project. First create a go.mod file:


# go.mod
module cli-demo

go 1.20

And install the tool from GitHub:


go get github.com/saschagrunert/demo@latest

Then create a main.go file which will become your demo script:


package main

import (
 demo "github.com/saschagrunert/demo"
 "github.com/urfave/cli/v2"
)

func main() {
 d := demo.New()
 d.Add(simple(), "simple", "Simple commands demo")
 d.Run()
}

func simple() *demo.Run {
 r := demo.NewRun(
  "Simple commands demo",
 )
 r.Step(demo.S(
  "Simple commands:",
 ), nil)
 r.Step(nil, demo.S(
  "ls -l",
 ))
 r.Step(nil, demo.S(
  "cat some-file",
 ))

 return r
}

In main function we instantiate the demo and add (Add(...)) demo runs to it. In this case we only add single run named simple, but you can use the Add function multiple times. In the demo run function we create individual steps using r.Step(demo.S(...)), where first and second parameter represent comment and CLI command respectively. Either of these can be omitted if you only want to print comment/command.

Now you can build it and run it:


go build
./cli-demo --all

And again, a complete example would look like so:

CLI Showcase

To keep things concise and readable, I didn't include the full code used to create the above GIF, but you can find it here.

One advantage of this tool over demo-magic are that it includes setup and teardown functions, so you can easily prepare and destroy the environment of your demo. It also produces a executable which can be useful for distributing the demo. On the other hand though, this tool doesn't have an option for customizing prompt or changing speed of simulated typing, with that said the whole code is less than 300 lines, so you could add these features quite easily.

Recording

Most of the time you might be satisfied with a runnable script. You will use it once during a live demo/showcase and never touch it again. However, in some cases, you might want to (pre)record the demo and share it. For example, so you can add an example GIF to a GitHub repository. For that you can use tool called terminalizer.

Nice thing about this tool is that it doesn't record an actual video of your screen/window, rather it generates a YAML file based on what you type in terminal. This file can be then adjusted/fixed, so can do some "post-processing" - such as removing frames - just by changing the generated YAML before rendering GIF from it.

To install it, run:


# You might need 'sudo'
npm install -g terminalizer

terminalizer init
# The global config directory is created at
# /home/martin/.terminalizer

After installing with npm, we initialize terminalizer which creates a global configuration file at /home/user/.terminalizer/config.yaml. You can leave the config as is, but I recommend adjusting some options:


frameDelay: 150
# If you want to force size of window
cols: 140
rows: 75

frameBox:
  type: floating
  title: <Demo Specific>  # or null
  style:
    backgroundColor: "#263238"
    border: 0px black solid

theme:
  background: "transparent"
  foreground: "#afafaf"
  cursor: "#c7c7c7"
  black: "#2E3436"
  red: "#CC0000"
  # ...

As I mentioned, terminalizer only records strokes, not the actual screen, so this config allows us to change styling of the final render, including color scheme, window size, etc.

By default, terminalizer will use same pauses between strokes/typing as was during recording, which might mean that there will be unnecessarily long or short pauses. To fix that we can change frameDelay value to specific number of milliseconds between each frame.

Another configuration you might want to change is framebox, which sets terminal window title, size and style. Finally, I also like to set theme to the same color palette, which I use in the actual terminal.

If you want some inspiration, then my configuration can be found here.

With global config ready, we can run terminalizer config which will copy the default config into current directory. We can then record our demo in this directory without making changes to the default config:


terminalizer record demo --config config.yaml

# ... perform the demo commands

# CTRL+D to stop recording...

# Successfully Recorded
# The recording data is saved into the file:
# /home/.../demo.yml

# ---------

terminalizer play demo

# https://github.com/faressoft/terminalizer/issues/29
# sudo chown -R <YOUR_USERNAME> /usr/lib/node_modules/terminalizer/render/
terminalizer render demo
# ...
# Successfully Rendered
# The animated GIF image is saved into the file:
# /.../render1677937002675.gif

Running terminalizer record demo will start screen (keystroke) recording and hitting CTRL+D will stop and save the results into ./demo.yaml. You can then use terminalizer play demo to replay the demo right in the terminal as if you were typing it. Afterwards, when you're satisfied with the recording, you can run terminalizer render demo which will render a GIF using the style configuration in config.yaml.

To then combine the scripting and recording into one piece you can do something like this (that is - my preferred workflow):


# ... Edit 'main.go'
go build
# OR
cp template.sh my-cool-demo.sh
# Edit 'my-cool-demo.sh'

# -----

# Start recording:
terminalizer record demo
# Start scripted demo:
./cli-demo --all -i
# OR
./my-cool-demo.sh
# ... Go through the demo
# CTRL+D
terminalizer play demo  # To check
# ... Adjust demo.yml
terminalizer render demo  # To render

The GIFs that this will produce can be pretty big - in my case upwards of 40MBs - so you might want to use --step N argument with the render command to skip some frames. Alternatively, you can also run it through https://gifcompressor.com/.

Finally, while rendering GIFs has been so far sufficient for me, you might also want render videos, which terminalizer can't do - for that I recommend checking out asciinema which renders output that is closer to a proper video format.

Conclusion

While point of this article was to provide a way to make your showcases and demos a little less stressful and smoother, another big benefit of using these tools is better quality - running a cool, impressive, fancy demo can make a big difference. Having a fancy showcase can keep audience engaged and can help you leave a good impression even if you're showcasing something boring.