inputs: use a struct rather than an interface

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-03-26 14:11:54 -07:00
parent a1c9041363
commit ef84d2d431
3 changed files with 82 additions and 41 deletions

View File

@ -19,22 +19,61 @@ import (
// Under the hood, an artifact is encoded as a LLB pipeline, and // Under the hood, an artifact is encoded as a LLB pipeline, and
// attached to the cue configuration as a // attached to the cue configuration as a
// //
type Input interface { type InputType string
// Compile to a cue value which can be merged into a route config
Compile() (*compiler.Value, error) const (
InputTypeDir InputType = "dir"
InputTypeGit InputType = "git"
InputTypeDocker InputType = "docker"
InputTypeText InputType = "text"
InputTypeJSON InputType = "json"
InputTypeYAML InputType = "yaml"
)
type Input struct {
Type InputType `json:"type,omitempty"`
Dir *dirInput `json:"dir,omitempty"`
Git *gitInput `json:"git,omitempty"`
Docker *dockerInput `json:"docker,omitempty"`
Text *textInput `json:"text,omitempty"`
JSON *jsonInput `json:"json,omitempty"`
YAML *yamlInput `json:"yaml,omitempty"`
}
func (i Input) Compile() (*compiler.Value, error) {
switch i.Type {
case InputTypeDir:
return i.Dir.Compile()
case InputTypeGit:
return i.Git.Compile()
case InputTypeDocker:
return i.Docker.Compile()
case InputTypeText:
return i.Text.Compile()
case InputTypeJSON:
return i.JSON.Compile()
case InputTypeYAML:
return i.YAML.Compile()
case "":
return nil, fmt.Errorf("input has not been set")
default:
return nil, fmt.Errorf("unsupported input type: %s", i.Type)
}
} }
// An input artifact loaded from a local directory // An input artifact loaded from a local directory
func DirInput(path string, include []string) Input { func DirInput(path string, include []string) Input {
return &dirInput{ return Input{
Type: "dir", Type: InputTypeDir,
Dir: &dirInput{
Path: path, Path: path,
Include: include, Include: include,
},
} }
} }
type dirInput struct { type dirInput struct {
Type string `json:"type,omitempty"`
Path string `json:"path,omitempty"` Path string `json:"path,omitempty"`
Include []string `json:"include,omitempty"` Include []string `json:"include,omitempty"`
} }
@ -55,18 +94,19 @@ func (dir dirInput) Compile() (*compiler.Value, error) {
// An input artifact loaded from a git repository // An input artifact loaded from a git repository
type gitInput struct { type gitInput struct {
Type string `json:"type,omitempty"`
Remote string `json:"remote,omitempty"` Remote string `json:"remote,omitempty"`
Ref string `json:"ref,omitempty"` Ref string `json:"ref,omitempty"`
Dir string `json:"dir,omitempty"` Dir string `json:"dir,omitempty"`
} }
func GitInput(remote, ref, dir string) Input { func GitInput(remote, ref, dir string) Input {
return &gitInput{ return Input{
Type: "git", Type: InputTypeGit,
Git: &gitInput{
Remote: remote, Remote: remote,
Ref: ref, Ref: ref,
Dir: dir, Dir: dir,
},
} }
} }
@ -76,14 +116,15 @@ func (git gitInput) Compile() (*compiler.Value, error) {
// An input artifact loaded from a docker container // An input artifact loaded from a docker container
func DockerInput(ref string) Input { func DockerInput(ref string) Input {
return &dockerInput{ return Input{
Type: "docker", Type: InputTypeDocker,
Docker: &dockerInput{
Ref: ref, Ref: ref,
},
} }
} }
type dockerInput struct { type dockerInput struct {
Type string `json:"type,omitempty"`
Ref string `json:"ref,omitempty"` Ref string `json:"ref,omitempty"`
} }
@ -93,14 +134,15 @@ func (i dockerInput) Compile() (*compiler.Value, error) {
// An input value encoded as text // An input value encoded as text
func TextInput(data string) Input { func TextInput(data string) Input {
return &textInput{ return Input{
Type: "text", Type: InputTypeText,
Text: &textInput{
Data: data, Data: data,
},
} }
} }
type textInput struct { type textInput struct {
Type string `json:"type,omitempty"`
Data string `json:"data,omitempty"` Data string `json:"data,omitempty"`
} }
@ -110,14 +152,15 @@ func (i textInput) Compile() (*compiler.Value, error) {
// An input value encoded as JSON // An input value encoded as JSON
func JSONInput(data string) Input { func JSONInput(data string) Input {
return &jsonInput{ return Input{
Type: "json", Type: InputTypeJSON,
JSON: &jsonInput{
Data: data, Data: data,
},
} }
} }
type jsonInput struct { type jsonInput struct {
Type string `json:"type,omitempty"`
// Marshalled JSON data // Marshalled JSON data
Data string `json:"data,omitempty"` Data string `json:"data,omitempty"`
} }
@ -128,14 +171,15 @@ func (i jsonInput) Compile() (*compiler.Value, error) {
// An input value encoded as YAML // An input value encoded as YAML
func YAMLInput(data string) Input { func YAMLInput(data string) Input {
return &yamlInput{ return Input{
Type: "yaml", Type: InputTypeYAML,
YAML: &yamlInput{
Data: data, Data: data,
},
} }
} }
type yamlInput struct { type yamlInput struct {
Type string `json:"type,omitempty"`
// Marshalled YAML data // Marshalled YAML data
Data string `json:"data,omitempty"` Data string `json:"data,omitempty"`
} }

View File

@ -206,13 +206,11 @@ func (r *Route) LocalDirs() map[string]string {
} }
// 2. Scan the layout // 2. Scan the layout
if r.st.LayoutSource != nil {
layout, err := r.st.LayoutSource.Compile() layout, err := r.st.LayoutSource.Compile()
if err != nil { if err != nil {
panic(err) panic(err)
} }
localdirs(layout) localdirs(layout)
}
return dirs return dirs
} }

View File

@ -107,12 +107,11 @@ func (s *Store) indexRoute(r *RouteState) {
s.routesByName[r.Name] = r s.routesByName[r.Name] = r
mapPath := func(i Input) { mapPath := func(i Input) {
d, ok := i.(*dirInput) if i.Type != InputTypeDir {
if !ok {
return return
} }
s.routesByPath[d.Path] = r s.routesByPath[i.Dir.Path] = r
s.pathsByRoute[r.ID] = append(s.pathsByRoute[r.ID], d.Path) s.pathsByRoute[r.ID] = append(s.pathsByRoute[r.ID], i.Dir.Path)
} }
mapPath(r.LayoutSource) mapPath(r.LayoutSource)