Skip to content

Commit

Permalink
Add config generator and workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonhasacat committed Feb 7, 2024
1 parent 5f100fa commit d556663
Show file tree
Hide file tree
Showing 5 changed files with 1,287 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/generate-configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Generate ConfigMap
on: [push]

jobs:
push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v4
with:
go-version: '1.21.x'
- name: Build Generator
run: go build -o bin/generator ./generator/generator.go
- name: Generate ConfigMap
run: ./bin/generator
# Commit all changed files back to the repository
- uses: stefanzweifel/git-auto-commit-action@v4
id: auto_commit
with:
branch: main
70 changes: 70 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
)

type ConfigFile struct {
FileName string
Contents string
Lines []string
}

func main() {
err := generateConfigMap()
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
}
func generateConfigMap() error {
rootPath := "servers"
pfx := len(rootPath) + 1
tpl := template.Must(template.ParseFiles("generator/templates/configmap.yaml"))

var configFiles []ConfigFile

err := filepath.Walk(rootPath, func(path string, info os.FileInfo, e1 error) error {
if !info.IsDir() && strings.HasSuffix(path, ".yaml") {
if e1 != nil {
return e1
}

file, err := os.Open(path)
if err != nil {
return err
}
fileScanner := bufio.NewScanner(file)
fileScanner.Split(bufio.ScanLines)

var lines []string

for fileScanner.Scan() {
lines = append(lines, fileScanner.Text())
}

name := path[pfx:]

configFiles = append(configFiles, ConfigFile{
FileName: name,
Lines: lines,
})
}
return nil
})
if err != nil {
return err
}

f, err := os.OpenFile("manifest/configmap.yaml", os.O_WRONLY+os.O_TRUNC, os.ModeExclusive)
err = tpl.ExecuteTemplate(f, "configmap.yaml", configFiles)
if err != nil {
return err
}
return nil
}
11 changes: 11 additions & 0 deletions generator/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: discord-bot-v3-config
data:
{{- range . }}
{{.FileName}}: |
{{- range .Lines }}
{{ . }}
{{- end -}}
{{- end -}}
Loading

0 comments on commit d556663

Please sign in to comment.