Exemple #1
0
func main() {
	flags := struct {
		help    bool
		pretty  bool
		inFile  string
		outFile string
	}{}

	flag.BoolVar(&flags.help, "help", false, "print help and exit")
	flag.BoolVar(&flags.pretty, "pretty", false, "indent the output file")
	flag.StringVar(&flags.inFile, "in-file", "/dev/stdin", "input file (YAML)")
	flag.StringVar(&flags.outFile, "out-file", "/dev/stdout", "output file (JSON)")

	flag.Parse()

	if flags.help {
		flag.Usage()
		return
	}

	cfg := config.Config{}
	dataIn, err := ioutil.ReadFile(flags.inFile)
	if err != nil {
		stderr("Failed to read: %v", err)
		os.Exit(1)
	}

	if err := yaml.Unmarshal(dataIn, &cfg); err != nil {
		stderr("Failed to unmarshal input: %v", err)
		os.Exit(1)
	}

	var inCfg interface{}
	if err := yaml.Unmarshal(dataIn, &inCfg); err != nil {
		stderr("Failed to unmarshal input: %v", err)
		os.Exit(1)
	}

	if hasUnrecognizedKeys(inCfg, reflect.TypeOf(cfg)) {
		stderr("Unrecognized keys in input, aborting.")
		os.Exit(1)
	}

	var dataOut []byte
	if flags.pretty {
		dataOut, err = json.MarshalIndent(&cfg, "", "  ")
		dataOut = append(dataOut, '\n')
	} else {
		dataOut, err = json.Marshal(&cfg)
	}
	if err != nil {
		stderr("Failed to marshal output: %v", err)
		os.Exit(1)
	}

	if err := ioutil.WriteFile(flags.outFile, dataOut, 0640); err != nil {
		stderr("Failed to write: %v", err)
		os.Exit(1)
	}
}
Exemple #2
0
func TestHasUnrecognizedKeys(t *testing.T) {
	tests := []struct {
		in    string
		unrec bool
	}{
		{
			in:    "ignition_version: 1",
			unrec: false,
		},
		{
			in:    "ignition_version: 1\npasswd:\n users:\n  - name: foobar\n",
			unrec: false,
		},
		{
			in:    "foo: bar",
			unrec: true,
		},
		{
			in:    "ignition_version: 1\npasswd:\n users:\n  - naem: foobar\n",
			unrec: true,
		},
	}

	for i, tt := range tests {
		var cfg interface{}
		if err := yaml.Unmarshal([]byte(tt.in), &cfg); err != nil {
			t.Errorf("%d: unmarshal failed: %v", i, err)
			continue
		}
		if unrec := hasUnrecognizedKeys(cfg, reflect.TypeOf(config.Config{})); unrec != tt.unrec {
			t.Errorf("%d: expected %v got %v", i, tt.unrec, unrec)
		}
	}
}
Exemple #3
0
func main() {
	flag.Parse()

	if *flagHelp {
		flag.Usage()
		os.Exit(1)
	}

	cfg := config.Config{}
	b, err := ioutil.ReadFile(*flagInFile)
	if err != nil {
		stderr("Failed to read: %v", err)
		os.Exit(2)
	}

	if err := yaml.Unmarshal(b, &cfg); err != nil {
		stderr("Failed to unmarshal input: %v", err)
		os.Exit(3)
	}

	b, err = json.Marshal(&cfg)
	if err != nil {
		stderr("Failed to marshal output: %v", err)
		os.Exit(4)
	}

	if err := ioutil.WriteFile(*flagOutFile, b, 0640); err != nil {
		stderr("Failed to write: %v", err)
		os.Exit(5)
	}

	os.Exit(0)
}
Exemple #4
0
func TestDevicePathUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		device DevicePath
		err    error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: `"/path"`},
			out: out{device: DevicePath("/path")},
		},
		{
			in:  in{data: `"bad"`},
			out: out{device: DevicePath("bad"), err: errors.New("device path not absolute")},
		},
	}

	for i, test := range tests {
		var device DevicePath
		err := yaml.Unmarshal([]byte(test.in.data), &device)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.device, device) {
			t.Errorf("#%d: bad device: want %#v, got %#v", i, test.out.device, device)
		}
	}
}
Exemple #5
0
func TestMkfsOptionsUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		options MkfsOptions
		err     error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: `["--label=ROOT"]`},
			out: out{options: MkfsOptions([]string{"--label=ROOT"})},
		},
	}

	for i, test := range tests {
		var options MkfsOptions
		err := yaml.Unmarshal([]byte(test.in.data), &options)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.options, options) {
			t.Errorf("#%d: bad device: want %#v, got %#v", i, test.out.options, options)
		}
	}
}
Exemple #6
0
func TestFilesystemFormatUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		format FilesystemFormat
		err    error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: `"ext4"`},
			out: out{format: FilesystemFormat("ext4")},
		},
		{
			in:  in{data: `"bad"`},
			out: out{format: FilesystemFormat("bad"), err: errors.New("invalid filesystem format")},
		},
	}

	for i, test := range tests {
		var format FilesystemFormat
		err := yaml.Unmarshal([]byte(test.in.data), &format)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.format, format) {
			t.Errorf("#%d: bad device: want %#v, got %#v", i, test.out.format, format)
		}
	}
}
Exemple #7
0
func TestNetworkdUnitNameUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		unit NetworkdUnitName
		err  error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: `"test.network"`},
			out: out{unit: NetworkdUnitName("test.network")},
		},
		{
			in:  in{data: `"test.link"`},
			out: out{unit: NetworkdUnitName("test.link")},
		},
		{
			in:  in{data: `"test.netdev"`},
			out: out{unit: NetworkdUnitName("test.netdev")},
		},
		{
			in:  in{data: `"test.blah"`},
			out: out{err: errors.New("invalid networkd unit extension")},
		},
	}

	for i, test := range tests {
		var unit NetworkdUnitName
		err := yaml.Unmarshal([]byte(test.in.data), &unit)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if err != nil {
			continue
		}

		if !reflect.DeepEqual(test.out.unit, unit) {
			t.Errorf("#%d: bad unit: want %#v, got %#v", i, test.out.unit, unit)
		}
	}
}
Exemple #8
0
func TestFileModeUnmarshalYAML(t *testing.T) {
	type in struct {
		data string
	}
	type out struct {
		mode FileMode
		err  error
	}

	tests := []struct {
		in  in
		out out
	}{
		{
			in:  in{data: `0644`},
			out: out{mode: FileMode(0644)},
		},
		{
			in:  in{data: `0420`},
			out: out{mode: FileMode(0420)},
		},
		{
			in:  in{data: `017777`},
			out: out{mode: FileMode(017777), err: ErrFileIllegalMode},
		},
	}

	for i, test := range tests {
		var mode FileMode
		err := yaml.Unmarshal([]byte(test.in.data), &mode)
		if !reflect.DeepEqual(test.out.err, err) {
			t.Errorf("#%d: bad error: want %v, got %v", i, test.out.err, err)
		}
		if !reflect.DeepEqual(test.out.mode, mode) {
			t.Errorf("#%d: bad mode: want %#o, got %#o", i, test.out.mode, mode)
		}
	}
}