Skip to content

Instantly share code, notes, and snippets.

@s1moe2
s1moe2 / bresenham.go
Last active May 25, 2022 14:20
Golang implementation of Bresenham's line algorithm
package main
import (
"fmt"
"math"
"os"
"strconv"
)
func main() {
@s1moe2
s1moe2 / test.sh
Created April 23, 2022 14:29
Run Go tests in ephemeral Docker container
docker run --rm \
--name test-acme \
-v "$(pwd)":/usr/app \
golang:1.18 \
bash -c "cd /usr/app && go test -v ./..."
@s1moe2
s1moe2 / pool.yaml
Created February 4, 2022 11:05
CloudFormation - user pool & client setup sample
AWSTemplateFormatVersion: 2010-09-09
Parameters:
PoolName:
Type: String
PoolClientName:
Type: string
Resources:
Pool:
@s1moe2
s1moe2 / index.js
Created November 26, 2021 15:57
Basic usage of sessions in Express.js
const express = require("express")
const sessions = require("express-session")
const FileStore = require("session-file-store")(sessions)
const app = express()
app.use(sessions({
secret: "whatasecret",
saveUninitialized: false,
resave: false,
store: new FileStore({}),
@s1moe2
s1moe2 / replaceLines.go
Created March 21, 2021 18:03
Replace lines that start with a prefix in a file
func replaceImageInTemplate(filepath string, prefix string, newString string) error {
input, err := ioutil.ReadFile(filepath)
if err != nil {
return err
}
r, err := regexp.Compile(fmt.Sprintf("(?m)^%s(.*?)$", prefix))
if err != nil {
return err
}
@s1moe2
s1moe2 / replace.go
Created March 21, 2021 16:57
Replace placeholders in text file with Go
func ReplaceInTemplate(filepath string, origString string, replaceString string) error {
input, err := ioutil.ReadFile(filepath)
if err != nil {
return err
}
output := bytes.Replace(input, []byte(origString), []byte(replaceString), -1)
if err = ioutil.WriteFile(filepath, output, 0666); err != nil {
return err
@s1moe2
s1moe2 / removeFiles.go
Created March 21, 2021 16:57
Remove all files/directories from a directory 'dir'
func RemoveFiles(dir string) error {
d, err := os.Open(dir)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
@s1moe2
s1moe2 / findTag.go
Created February 9, 2021 16:51
Find commit hash tag by "latest" tag on same image
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"net/http"
"os"
@s1moe2
s1moe2 / clone.go
Last active February 14, 2024 06:01
Clone private GitHub repo with go-git
// SSHClone clones a private GitHub repository using SSH, in the directory passed as parameter
func SSHClone(dest string) error {
var publicKey *ssh.PublicKeys
sshKey, err := ioutil.ReadFile("/Users/youruser/.ssh/id_rsa")
if err != nil {
return err
}
publicKey, err = ssh.NewPublicKeys("git", sshKey, "")
if err != nil {
@s1moe2
s1moe2 / abstract.js
Last active November 9, 2020 09:43
JavaScript (sort of) Abstract Class
class EmailProvider {
#config = {}
constructor(config) {
this.#config = config
if (new.target === EmailProvider) {
throw new Error('Cannot construct EmailProvider instances directly')
}
if (this.send === EmailProvider.prototype.send) {