Skip to content

Instantly share code, notes, and snippets.

View nebez's full-sized avatar

Nebez Briefkani nebez

View GitHub Profile
@nebez
nebez / command-not-found.sh
Last active May 15, 2023 15:00 — forked from InternetUnexplorer/command-not-found.sh
Improved home-manager nix-locate configuration, with an auto-updated index and a better command-not-found handler
# Adapted from https://github.com/bennofs/nix-index/blob/master/command-not-found.sh
command_not_found_handle () {
if [ -n "${MC_SID-}" ] || ! [ -t 1 ]; then
>&2 echo "$1: command not found"
return 127
fi
echo -n "searching nix-index..."
ATTRS=$(@nix-locate@ --minimal --no-group --type x --type s --top-level --whole-name --at-root "/bin/$1")

Bash best practices and style-guide

Just simple methods to keep the code clean.

Inspired by progrium/bashstyle and Kfir Lavi post.

Quick big rules

  • All code goes in a function
  • Always double quote variables
interface Circle {
type: 'circle';
radius: number;
}
interface Square {
type: 'square';
width: number;
}
@nebez
nebez / env-test.ts
Created September 17, 2018 17:59
Environment variable loading tests
import { env, MissingEnvironmentVariableError, InvalidEnvironmentVariableError } from 'src/Application/env';
describe('Environment Variable Loader', () => {
const originalProcessEnv = Object.assign({}, process.env);
afterEach(() => {
process.env = originalProcessEnv;
});
describe('Default values with non-existent env vars', () => {
import { expect } from 'chai';
import * as Str from 'src/Support/String';
const app: any = {};
const otherMumboJumbo: any = {};
const validStatusCode = 200;
const invalidStatusCode = 500;
describe('String utility', () => {
describe('slugify', () => {
@nebez
nebez / config.ts
Created September 4, 2018 14:52
Type-safe config loading
// TODO: We need an overload for a default value so it doesn't throw
function env(name: string): string;
function env<T>(name: string, transformer: (dirty: string) => T): T;
function env<T>(name: string, transformer?: (dirty: string) => T): T | string {
const value = process.env[name];
if (value == undefined) {
// todo: should null be allowed? it might be a valid value for some
// consumers instead of defaulting to an empty string.
throw new Error(`Missing environment variable: ${name}`);
@nebez
nebez / express.d.ts
Last active August 17, 2018 01:39
Make Express Great Again
import * as express from 'express';
declare module 'express' {
interface Request {
body: unknown;
cookies: Record<string, unknown>;
params: Record<string, unknown>;
query: Record<string, unknown>;
route: Record<string, unknown>;
signedCookies: Record<string, unknown>;
interface Person {
name: string;
spent: number;
}
class Trip {
private people: Person[];
constructor() {
this.people = [];
@nebez
nebez / nginx
Created February 17, 2016 15:13 — forked from vdel26/nginx
Openresty init.d script
#!/bin/sh
#
# chkconfig: 2345 55 25
# Description: Nginx init.d script, put in /etc/init.d, chmod +x /etc/init.d/nginx
# For Debian, run: update-rc.d -f nginx defaults
# For CentOS, run: chkconfig --add nginx
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all