initial commit

This commit is contained in:
MrLetsplay 2023-07-12 18:45:29 +00:00
commit 040d3bbc10
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
6 changed files with 157 additions and 0 deletions

View File

@ -0,0 +1,22 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/go
{
"name": "Go",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/go:0-1-bullseye"
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "go version",
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build/

15
build.sh Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh
userID=$(id -u)
groupID=$(id -g)
mkdir -p ./build
echo "Building Docker image"
imageID=$(docker build -f docker/Dockerfile --build-arg UID="${userID}" --build-arg GID="${groupID}" -q .)
echo "Running build"
docker run --rm -v ./build:/build "$imageID"
echo "Removing image"
docker image rm "$imageID"

17
docker/Dockerfile Normal file
View File

@ -0,0 +1,17 @@
FROM golang:latest
ARG UID=1000
ARG GID=1000
RUN addgroup --gid ${GID} bobthebuilder && adduser --gid ${GID} --uid ${UID} bobthebuilder
RUN mkdir /build
RUN chown -Rv bobthebuilder:bobthebuilder /build
COPY . /workspace
RUN chown -Rv bobthebuilder:bobthebuilder /workspace
USER bobthebuilder
WORKDIR /workspace
CMD [ "go", "build", "-o", "/build/docker_launcher", "." ]

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module docker_launcher
go 1.20

99
main.go Normal file
View File

@ -0,0 +1,99 @@
package main
import (
"encoding/json"
"flag"
"log"
"os"
"regexp"
)
type config struct {
Files []string
VariableRegex string
RegexGroup int
}
var defaultVariableRegex = `\$\{([a-zA-Z0-9_-]+)\}`
func loadConfig(path string) (*config, error) {
raw, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var config config
err = json.Unmarshal(raw, &config)
if err != nil {
return nil, err
}
return &config, nil
}
func filterFile(path string, regex regexp.Regexp, regexGroup int) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
text := string(data)
text = regex.ReplaceAllStringFunc(text, func(s string) string {
matches := regex.FindStringSubmatch(text)
log.Println(matches)
return os.Getenv(matches[regexGroup])
})
log.Println(text)
return nil
}
func filterFiles(config config) error {
var regexString string
if len(config.VariableRegex) == 0 {
regexString = defaultVariableRegex
} else {
regexString = config.VariableRegex
}
var regexGroup int
if config.RegexGroup == 0 {
regexGroup = 1
} else {
regexGroup = config.RegexGroup
}
log.Println("Regex is:", regexString)
regex, err := regexp.Compile(regexString)
if err != nil {
return err
}
for _, file := range config.Files {
log.Println("Filtering file:", file)
err := filterFile(file, *regex, regexGroup)
if err != nil {
return err
}
}
return nil
}
func main() {
configPath := flag.String("config", "config.json", "Path to config file")
flag.Parse()
config, err := loadConfig(*configPath)
if err != nil {
log.Fatalln("Failed to load config:", err)
}
err = filterFiles(*config)
if err != nil {
log.Fatalln(err)
}
}