Skip to content

Instantly share code, notes, and snippets.

View dominicbartl's full-sized avatar

Dominic Bartl dominicbartl

View GitHub Profile
@dominicbartl
dominicbartl / write-secrest.js
Created March 18, 2022 08:34
Fetch secrets from GCP Secret Manager and write them to an env file
#!/usr/bin/env node
const {SECRETS} = require('../build/dist/functions/secrets');
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
const {join} = require('path');
const {writeFileSync} = require('fs')
const client = new SecretManagerServiceClient();
const projectId = process.argv[2];
const outputFile = join(__dirname, '..', `.env.${projectId}`);
@dominicbartl
dominicbartl / logger.middleware.ts
Last active January 26, 2022 13:33
Express middleware to log HTTP requests on Firebase functions
import { NextFunction, Request, Response } from 'express';
import { LogEntry, LogSeverity, write } from 'firebase-functions/lib/logger';
export function functionRequestLogger(req: Request, res: Response, next: NextFunction) {
const start = Date.now();
res.on('finish', () => {
const code = res.statusCode;
const logEntry: LogEntry = {
severity: getSeverity(code),
httpRequest: {
@dominicbartl
dominicbartl / promise_utils.ts
Last active January 20, 2020 13:50
Process a batch of promises in parallel
/**
* Processes a batch of items in parallel using a handler function to generate the promises. It starts by initializing
* the amount of Promises given by the batchSize parameter. Once a Promise finishes it immediately starts the next one.
* The function returns the results in an array in the order in which the promises finished.
*
* The optional onUpdate function is called every time a new Promises starts or is finished
*/
export async function processPromisesParallel<T, K>(
items: T[],
batchSize: number,
@dominicbartl
dominicbartl / index.js
Last active November 25, 2017 09:23
A list of countries with name, currency and info if they're in the EU
const countries = require('world-countries/countries');
const EU = ["AT","BE","BG","CY","CZ","DE","DK","ES","EE","FI","FR","GB","GR","HR","HU","IE","IT","LT","LU","LV","MT","NL","PL","PT","RO","SK","SI","SE"];
let mapped = countries.map((country) => {
return {
cca2: country.cca2,
ccn3: country.ccn3,
cca3: country.cca3,
currency: country.currency,
name: country.name.common,
@dominicbartl
dominicbartl / SeekBar.java
Created August 25, 2015 14:03
This view is not finished
package de.contentfleet.corporatecampus.ui.widget;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
@dominicbartl
dominicbartl / ServerUtil.java
Created September 30, 2014 16:30
Inject dynamic host IP address in with Gradle
private static final String HOST_EMULATOR = "10.0.2.2";
private static final String HOST_PRODUCTION = "example.com";
public static String getHost() {
if (BuildConfig.DEBUG) {
return (Build.PRODUCT).contains("sdk") ? HOST_EMULATOR : BuildConfig.LOCAL_IP;
}
return HOST_PRODUCTION;
}
@dominicbartl
dominicbartl / TypedTextView.java
Last active August 29, 2015 14:04
Android TextView with font set by XML
package com.playquickly.ui.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
import com.playquickly.R;
@dominicbartl
dominicbartl / ps1.sh
Last active December 29, 2015 06:59
A colorful prompt which displays the time of your last command, the working directory, the git branch and status characters
function timer_start {
timer=${timer:-$(date +%s%N)/1000000}
}
function timer_stop {
millis=$(($(date +%s%N)/1000000 - $timer))
if [[ $millis -ge 1000 ]] ; then
timer_mout="$(($millis/1000))"s ;
else
timer_mout="$millis"ms ;
<snippet>
<content><![CDATA[
(function ($1){
$SELECTION$2
})($1);
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>closure</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.js</scope>
@dominicbartl
dominicbartl / custom-eventemitter1.js
Last active March 3, 2018 21:23
2 ways to inherit from EventEmitter in Node.JS
var EventEmitter = require('events').EventEmitter;
module.exports = new EventEmitter();
exports.emitSomethingLater = function()
setTimeout(function() {
module.exports.emit('something');
}, 1000);
}