Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Making an impact

Mark McDonnell Integralist

🎯
Making an impact
View GitHub Profile
@Integralist
Integralist / How to ignore root file but not a sub directory of the same name.md
Last active Jul 15, 2022
[How to ignore root file but not a sub directory of the same name] #git #ignore
View How to ignore root file but not a sub directory of the same name.md

Imagine you have the following tree structure:

.
├── cmd
│   └── fastly
├── fastly

You want to avoid commiting the fastly file in the root, but you're OK with cmd/fastly being committed.

@Integralist
Integralist / main.go
Last active Jun 30, 2022
[Go file lock] #go #golang #flock
View main.go
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/gofrs/flock"
)
@Integralist
Integralist / main.go
Last active Jun 30, 2022
[Go retry] #go #golang #retry
View main.go
package main
import (
"context"
"fmt"
"time"
"github.com/sethvargo/go-retry"
)
@Integralist
Integralist / partial.js
Created Jun 28, 2022
[Simple JavaScript partial application function]
View partial.js
export const partial = (fn, arg) => {
return (...rest) => {
return fn(arg, ...rest)
}
}
@Integralist
Integralist / 1. GOOD main.go
Last active Jun 23, 2022
[Go remove cookie from http.Request] #go #golang #http #cookie
View 1. GOOD main.go
// For proxy situations where you want to strip a cookie from the incoming request at the proxy layer, before proxying it onto the actual backend.
package main
import (
"fmt"
"net/http"
)
func main() {
@Integralist
Integralist / cli.md
Last active Jun 22, 2022
[Perl add language to markdown code block] #perl #regex #codeblock #markdown
View cli.md
# Ensure all code blocks without a language get 'plain' appended.
# NOTE: constraint is that there needs to be an empty line preceding the code block.
cat file.mdx | perl -pe 'BEGIN { undef $/ }; s/(?<=^\n)(```)(\n.+?```)/\1plain\2/s'

NOTE: The Perl variable $/ stores the line ending, used for processing files line-by-line. By calling undef on it, we cause Perl to slurp the entire file all in one string at once for us to process.

> WARNING: If the code block has a non-empty preceding line, and the contents of the code block has an empty line before the closing code fence, then the current pattern will accidentally match. So if that's the case, we need to ensure all code blocks don't have an unnecessary empty line at the bottom of them.

@Integralist
Integralist / main.rs
Created Jun 20, 2022
[Rust flatten vector of Results] #rust
View main.rs
use std::result::Result::{Ok, Err};
fn main() {
let v = vec![Ok("foo"), Ok("bar"), Err("whoops"), Ok("baz")]; // Err should be skipped
for r in v.into_iter().flatten() {
println!("{:#?}", r);
}
}
/*
@Integralist
Integralist / rust smart pointers.md
Last active Jun 16, 2022
[Rust Smart Pointers] #rust
View rust smart pointers.md

Summary

  • Box<T>: A pointer type for heap allocation.
  • Rc<T>: A read-only, single-threaded reference-counting (i.e. multiple owners) pointer Ω.
  • Arc<T>: A read-only, thread-safe (i.e. extra performance overhead) reference-counting (i.e. multiple owners) pointer †.
  • Cell<T>: A single-threaded mutable memory location (where values are moved in and out of the cell).
  • RefCell<T>: A single-threaded mutable memory location with dynamically checked (i.e. at runtime) borrow rules.

Ω Wrap the value inside the Rc with either Cell<T> or RefCell<T> for mutability.
> † Wrap the value inside the Arc with either Mutex, RwLock or one of the Atomic* types for mutability.

@Integralist
Integralist / main.rs
Last active Jun 15, 2022
[Rust Tokio Spawn and Retry] #rust #async #retry
View main.rs
use std::any::type_name;
use tokio_retry::strategy::{jitter, FixedInterval};
use tokio_retry::Retry;
#[derive(Debug)]
struct ImageId {
id: Option<String>,
}
async fn action() -> Result<ImageId, ()> {
@Integralist
Integralist / Terraform Debugging.md
Created Jun 14, 2022
[Terraform Debugging] #terraform #debug #log
View Terraform Debugging.md

https://www.terraform.io/internals/debugging

  • TF_LOG: TRACE, DEBUG, INFO, WARN or ERROR (or JSON which is JSON formatted TRACE logs).
  • TF_LOG_CORE: only terraform logs.
  • TF_LOG_PROVIDER: only provider logs.
  • TF_LOG_PATH: set along with one of the above to ensure logs are sent to a file.