Skip to content

Instantly share code, notes, and snippets.

@podhmo
Last active Feb 19, 2020
Embed
What would you like to do?
--- 00config/output.txt 2020-02-19 20:01:01.000000000 +0900
+++ 01overwrite-config/output.txt 2020-02-19 20:01:01.000000000 +0900
@@ -1,10 +1,10 @@
-conf.Config{
+&conf.Config{
Version: "3.7",
Services: conf.Services{
Wordpress: conf.Wordpress{
Image: "wordpress",
Ports: []string{
- "8080:80",
+ "9090:80",
},
Networks: []string{
"overlay",
@@ -25,7 +25,7 @@
},
Deploy: conf.Deploy{
Mode: "replicated",
- Replicas: 2,
+ Replicas: 5,
EndpointMode: "dnsrr",
},
},
package conf
// https://docs.docker.com/compose/compose-file/
// https://mholt.github.io/json-to-go/
type Config struct {
Version string `json:"version"`
Services Services `json:"services"`
Volumes Volumes `json:"volumes"`
Networks Networks `json:"networks"`
}
type Deploy struct {
Mode string `json:"mode"`
Replicas int `json:"replicas"`
EndpointMode string `json:"endpoint_mode"`
}
type Wordpress struct {
Image string `json:"image"`
Ports []string `json:"ports"`
Networks []string `json:"networks"`
Deploy Deploy `json:"deploy"`
}
type Mysql struct {
Image string `json:"image"`
Volumes []string `json:"volumes"`
Networks []string `json:"networks"`
Deploy Deploy `json:"deploy"`
}
type Services struct {
Wordpress Wordpress `json:"wordpress"`
Mysql Mysql `json:"mysql"`
}
type Volumes struct {
DbData interface{} `json:"db-data"`
}
type Networks struct {
Overlay interface{} `json:"overlay"`
}
{
"version": "3.7",
"services": {
"wordpress": {
"image": "wordpress",
"ports": [
"8080:80"
],
"networks": [
"overlay"
],
"deploy": {
"mode": "replicated",
"replicas": 2,
"endpoint_mode": "vip"
}
},
"mysql": {
"image": "mysql",
"volumes": [
"db-data:/var/lib/mysql/data"
],
"networks": [
"overlay"
],
"deploy": {
"mode": "replicated",
"replicas": 2,
"endpoint_mode": "dnsrr"
}
}
},
"volumes": {
"db-data": null
},
"networks": {
"overlay": null
}
}
module m
go 1.13
require (
github.com/imdario/mergo v0.3.8 // indirect
github.com/k0kubun/pp v3.0.1+incompatible // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
github.com/imdario/mergo v0.3.8 h1:CGgOkSJeqMRmt0D9XLWExdT4m4F1vd3FV3VPt+0VxkQ=
github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
package main
import (
"encoding/json"
"fmt"
"log"
"m/conf"
"os"
"github.com/imdario/mergo"
"github.com/k0kubun/pp"
"github.com/spf13/pflag"
)
// JSONLoadFile ...
func JSONLoadFile(filename string, c interface{}) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
decoder := json.NewDecoder(f)
return decoder.Decode(c)
}
// LoadConfig ...
func LoadConfig(filename string) (*conf.Config, error) {
var c conf.Config
if err := JSONLoadFile(filename, &c); err != nil {
return nil, err
}
overwritefile := os.Getenv("OVERWRITE_CONFIG")
if overwritefile == "" {
return &c, nil
}
fmt.Fprintf(os.Stderr, "***** OVERWRITE CONFIG by %q *****\n", overwritefile)
var c2 conf.Config
if err := JSONLoadFile(overwritefile, &c2); err != nil {
return &c, err
}
if err := mergo.Merge(&c2, &c); err != nil {
return &c, err
}
return &c2, nil
}
var (
// Cmd ...
Cmd = pflag.CommandLine
config = Cmd.String("config", "", "config")
)
func main() {
Cmd.Parse(os.Args[1:])
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
config := *config
c, err := LoadConfig(config)
if err != nil {
return err
}
pp.ColoringEnabled = false
pp.Println(c)
return nil
}
00:
go run $(shell echo $@*/)main.go --config config.json | tee $(shell echo $@*/)output.txt
# overwrite
01:
OVERWRITE_CONFIG=overwrite.json go run $(shell echo $@*/)main.go --config config.json | tee $(shell echo $@*/)output.txt
02:
diff -u $(shell echo 00*/)output.txt $(shell echo 01*/)output.txt > $@.diff || exit 0
# config (python)
03:
python $(shell echo $@*/)main.py --config config.json | tee $(shell echo $@*/)output.txt
# overwrite (python)
04:
OVERWRITE_CONFIG=overwrite.json python $(shell echo $@*/)main.py --config config.json | tee $(shell echo $@*/)output.txt
05:
diff -u $(shell echo 03*/)output.txt $(shell echo 04*/)output.txt > $@.diff || exit 0
&conf.Config{
Version: "3.7",
Services: conf.Services{
Wordpress: conf.Wordpress{
Image: "wordpress",
Ports: []string{
"9090:80",
},
Networks: []string{
"overlay",
},
Deploy: conf.Deploy{
Mode: "replicated",
Replicas: 2,
EndpointMode: "vip",
},
},
Mysql: conf.Mysql{
Image: "mysql",
Volumes: []string{
"db-data:/var/lib/mysql/data",
},
Networks: []string{
"overlay",
},
Deploy: conf.Deploy{
Mode: "replicated",
Replicas: 5,
EndpointMode: "dnsrr",
},
},
},
Volumes: conf.Volumes{
DbData: nil,
},
Networks: conf.Networks{
Overlay: nil,
},
}
{
"services": {
"wordpress": {
"ports": [
"9090:80"
]
},
"mysql": {
"deploy": {
"replicas": 5
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment