root.go 3.59 KB
Newer Older
songlinfeng's avatar
songlinfeng committed
1
2
3
4
5
6
7
/**
# Copyright (c) 2024, HCUOpt CORPORATION.  All rights reserved.
**/

package root

import (
8
9
10
	"dcu-container-toolkit/internal/logger"
	"dcu-container-toolkit/pkg/c3000cdi/spec"
	transformroot "dcu-container-toolkit/pkg/c3000cdi/transform/root"
songlinfeng's avatar
songlinfeng committed
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	"fmt"
	"io"
	"os"

	"github.com/urfave/cli/v2"
	"tags.cncf.io/container-device-interface/pkg/cdi"
)

type command struct {
	logger logger.Interface
}

type transformOptions struct {
	input  string
	output string
}

type options struct {
	transformOptions
	from       string
	to         string
	relativeTo string
}

// NewCommand constructs a generate-cdi command with the specified logger
func NewCommand(logger logger.Interface) *cli.Command {
	c := command{
		logger: logger,
	}
	return c.build()
}

// build creates the CLI command
func (m command) build() *cli.Command {
	opts := options{}

	c := cli.Command{
		Name:  "root",
		Usage: "Apply a root transform to a CDI specification",
		Before: func(c *cli.Context) error {
			return m.validateFlags(c, &opts)
		},
		Action: func(c *cli.Context) error {
			return m.run(c, &opts)
		},
	}

	c.Flags = []cli.Flag{
		&cli.StringFlag{
			Name:        "from",
			Usage:       "specify the root to be transformed",
			Destination: &opts.from,
		},
		&cli.StringFlag{
			Name:        "input",
			Usage:       "Specify the file to read the CDI specification from. If this is '-' the specification is read from STDIN",
			Value:       "-",
			Destination: &opts.input,
		},
		&cli.StringFlag{
			Name:        "output",
			Usage:       "Specify the file to output the generated CDI specification to. If this is '' the specification is output to STDOUT",
			Destination: &opts.output,
		},
		&cli.StringFlag{
			Name:        "relative-to",
			Usage:       "specify whether the transform is relative to the host or to the container. One of [ host | container ]",
			Value:       "host",
			Destination: &opts.relativeTo,
		},
		&cli.StringFlag{
			Name:        "to",
			Usage:       "specify the replacement root. If this is the same as the from root, the transform is a no-op.",
			Value:       "",
			Destination: &opts.to,
		},
	}

	return &c
}

func (m command) validateFlags(c *cli.Context, opts *options) error {
	switch opts.relativeTo {
	case "host":
	case "container":
	default:
		return fmt.Errorf("invalid --relative-to value: %v", opts.relativeTo)
	}
	return nil
}

func (m command) run(c *cli.Context, opts *options) error {
	spec, err := opts.Load()
	if err != nil {
		return fmt.Errorf("failed to load CDI specification: %w", err)
	}

	err = transformroot.New(
		transformroot.WithRoot(opts.from),
		transformroot.WithTargetRoot(opts.to),
		transformroot.WithRelativeTo(opts.relativeTo),
	).Transform(spec.Raw())
	if err != nil {
		return fmt.Errorf("failed to transform CDI specification: %w", err)
	}

	return opts.Save(spec)
}

// Load lodas the input CDI specification
func (o transformOptions) Load() (spec.Interface, error) {
	contents, err := o.getContents()
	if err != nil {
		return nil, fmt.Errorf("failed to read spec contents: %v", err)
	}

	raw, err := cdi.ParseSpec(contents)
	if err != nil {
		return nil, fmt.Errorf("failed to parse CDI spec: %v", err)
	}

	return spec.New(
		spec.WithRawSpec(raw),
	)
}

func (o transformOptions) getContents() ([]byte, error) {
	if o.input == "-" {
		return io.ReadAll(os.Stdin)
	}

	return os.ReadFile(o.input)
}

// Save saves the CDI specification to the output file
func (o transformOptions) Save(s spec.Interface) error {
	if o.output == "" {
		_, err := s.WriteTo(os.Stdout)
		if err != nil {
			return fmt.Errorf("failed to write CDI spec to STDOUT: %v", err)
		}
		return nil
	}

	return s.Save(o.output)
}