Skip to content

Instantly share code, notes, and snippets.

View stevenharman's full-sized avatar

Steven Harman stevenharman

View GitHub Profile
stevenharman / .env
Last active September 7, 2023 15:56
Bug with ENV loading in Heroku CLI (dotenv, .env, .env.local, etc…)
BASE_ENV_VAR="value for base"
# Set a value specific to you in your .env.local
stevenharman / enable_sudo_touchid
Last active August 19, 2022 14:55
Enable TouchID for sudo permissions on macOS
#!/usr/bin/env bash
if grep '' /etc/pam.d/sudo --silent; then
echo "TouchID is already enabled ✅"
echo "Enabling TouchID for sudo 🪪";
sudo sed -i -e '1s;^;auth sufficient\n;' /etc/pam.d/sudo
stevenharman / config.rb
Last active April 12, 2022 02:19
An example of an app-specific configuration wrapper around ENV and Rails.env. This consolidates access to config vars, while providing a seam for adding conditional logic, data munging, type coercion, etc...
module Example
class Config
def initialize(host_env: ENV, rails_env: Rails.env)
@host_env = host_env.to_hash
@rails_env = rails_env
def admin_qa_tooling_enabled?
coerce_boolean(host_env.fetch("ADMIN_QA_TOOLING", "false"))
stevenharman / date_range.rb
Created March 4, 2022 15:03
A DateRange value object, for those times you need to work with a range (or span) of dates (or really... datetimes)!
# frozen_string_literal: true
require 'active_support'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/time'
class DateRange
attr_reader :start, :stop
def initialize(start: nil, stop: nil)
stevenharman /
Last active January 21, 2022 17:31
Add a line of text to a file ONLY if the file doesn't already contain the line. Oh, but first append a line to the start of every file without that line. grep, sed, and xargs, folks!
# So… uuuh… what had happend was - I'd changed our `.rspec` config so that we
# could have isolated specs. The only real change was to not auto-require
# `rails_helper` in the `.rspec` config. Instead we auto-require `spec_helper`,
# and then files that need `rails_helper` (to boot rails) will have to add
# `require 'rails_helper'`.
# I didn’t think much of it b/c I’d noted that the specs I’d been looking at
# were already doing `require 'rails_helper'`, so it should be a no-op for
# them. It turns out, 700 or so other spec files ARE NOT already requiring
# `rails_helper`. So… what does one do when confronted with needing to in-place
stevenharman / telemetry_initializer.rb
Last active November 23, 2021 21:05
A very thin wrapper for consistently configuring Ruby components for exporting OpenTelemetry tracing to Honeycomb
Telemetry.configure do |c|
c.component = ENV.fetch("COMPONENT_NAME")
c.environment = ENV.fetch("APP_ENV")
c.deployment_environment = ENV.fetch("DEPLOYMENT_ENVIRONMENT")
c.honeycomb_api_key = ENV.fetch("HONEYCOMB_API_KEY", nil)
c.honeycomb_dataset = ENV.fetch("HONEYCOMB_DATASET", nil)
# This is delegating through to `OpenTelemetry::SDK::Configurator#use_all`
c.use_all("OpenTelemetry::Instrumentation::Sidekiq" => {span_naming: :job_class})
stevenharman / Gemfile
Created November 20, 2021 03:02
A minimally functional OpenTelemetry setup to send traces to Honeycomb. This leaves a lot to be desired, but is a starting point.
# Observability
gem "opentelemetry-sdk"
gem "opentelemetry-exporter-otlp"
gem "opentelemetry-instrumentation-all"
stevenharman / gemfresh
Last active December 29, 2023 16:05
gemfresh: A handy script for determining the freshness of dependencies in a Ruby code base. Leverages bundler and libyear-bundler.
#!/usr/bin/env bash
# shellcheck disable=SC2059
set -euo pipefail
stevenharman / erb_now.rb
Last active December 16, 2020 17:28
Render Ruby ERB templates, inline, with variables bound and available local variables in the template. ✨ This technique is useful for rendering test fixtures via ERB, for example.
require "ostruct"
# A wrapper around a single ERB template that will bind the given Hash of values,
# making them available as local variables w/in the template.
class ErbNow < OpenStruct
# Render a given ERB +template+ string, binding the entries in the given +attrs+ Hash
# as local variables in the template, with the key as the variable name.
# @example
stevenharman / restart-network-services
Created June 16, 2020 16:25
Restart Active Network Services on your Mac. You know, for the VPN!
#! /usr/bin/env bash
# shellcheck disable=SC2059
set -euo pipefail
# Toggle all currently 'Active' network servcies (e.g., Wi-Fi, Ethernet
# connections, etc...) to "restart" them. We'll ignore any already 'Disabled'
# services, and toggle all of the others to 'Disabled' and then back to
# 'Enabled'. This has been found helpful when your VPN won't re-connect after
# undocking and re-docking your MacBook, for example.