Skip to content

Instantly share code, notes, and snippets.

Avatar

Steven Harman stevenharman

View GitHub Profile
@stevenharman
stevenharman / enable_sudo_touchid
Last active Aug 19, 2022
Enable TouchID for sudo permissions on macOS
View enable_sudo_touchid
#!/usr/bin/env bash
if grep 'pam_tid.so' /etc/pam.d/sudo --silent; then
echo "TouchID is already enabled ✅"
else
echo "Enabling TouchID for sudo 🪪";
sudo sed -i -e '1s;^;auth sufficient pam_tid.so\n;' /etc/pam.d/sudo
fi
@stevenharman
stevenharman / config.rb
Last active Apr 12, 2022
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...
View config.rb
module Example
class Config
def initialize(host_env: ENV, rails_env: Rails.env)
@host_env = host_env.to_hash
@rails_env = rails_env
end
def admin_qa_tooling_enabled?
coerce_boolean(host_env.fetch("ADMIN_QA_TOOLING", "false"))
end
@stevenharman
stevenharman / date_range.rb
Created Mar 4, 2022
A DateRange value object, for those times you need to work with a range (or span) of dates (or really... datetimes)!
View date_range.rb
# 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
stevenharman / add_rails_helper_to_files_with_grep_sed_xargs.sh
Last active Jan 21, 2022
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!
View add_rails_helper_to_files_with_grep_sed_xargs.sh
# 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
stevenharman / telemetry_initializer.rb
Last active Nov 23, 2021
A very thin wrapper for consistently configuring Ruby components for exporting OpenTelemetry tracing to Honeycomb
View telemetry_initializer.rb
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
stevenharman / Gemfile
Created Nov 20, 2021
A minimally functional OpenTelemetry setup to send traces to Honeycomb. This leaves a lot to be desired, but is a starting point.
View Gemfile
# Observability
gem "opentelemetry-sdk"
gem "opentelemetry-exporter-otlp"
gem "opentelemetry-instrumentation-all"
@stevenharman
stevenharman / gemfresh
Last active Apr 6, 2022
gemfresh: A handy script for determining the freshness of dependencies in a Ruby code base. Leverages bundler and libyear-bundler.
View gemfresh
#!/usr/bin/env bash
# shellcheck disable=SC2059
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
NO_COLOR='\033[0m'
CLEAR_LINE='\r\033[K'
@stevenharman
stevenharman / erb_now.rb
Last active Dec 16, 2020
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.
View erb_now.rb
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
stevenharman / restart-network-services
Created Jun 16, 2020
Restart Active Network Services on your Mac. You know, for the VPN!
View restart-network-services
#! /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.
@stevenharman
stevenharman / format-ruby
Created Mar 19, 2020
Run `standardrb --fix` as a git pre-commit hook. NOTE: this assumes `gem "standard"` is in your Gemfile
View format-ruby
#!/bin/sh
# This file is `.git/hooks/format-ruby` and it has been `chmod +x`'d
# Assumption: https://github.com/testdouble/standard is in your Gemfile
set -e
rubyfiles=$(git diff --cached --name-only --diff-filter=ACM "*.rb" "*.rake" "Gemfile" "Rakefile" | tr '\n' ' ')
[ -z "$rubyfiles" ] && exit 0
# Standardize all ruby files