Skip to content

Instantly share code, notes, and snippets.

@SCP002
SCP002 / Start-ProcessExt.psm1
Last active September 15, 2024 11:36
PowerShell: Start-Process cmdlet, but with support of -CaptureOutput flag.
#Requires -Version 7.4
#Requires -PSEdition Core
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
<#
.SYNOPSIS
The same as Start-Process but with support of -CaptureOutput parameter.
@SCP002
SCP002 / build.sh
Created July 28, 2024 14:49
Golang: Shell script to build your project to each OS / Architecture specified.
#!/bin/bash
# Tested with go 1.22.5
project_name="my_project" # Change to your project name
main_file="${project_name}.go" # Or main.go, depends on your project structure
build_path="./build"
# Add values to your liking from "go tool dist list" command
# or https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63
@SCP002
SCP002 / minecraft-server-docker-howto.md
Last active July 27, 2024 12:03
Minecraft: Run fabric server with mods in docker container.

Create minecraft/docker-compose.yml:

services:
  minecraft:
    image: itzg/minecraft-server
    container_name: minecraft
    environment:
      TYPE: FABRIC
 SERVER_NAME: Dungeon
@SCP002
SCP002 / process_new_console_example.rs
Created November 29, 2023 18:48
Rust: Start windows process in new console using CREATE_NEW_CONSOLE creation flag.
// Add to your Cargo.toml:
// [dependencies]
// subprocess = { version = "0.2.10", git = "https://github.com/SCP002/rust-subprocess.git" }
use subprocess::Exec;
fn main() {
let exit_status = Exec::cmd("my-executable-name")
.arg("arg1")
.creation_flags(0x00000010) // CREATE_NEW_CONSOLE
@SCP002
SCP002 / process_example.rs
Created November 29, 2023 18:34
Rust: Start process. Keep StdOut and StdErr in the original order. Display output real time character by character. Capture output on exit.
// Add to your Cargo.toml:
// [dependencies]
// utf8-chars = "1.0.0"
// subprocess = { version = "0.2.10", git = "https://github.com/SCP002/rust-subprocess.git" }
use std::io;
use std::io::BufReader;
use std::io::Write;
use subprocess::Exec;
use subprocess::ExitStatus;
@SCP002
SCP002 / filter.rs
Last active November 27, 2023 08:12
Rust: concurrent filter example. Keeps empty elements and original order. Using thread pool.
// Add to your Cargo.toml:
// [dependencies]
// rayon = "1.1"
use rayon::prelude::*;
use rayon::ThreadPoolBuilder;
use std::thread;
use std::time::{Duration, Instant};
fn main() {
@SCP002
SCP002 / json.rs
Last active November 24, 2023 12:43
Rust: Serialize & Deserialize partially known / dynamic JSON. Using structs (type safe).
// Add to your Cargo.toml:
// [dependencies]
// serde = { version = "1.0.193", features = ["derive"] }
// serde_json = "1.0.108"
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
#[derive(Serialize, Deserialize)]
struct TopStruct {
@SCP002
SCP002 / setup.sh
Created July 18, 2023 02:08
Example of running two Cesbo Astra instances on two different ports as systemctl services.
# write systemd config for the first instance
sudo tee /lib/systemd/system/astra-8000.service > /dev/null << ENDSEQ
[Unit]
Description=Astra 8000 service (%N)
After=network-online.target
StartLimitBurst=5
StartLimitIntervalSec=10
[Service]
Type=simple
@SCP002
SCP002 / err.go
Last active December 1, 2023 19:56
Golang: Run both HTTP and HTTPS server with dummy TLS certificates for testing. Network error classifier included.
package main
import (
"errors"
"net"
"net/url"
"syscall"
)
// ErrType represents network error type
@SCP002
SCP002 / write_to_stdin_posix.go
Last active March 15, 2022 22:00
Golang: Write a message to any console's input on POSIX.
// Used in https://github.com/SCP002/terminator.
// For Windows, see: https://gist.github.com/SCP002/1b7fd91a519a2dc60fc5b179f90472b6.
package main
import (
"fmt"
"os"
"syscall"
"unsafe"