Skip to content

Instantly share code, notes, and snippets.

View vladfr's full-sized avatar

Vlad Fratila vladfr

View GitHub Profile
@vladfr
vladfr / go_defer_order_explained.go
Last active February 6, 2022 11:18
Go defer order explained
// This example shows how Go executes deferred statements in relation to a return
//
// This will output:
//
// Setting string to My first program says:
// Setting string to 3. Hello
//Setting string to 2. world
// Setting string to 1. from Deferred Go
// In main(), deferred() returned 3. Hello
// but str is now 1. from Deferred Go
func (a *IDPConfig) AuthUnaryInterceptor(authFunc AuthFunc) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
meta, ok := metadata.FromIncomingContext(ctx)
// [...]
token, err := a.validateToken(ctx, rawToken)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "invalid auth token: %v", err)
}
@vladfr
vladfr / completion.sh
Created May 26, 2020 18:16
kubectl shell completion
## Bash - OSX
# Get bash-completion going
$ brew install bash-completion
$ echo "[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion" >> ~/.bash_profile
# Put this in ~/.bash_profile
source <(kubectl completion bash)
alias k=kubectl
complete -F __start_kubectl k
@vladfr
vladfr / 1-standard.js
Last active May 9, 2023 07:34
Use async/await and for..of in Cloud Firestore
// In a Firestore standard example, we quickly create a 'xmas tree' of nested stuff
// We use Promises directly: get().then(callback) and use snapshot.forEach() to iterate
let campaignsRef = db.collection('campaigns');
let activeCampaigns = campaignsRef.where('active', '==', true).select().get()
.then(snapshot => {
snapshot.forEach(campaign => {
console.log(campaign.id);
let allTasks = campaignsRef.doc(campaign.id).collection('tasks').get().then(
snapshot => {
snapshot.forEach(task => {