inputs: use a struct rather than an interface
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
parent
a1c9041363
commit
ef84d2d431
106
dagger/input.go
106
dagger/input.go
@ -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,
|
||||||
Path: path,
|
Dir: &dirInput{
|
||||||
Include: include,
|
Path: path,
|
||||||
|
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,
|
||||||
Remote: remote,
|
Git: &gitInput{
|
||||||
Ref: ref,
|
Remote: remote,
|
||||||
Dir: dir,
|
Ref: ref,
|
||||||
|
Dir: dir,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,15 +116,16 @@ 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,
|
||||||
Ref: ref,
|
Docker: &dockerInput{
|
||||||
|
Ref: ref,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type dockerInput struct {
|
type dockerInput struct {
|
||||||
Type string `json:"type,omitempty"`
|
Ref string `json:"ref,omitempty"`
|
||||||
Ref string `json:"ref,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i dockerInput) Compile() (*compiler.Value, error) {
|
func (i dockerInput) Compile() (*compiler.Value, error) {
|
||||||
@ -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,
|
||||||
Data: data,
|
Text: &textInput{
|
||||||
|
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,
|
||||||
Data: data,
|
JSON: &jsonInput{
|
||||||
|
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,
|
||||||
Data: data,
|
YAML: &yamlInput{
|
||||||
|
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"`
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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)
|
||||||
|
Reference in New Issue
Block a user