Skip to content

Instantly share code, notes, and snippets.

View rcook's full-sized avatar

Richard Cook rcook

View GitHub Profile
@rcook
rcook / main.rs
Created April 18, 2024 20:50
Show creation and modification times for files in a directory
use anyhow::Result;
use chrono::{DateTime, Local};
use std::collections::VecDeque;
use std::fs::{metadata, read_dir};
use std::iter::once;
use std::path::{Path, PathBuf};
use std::time::SystemTime;
fn main() -> Result<()> {
let mut queue =
@rcook
rcook / LICENSE
Last active December 24, 2023 00:41
Simulating packet loss with iptables/ipfw
The MIT License (MIT)
Copyright (c) 2010 Richard Cook
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
@rcook
rcook / LICENSE
Last active August 8, 2023 00:27
AWS via Haskell (part 1): DynamoDB
The MIT License (MIT)
Copyright (c) 2017 Richard Cook
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
@rcook
rcook / macros.rs
Created July 21, 2023 23:58
More Rust macros
use std::collections::HashMap;
macro_rules! from_map {
($map: ident, $obj: ident, $field: ident) => {
$obj.$field = $map.get(stringify!($field)).unwrap().to_string();
};
($map: ident, $obj: ident, $head: ident, $($tail: ident), +) => {
from_map!($map, $obj, $head);
from_map!($map, $obj, $($tail), +);
@rcook
rcook / lib.rs
Created May 21, 2023 19:54
Clippy settings
#![warn(clippy::all)]
#![warn(clippy::cargo)]
//#![warn(clippy::expect_used)]
#![warn(clippy::nursery)]
//#![warn(clippy::panic_in_result_fn)]
#![warn(clippy::pedantic)]
#![allow(clippy::derive_partial_eq_without_eq)]
#![allow(clippy::enum_glob_use)]
#![allow(clippy::match_wildcard_for_single_variants)]
#![allow(clippy::missing_errors_doc)]
@rcook
rcook / var_opt.rs
Last active May 9, 2023 17:03
Optionally get environment variable
fn var_opt<K>(key: K) -> Result<Option<String>>
where
K: AsRef<OsStr>,
{
use std::env::{var, VarError};
match var(key) {
Ok(value) => Ok(Some(value)),
Err(VarError::NotPresent) => Ok(None),
_ => bail!("environment variable not found"),
@rcook
rcook / AWSInfo.hs
Last active April 7, 2023 09:46
AWS via Haskell Part 2 (S3)
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RecordWildCards #-}
module AWSViaHaskell.AWSInfo
( AWSInfo(..)
, LoggingState(..)
, ServiceEndpoint(..)
, getAWSInfo
, withAWS
, withAWS'
@rcook
rcook / foo.ps1
Last active March 15, 2023 11:49
param(
[Parameter(Mandatory = $false, Position = 0, ValueFromRemainingArguments = $true)]
[object[]] $Arguments
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Show-Usage {
Write-Host -Object @"
@rcook
rcook / AWSService.hs
Last active May 23, 2022 09:14
AWS via Haskell Part 5 (Lambda)
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module AWSViaHaskell.AWSService
@rcook
rcook / compare_dicts.py
Created March 13, 2022 16:49
Compare dictionaries in Python
def dump_diffs(x, y, parts):
path = "/".join(parts)
if x is None:
if y is None:
pass
else:
print(f"{path}: x value {x} does not match y value {y}")
elif isinstance(x, int):
if isinstance(y, int):
if x == y: