Task CLI is an example command-line interface created to facilitate examples in Part 4: Put On Your PowerShell® Hat. Despite having hard coded logic, it allows a user to experiment with object-style text processing in PowerShell and observe error code handling.
The following source code can be used to build and experiment with Task CLI in both a traditional Linux shell as well as PowerShell.
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"strings"
)
// Task defines a sample struct
type Task struct {
ID int `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
}
func getTasks() []Task {
return []Task{
{ID: 1, Name: "Write spec", Status: "in-progress"},
{ID: 2, Name: "Review code", Status: "done"},
{ID: 3, Name: "Deploy", Status: "pending"},
}
}
func outputText(tasks []Task, quiet bool) {
if !quiet {
fmt.Printf("%-5s %-20s %-12s\n", "ID", "Name", "Status")
fmt.Println(strings.Repeat("-", 40))
}
for _, t := range tasks {
fmt.Printf("%-5d %-20s %-12s\n", t.ID, t.Name, t.Status)
}
}
func outputJSON(tasks []Task) {
out, err := json.MarshalIndent(tasks, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "JSON serialization error: %v\n", err)
os.Exit(1)
}
fmt.Println(string(out))
}
func printVersion() {
fmt.Println("taskcli v0.1.0")
}
func main() {
output := flag.String("output", "text", "Output format: text or json")
quiet := flag.Bool("quiet", false, "Suppress text output headers")
version := flag.Bool("version", false, "Show version and exit")
flag.Parse()
if *version {
printVersion()
os.Exit(0)
}
tasks := getTasks()
switch *output {
case "json":
outputJSON(tasks)
case "text":
outputText(tasks, *quiet)
default:
fmt.Fprintf(os.Stderr, "Unsupported output format: %s\n", *output)
os.Exit(1)
}
}
Assuming Go is already installed, create and build taskcli, use the following steps:
mkdir /root/build/taskcli
cd /root/build/taskcli
vim main.go
go mod init taskcli
go: creating new go.mod: module taskcli
go: to add module requirements and sums:
go mod tidy
go mod tidy
go build -o taskcli
To install PowerShell on Ubuntu 24.04, use the following steps:
# make a PowerShell directory for testing
cd /root/
mkdir pwsh
# do this in Ubuntu# 1) cd into your pwsh directory
cd /root/pwsh
# 2) Install prerequisites (Ubuntu 24.04 “Noble” ships libicu74, not libicu72)
apt-get update
apt-get install -y \
curl \
libunwind8 \
libssl3 \
libicu74 \
zlib1g \
libstdc++6 \
libgcc-s1 \
libgssapi-krb5-2 # [oai_citation_attribution:0‡Launchpad](https://launchpad.net/ubuntu/noble/%2Bpackage/libicu74?utm_source=chatgpt.com)
# 3) Download the PowerShell 7.5.0 Linux x64 tarball
curl -sSL -o powershell-7.5.0-linux-x64.tar.gz \
https://github.com/PowerShell/PowerShell/releases/download/v7.5.0/powershell-7.5.0-linux-x64.tar.gz # [oai_citation_attribution:1‡Microsoft Learn](https://learn.microsoft.com/en-us/powershell/scripting/install/install-other-linux?view=powershell-7.5&utm_source=chatgpt.com)
# 4) Extract everything from the versioned subfolder
tar -xvf powershell-7.5.0-linux-x64.tar.gz
# 5) Move its contents up into your current directory
mv powershell-7.5.0-linux-x64/* .
# (move any hidden files too; ignore errors if none)
mv powershell-7.5.0-linux-x64/.* . 2>/dev/null || true
# 6) Remove the now-empty folder and the archive
rm -rf powershell-7.5.0-linux-x64 powershell-7.5.0-linux-x64.tar.gz
# 7) Ensure the pwsh binary is executable
chmod +x pwsh
# 8) Launch PowerShell
./pwsh and get the steps right.
Enter PowerShell and run taskcli both using Linux composed commands as well as PowerShell scripting semantics:
# enter PowerShell
cd /root/pwsh
./pwsh
PowerShell 7.5.0
PS /root/pwsh>
# run taskcli with Linux style syntax.
cd /root/build/taskcli
./taskcli | grep "in-progress"
1 Write spec in-progress
# run taskcli with PowerShell style syntax.
./taskcli --output=json | ConvertFrom-Json | Where-Object { $_.status -ne "done" } | Format-Table
id name status
-- ---- ------
1 Write spec in-progress
3 Deploy pending
# run taskcli with PowerShell processing text.
./taskcli --output=text | Select-String -NotMatch 'done'
ID Name Status
----------------------------------------
1 Write spec in-progress
3 Deploy pending
You can use the Exit Code CLI program from this site to test PowerShell exit code processing.
./exitcode 0
Exited with code 0 - Exited Successfully
PS /root/build/exit-code> echo $LASTEXITCODE
0
PS /root/build/exit-code> ./exitcode 1
Exited with code 1 - General Error
PS /root/build/exit-code> echo $LASTEXITCODE
1