/
instruction.go
48 lines (39 loc) · 1.54 KB
/
instruction.go
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
package dockerfile
import "fmt"
type InstructionDataInterface interface {
Name() string
}
type DockerfileStageInstructionInterface interface {
SetDependencyByStageRef(ref string, dep *DockerfileStage)
GetDependencyByStageRef(ref string) *DockerfileStage
GetDependenciesByStageRef() map[string]*DockerfileStage
GetInstructionData() InstructionDataInterface
}
type DockerfileStageInstruction[T InstructionDataInterface] struct {
Data T
DependenciesByStageRef map[string]*DockerfileStage
}
func NewDockerfileStageInstruction[T InstructionDataInterface](data T) *DockerfileStageInstruction[T] {
return &DockerfileStageInstruction[T]{
Data: data,
DependenciesByStageRef: make(map[string]*DockerfileStage),
}
}
func (i *DockerfileStageInstruction[T]) SetDependencyByStageRef(ref string, dep *DockerfileStage) {
if d, hasDep := i.DependenciesByStageRef[ref]; hasDep {
if d.Index != dep.Index {
panic(fmt.Sprintf("already set instruction dependency %q to stage %d named %q, cannot replace with stage %d named %q, please report a bug", ref, d.Index, d.StageName, dep.Index, dep.StageName))
}
return
}
i.DependenciesByStageRef[ref] = dep
}
func (i *DockerfileStageInstruction[T]) GetDependencyByStageRef(ref string) *DockerfileStage {
return i.DependenciesByStageRef[ref]
}
func (i *DockerfileStageInstruction[T]) GetDependenciesByStageRef() map[string]*DockerfileStage {
return i.DependenciesByStageRef
}
func (i *DockerfileStageInstruction[T]) GetInstructionData() InstructionDataInterface {
return i.Data
}