71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"github.com/lucaslorentz/caddy-docker-proxy/v2/caddyfile"
|
|
"gopkg.in/yaml.v2"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
func generateConfig(labels map[string]string, upstreams string) []byte {
|
|
UpstreamsFunc := map[string]any{
|
|
"upstreams": func(upses ...any) string {
|
|
return upstreams
|
|
},
|
|
}
|
|
|
|
container, err := caddyfile.FromLabels(labels, nil, UpstreamsFunc)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return container.Marshal()
|
|
}
|
|
|
|
type DockerCompose struct {
|
|
Services map[string]Service `yaml:"services"`
|
|
}
|
|
|
|
type Service struct {
|
|
Labels map[string]string `yaml:"labels"`
|
|
}
|
|
|
|
func main() {
|
|
// Define command-line flag to accept docker-compose file path
|
|
composeFilePath := flag.String("compose-file", "docker-compose.yml", "Path to docker-compose file")
|
|
flag.Parse()
|
|
|
|
// Open the docker-compose file
|
|
file, err := os.Open(*composeFilePath)
|
|
if err != nil {
|
|
log.Fatalf("Failed to open docker-compose file: %v", err)
|
|
}
|
|
defer file.Close()
|
|
|
|
// Parse the docker-compose file into a struct
|
|
var compose DockerCompose
|
|
err = yaml.NewDecoder(file).Decode(&compose)
|
|
if err != nil {
|
|
log.Fatalf("Failed to parse docker-compose file: %v", err)
|
|
}
|
|
|
|
// Example: Mapping service labels to our desired format
|
|
labels := map[string]string{}
|
|
for serviceName, service := range compose.Services {
|
|
for labelKey, labelValue := range service.Labels {
|
|
// Modify this logic as needed to map labels to desired format
|
|
mappedKey := fmt.Sprintf("%s.%s", serviceName, labelKey)
|
|
labels[mappedKey] = labelValue
|
|
}
|
|
}
|
|
|
|
// Generate config with extracted labels and upstreams
|
|
upstreams := "localhost" // This could be passed as a cmd argument or dynamically set
|
|
config := generateConfig(labels, upstreams)
|
|
|
|
// Output the generated config
|
|
fmt.Print(string(config))
|
|
}
|