File: main.go

package info (click to toggle)
golang-github-containers-common 0.64.2%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,528 kB
  • sloc: makefile: 130; sh: 102
file content (69 lines) | stat: -rw-r--r-- 1,354 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/containers/common/libnetwork/types"
)

type Error struct {
	Msg string `json:"error"`
}

func errAndExit(msg string) {
	fmt.Fprintln(os.Stderr, msg)
	os.Exit(1)
}

func main() {
	if len(os.Args) < 2 {
		errAndExit("netavark testplugin: argument required: [create|setup|teardown]")
	}

	switch os.Args[1] {
	case "create":
		if err := create(); err != nil {
			out := Error{Msg: err.Error()}
			_ = json.NewEncoder(os.Stdout).Encode(out)
			os.Exit(1)
		}
	case "setup", "teardown":
		// this is executed and tested in netavark so we not need it here
	default:
		errAndExit("unknown argument: " + os.Args[1])
	}
}

func create() error {
	network := types.Network{}
	d := json.NewDecoder(os.Stdin)
	err := d.Decode(&network)
	if err != nil {
		return fmt.Errorf("failed to decode network input: %w", err)
	}

	// for testing purpose error out when tests set error option
	msg, ok := network.Options["error"]
	if ok {
		return fmt.Errorf("%s", msg)
	}

	// for testing purpose change field when instructed to do so
	name, ok := network.Options["name"]
	if ok {
		network.Name = name
	}
	id, ok := network.Options["id"]
	if ok {
		network.ID = id
	}
	driver, ok := network.Options["driver"]
	if ok {
		network.Driver = driver
	}

	e := json.NewEncoder(os.Stdout)
	return e.Encode(network)
}