~egtann/inv2config

83282f120a4cd0531393d47978a9d28e7f285216 — Evan Tann 9 months ago 7db3057
fix bug introducing incorrect services
2 files changed, 27 insertions(+), 28 deletions(-)

M README.md
M main.go
M README.md => README.md +1 -4
@@ 31,7 31,7 @@ Example service-based SRP config file:
To use:

```bash
inventory2config -i inventory.json -s config.json > config.json
inventory2config -i inventory.json -c config_service.json > config.json
```

This outputs a config.json with backends populated with the correct ip:port


@@ 57,6 57,3 @@ combinations like so:
```

SRP will ignore the Service and Port. They are added for this tool only.

In the event of an error, inventory2json will still output the original file to
stdout, so no data is lost.

M main.go => main.go +26 -24
@@ 5,7 5,6 @@ import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"sort"
	"strings"


@@ 14,38 13,27 @@ import (
)

func main() {
	// Always send to stdout our new SRP or the original version
	srpOut, err := run()
	fmt.Printf(srpOut)
	if err != nil {
	if err := run(); err != nil {
		fmt.Fprintln(os.Stderr, fmt.Sprintf("\nerror: %v", err))
		os.Exit(1)
	}
}

func run() (string, error) {
func run() error {
	// Read an inventory file and an SRP config file
	var (
		invFile = flag.String("i", "inventory.json", "inventory file")
		srpFile = flag.String("c", "config.json", "srp config file")
		srpFile = flag.String("c", "config_service.json", "srp config file")
	)
	flag.Parse()

	// Parse our SRP config file, but retain the original bytes in case any
	// error occurs
	srpByt, err := ioutil.ReadFile(*srpFile)
	conf, err := parseSRPConfig(*srpFile)
	if err != nil {
		return "", fmt.Errorf("read srp config: %w", err)
	}
	origSRP := string(srpByt)
	conf := srpConfig{}
	if err = json.Unmarshal(srpByt, &conf); err != nil {
		return origSRP, fmt.Errorf("decode srp config: %w", err)
		return fmt.Errorf("parse srp config: %w", err)
	}

	inventory, err := parseInventory(*invFile)
	if err != nil {
		return origSRP, fmt.Errorf("parse inventory: %w", err)
		return fmt.Errorf("parse inventory: %w", err)
	}

	// Convert our inventory into a data structure for fast lookups


@@ 58,7 46,7 @@ func run() (string, error) {

	// Update ip:port combos for each backend
	missingServices := serviceMap{}
	for _, backend := range conf.Services {
	for uri, backend := range conf.Services {
		service := backend.Service
		if service == "" {
			continue


@@ 76,21 64,35 @@ func run() (string, error) {
		}
		sort.Strings(ips)
		backend.Backends = append([]string{}, ips...)
		conf.Services[service] = backend
		conf.Services[uri] = backend
	}
	if len(missingServices) > 0 {
		err := fmt.Errorf("services undefined in inventory: %v",
		return fmt.Errorf("services undefined in inventory: %v",
			missingServices)
		return origSRP, err
	}

	buf := &bytes.Buffer{}
	enc := json.NewEncoder(buf)
	enc.SetIndent("", "\t")
	if err := enc.Encode(conf); err != nil {
		return origSRP, fmt.Errorf("encode config: %w", err)
		return fmt.Errorf("encode config: %w", err)
	}
	fmt.Print(string(buf.Bytes()))
	return nil
}

func parseSRPConfig(filename string) (*srpConfig, error) {
	fi, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer func() { _ = fi.Close() }()

	conf := &srpConfig{}
	if err = json.NewDecoder(fi).Decode(conf); err != nil {
		return nil, fmt.Errorf("decode: %w", err)
	}
	return string(buf.Bytes()), nil
	return conf, nil
}

func parseInventory(filename string) (up.Inventory, error) {