Skip to content

Instantly share code, notes, and snippets.

Avatar

Nikolai Zujev jaymecd

View GitHub Profile
@jaymecd
jaymecd / relative_time.sh
Created May 6, 2021
relative time in shell (awk)
View relative_time.sh
#!/user/bin/env bash
# run as ./relative_time 2021-05-06T20:37:59Z
relative_time() {
declare -r dt="$1"
declare ts_then ts_now retval
! { ts1=$(date +%s -d "${dt}"); retval=$?; }
@jaymecd
jaymecd / GNU-Make.md
Created Feb 12, 2021 — forked from rueycheng/GNU-Make.md
GNU Make cheatsheet
View GNU-Make.md
@jaymecd
jaymecd / rounding_decimals.md
Created Jan 1, 2021 — forked from jackiekazil/rounding_decimals.md
How do I round to 2 decimals in python?
View rounding_decimals.md

How do I round to 2 decimals?

In python, you have floats and decimals that can be rounded. If you care about the accuracy of rounding, use decimal type. If you use floats, you will have issues with accuracy.

All the examples use demical types, except for the original value, which is automatically casted as a float.

To set the context of what we are working with, let's start with an original value.

Original Value

@jaymecd
jaymecd / parallel_async_python3_with_timeout.py
Created Dec 28, 2020
parallel async python3 with timeout
View parallel_async_python3_with_timeout.py
#!/usr/bin/env python3
import asyncio
from typing import List, Awaitable
async def slow_job(delay: int):
print(f"running slow_job with {delay}s delay ...")
await asyncio.sleep(delay)
print(f"slow_job with {delay}s delay is done")
@jaymecd
jaymecd / bash_cli_skeleton.sh
Last active Nov 4, 2020
Bash CLI skeleton + debug reader
View bash_cli_skeleton.sh
#!/usr/bin/env bash
#/ Sample command to perform update and query it's status
#/
#/ Usage: <SELF_NAME> [-h|--help] <command>
#/
#/ Available commands:
#/ one - perform some update, idempotent
#/ usage: <SELF_NAME> one <arg1> [arg2]
#/ two - query status of update
@jaymecd
jaymecd / main.go
Created Aug 18, 2020 — forked from pteich/main.go
Example for using go's sync.errgroup together with signal detection signal.Notify to stop all running goroutines
View main.go
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"time"
View s3.batch.operations.manifest.split.sh
#!/usr/bin/env bash
#
# Split single S3 Inventory manifest into sequential subsets.
#
# Usage:
# $ env INVENTORY_BUCKET=my-inventory INVENTORY_PATH=sample-name ./s3.batch.operations.manifest.split.sh
#
set -euo pipefail
@jaymecd
jaymecd / a_json_data_pivot_with_jq.md
Created Jun 2, 2020
This JQ one-liner to pivot JSON data
View a_json_data_pivot_with_jq.md

Pivot JSON data

This one-liner pivots data from group>users or user>groups:

$ cat incoming.json \
  | jq 'map(. as $in | .users[] | . as $u | {user:$u, group:$in.group}) | group_by(.user) | map({user:.[0].user, groups: map(.group)})'

[
  {
@jaymecd
jaymecd / Makefile
Last active Mar 18, 2020
Default Makefile with envvar guard
View Makefile
MAKEFLAGS += --warn-undefined-variables
SHELL := bash -o pipefail -c
.DEFAULT_GOAL := help
.PHONY: help all deps build
guardEnvVar = $(if $(value $(1)),,$(error env $(1) not defined))
# Note: help extracts title from ## comment just above the target, builds target/title grid and prints it pretty.
## Show help
@jaymecd
jaymecd / build_stig_windows_with_packer.md
Last active Jul 5, 2021
Packer with WinRM over HTTPS
View build_stig_windows_with_packer.md

Way to build Windows STIG/CIS hardened AMI on AWS.

Problem is that WinRM Basic authentication is blocked by GroupPolicy.

Therefore it's required to setup WinRM over HTTPS.