Skip to content

Instantly share code, notes, and snippets.

@neomantra
neomantra / pointerValueMap.go
Created March 31, 2023 13:31
NewPointerValueMap converts a map[string]*T to a map[string]T, omitting nil pointers.
// NewPointerValueMap converts a map[string]*T to a map[string]T, omitting nil pointers.
func NewPointerValueMap[K comparable, V any](src map[K]*V) map[K]V {
dst := make(map[K]V)
for k, ptr := range src {
if ptr != nil {
dst[k] = *ptr
}
}
return dst
}
@neomantra
neomantra / directory.go
Created May 21, 2024 22:45
VerifyDirectoryExists
func VerifyDirectoryExists(dirPath string) error {
if fileInfo, err := os.Stat(dirPath); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("directory '%s' does not exist", dirPath)
} else {
return fmt.Errorf("error filestat on '%s': %w", dirPath, err)
}
} else if !fileInfo.IsDir() {
return fmt.Errorf("'%s' exists but is not a directory", dirPath)
}
@neomantra
neomantra / compressed_reader_writer.go
Created May 28, 2024 20:14
Compression wrappers for io.Reader and io.Writer
// Copyright (c) 2024 Neomantra BV
//
// Opinionated Reader/Writer wrappers
package nmio
import (
"compress/gzip"
"io"
"os"
@neomantra
neomantra / grafana-pdc.nomad.tpl
Last active July 12, 2024 19:44
pdc-agent with HashiCorp Nomad
# Grafana PDC -- private network tunnel
# This is templated by Terraform templatefile()
# https://grafana.com/docs/grafana-cloud/connect-externally-hosted/private-data-source-connect/configure-pdc/
job "grafana-pdc" {
region = "global"
datacenters = ["${NOMAD_DATACENTER}"]
namespace = "${NOMAD_NAMESPACE}"
type = "service"
events {
worker_connections 4096; ## Default: 1024
}
http {
# DEBUG:
map $uri $proxy_uri_base {
"~/crypto-proxy/(?<proxy_base>[^/]*)" $proxy_base;
default $uri;
}