Thursday, 27 April 2023

Why oh why did I forget vimdiff ?

 Whilst trying to compare two branches of a GitHub repo on my Mac, I was using diff to compare/contrast specific files, and trying to parse the differences.

And then I remembered vimdiff


Source: Linux `Vimdiff` Command – How to Compare Two Files in the Command Line 

PS Using freeCodeCamp for the above image, as I don't want to reveal my sources ( i.e. source code )

Unix - redirecting output to /dev/null

In the past, I've used redirection to send output to /dev/null such as: -

foobar 2> /dev/null

where foobar is a non-existent command/binary, but I want the error output ( stderr ) such as: -

zsh: command not found: foobar

or: -

foobar: command not found

to be "hidden"

Similarly, I've used redirection to send "pukka" output ( stdout ) to also go to /dev/null e.g. : -

uptime 1> /dev/null

However, I'd not seen the simple way to do both in one fell swoop: -

foobar &> /dev/null

uptime &> /dev/null

where the ampersand ( & ) is used to send BOTH stdout and stderr to /dev/null

As ever, which is nice

IBM Container Registry - searching and formatting

 So, when querying images that have been pushed to a namespace within IBM Container Registry, one can format the output to only return certain columns such as repository (image) name and tag.

Who knew ?

Well, the authors of the documentation did, apparently :-)

Formatting and filtering the CLI output

For example: -

ibmcloud cr images --format "{{ .Repository }}:{{ .Tag }}"

icr.io/hello/hello_world:1.1

icr.io/hello/hello_world:1.2

icr.io/hello/hello_world:latest

etc.

Other examples, from the doc, include: -

ibmcloud cr image-list --format "{{ if gt .Size 1000000 }}{{ .Repository }}:{{ .Tag }} {{ .SecurityStatus.Status }}{{end}}"

ibmcloud cr image-digests --format '{{if not .Tags}}{{.Repository}}@{{.Digest}}{{end}}'

ibmcloud cr image-inspect ibmliberty --format "{{ .ContainerConfig.Labels }}"

etc.

Which is nice!

*UPDATE*

And, bringing two posts together, I can report the created date AND format it from Epoch time: -

ic cr images --format "{{ .Repository }}:{{ .Tag }}:{{ .Created }}" | awk 'BEGIN { FS = ":"} ; {$3 = strftime("%c", $3)} 1'

Friday, 21 April 2023

Today I Learned - Munging Epochs using awk

So today I had a requirement to convert some Epoch-formatted dates, located in a CSV file, into human-readable dates...

So today I learned about awk vs. gawk, and the strftime() function ...

I also learned that awk on macOS isn't the same as "real" Gnu awk ( aka gawk ), hence the need for gawk ...

I started by installing gawk: -

brew install gawk

and then updated my PATH to reflect it: -

PATH="/opt/homebrew/opt/gawk/libexec/gnubin:$PATH"

Using an example of my data: -

cat file.txt

1681486514
1681990787
1681992853
1681712949

which is WAY simpler than my real data, I was then able to munge it using awk ( or, really, gawk ) : -

awk 'BEGIN { FS = ","} ; {$1 = strftime("%c", $1)} 1' file.txt

which returns: -

Fri 14 Apr 16:35:14 2023
Thu 20 Apr 12:39:47 2023
Thu 20 Apr 13:14:13 2023
Mon 17 Apr 07:29:09 2023

I'd previously done much the same using Excel, via a formula: -

=F17/86400+DATE(1970,1,1)

where cell F17 contained the Epoch-formatted date

But scripts are so much more fun ...

Monday, 17 April 2023

IBM Cloud and JQ - more querying fun

 A colleague laid down a challenge - well, he didn't actually lay it down, he merely posted a single-line script that used awk and sed and grep - so I decided to build a better mousetrap ...

The requirement ... to query one's IBM Cloud account for Kubernetes (K8s) clusters, in this case leveraging the IBM Kubernetes Service (IKS) offering, and report the cluster name and the flavour ( flavor to our US friends )

Here is up with what I ended: -

for i in $(ic cs cluster ls --provider vpc-gen2 --output JSON | jq -r '.[].name'); do echo "Cluster Name:" $i; echo -n "Flavor: "; ic cs workers --cluster $i --output JSON | jq -r '.[].flavor'; done

which resulted in: -

Cluster Name: davehay-14042023

Flavor: bx2.4x16

Cluster Name: davehay-15042023

Flavor: bx2.4x16

In essence, I list the clusters in the account ( across all regions ), specifically those using the Virtual Private Cloud Generation 2 ( vpc-gen2 ) provider, grab and output the name, and then use the name to inspect the cluster and report on it's flavor ( sic ).

Rather than using awk and sed and grep etc. I chose to use jq, working on the assumption that all of our engineers will have that installed as it's a ubiquitous tool these days.

Tuesday, 4 April 2023

IBM Cloud CLI - Debugging

From this: -
we have some rather useful debugging env vars such as: -

- IBMCLOUD_API_KEY
- IBMCLOUD_TRACE
- IBMCLOUD_COLOR


etc.

TIL: FuzzyFinder - fzf - and jq

Today I learned ( well, actually it was yesterday but who's counting days ? ) about fzf in the context of using it to test JQ expressions.

The revelation came from Julia Evans, author of WizardZines, aka b0rk, about whom I've written before.

She'd mentioned the use of fzf and jq in a post on Mastodon: -


so I had to try it out ...

Install fzf

brew install fzf

Create a JSON document

cat << EOF > the_simpsons.json
[
    {
        "givenName": "Maggie",
        "familyName": "Simpson"
    },
    {
        "givenName": "Lisa",
        "familyName": "Simpson"
    },
    {
        "givenName": "Marge",
        "familyName": "Simpson"
    },
    {
        "givenName": "Homer",
        "familyName": "Simpson"
    },
    {
        "givenName": "Bart",
        "familyName": "Simpson"
    }
]
EOF

Fire up fzf

echo '' | fzf --preview 'jq {q} < the_simpsons.json'

Tinker with various jq queries




Nice!

Reminder - installing podman and skopeo on Ubuntu 22.04

This follows on from: - Lest I forget - how to install pip on Ubuntu I had reason to install podman  and skopeo  on an Ubuntu box: - lsb_rel...