commit 040d3bbc106594c443e8844a562936125b9c717b Author: MrLetsplay2003 Date: Wed Jul 12 18:45:29 2023 +0000 initial commit diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..34962b0 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -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" +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..b17c42b --- /dev/null +++ b/build.sh @@ -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" diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..266e7f6 --- /dev/null +++ b/docker/Dockerfile @@ -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", "." ] diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..94d4ce8 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module docker_launcher + +go 1.20 diff --git a/main.go b/main.go new file mode 100644 index 0000000..6226dda --- /dev/null +++ b/main.go @@ -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) + } +}