1// SPDX-License-Identifier: MIT
2
3package workflow
4
5import (
6 "bnema.dev/ermite-actions/template"
7)
8
9// convertContainer validates a job container or, when service is set, a
10// service container. A token that contains an expression is not checked, as
11// in the reference.
12func (c *converter) convertContainer(t template.Token, service bool) {
13 if template.HasExpression(t) {
14 return
15 }
16 if _, ok := t.(*template.String); ok {
17 return
18 }
19 var image template.Token
20 c.handle(t, func() {
21 m := asMap(t, "container item")
22 for _, p := range m.Pairs {
23 key := asString(p.Key, "container item key")
24 v := p.Value
25 switch key.Value {
26 case "image":
27 image = asString(v, "container image")
28 case "credentials":
29 c.convertCredentials(v)
30 case "env":
31 for _, e := range asMap(v, "container env").Pairs {
32 asString(e.Key, "container env value")
33 }
34 case "ports":
35 for _, it := range asSeq(v, "container ports").Items {
36 asString(it, "container port")
37 }
38 case "volumes":
39 for _, it := range asSeq(v, "container volumes").Items {
40 asString(it, "container volume")
41 }
42 case "options":
43 asString(v, "container options")
44 case "entrypoint":
45 if !service {
46 c.ctx.Errorf(key, "Unexpected container item key: %s", key.Value)
47 continue
48 }
49 asString(v, "container entrypoint")
50 case "command":
51 if !service {
52 c.ctx.Errorf(key, "Unexpected container item key: %s", key.Value)
53 continue
54 }
55 asString(v, "container command")
56 default:
57 c.ctx.Errorf(key, "Unexpected container item key: %s", key.Value)
58 }
59 }
60 })
61 if image == nil {
62 if _, isMap := t.(*template.Mapping); isMap {
63 c.ctx.ErrorAt(t, "Container image cannot be empty")
64 }
65 }
66}
67
68func (c *converter) convertCredentials(t template.Token) {
69 for _, p := range asMap(t, "credentials").Pairs {
70 key := asString(p.Key, "credentials item")
71 switch key.Value {
72 case "username":
73 asString(p.Value, "credentials username")
74 case "password":
75 asString(p.Value, "credentials password")
76 default:
77 c.ctx.Errorf(key, "credentials key %s", key.Value)
78 }
79 }
80}
81
82// convertServices validates the services mapping of a job.
83func (c *converter) convertServices(t template.Token) {
84 for _, p := range asMap(t, "services").Pairs {
85 asString(p.Key, "service key")
86 c.convertContainer(p.Value, true)
87 }
88}