-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconverter.go
138 lines (115 loc) · 3.01 KB
/
converter.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
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
package zbaction
import (
"fmt"
"github.com/zeabur/action/proto"
)
func ActionToProto(action Action) (*proto.Action, error) {
p := &proto.Action{
Id: action.ID,
Jobs: make([]*proto.Job, len(action.Jobs)),
Variables: action.Variables,
Requirements: make([]*proto.Requirement, len(action.Requirements)),
Metadata: action.Metadata,
}
for requirementIndex, requirement := range action.Requirements {
p.Requirements[requirementIndex] = &proto.Requirement{
Expr: requirement.Expr,
Description: requirement.Description,
}
}
for jobIndex, job := range action.Jobs {
pj := &proto.Job{
Id: job.ID,
Steps: make([]*proto.Step, len(job.Steps)),
Variables: job.Variables,
}
for stepIndex, step := range job.Steps {
ps := &proto.Step{
Id: step.ID,
Name: step.Name,
Step: nil,
Variables: step.Variables,
}
if err := exactStepToProto(step, ps); err != nil {
return nil, fmt.Errorf("failed to convert step %s to proto: %w", step.ID, err)
}
pj.Steps[stepIndex] = ps
}
p.Jobs[jobIndex] = pj
}
return p, nil
}
func ActionFromProto(p *proto.Action) (Action, error) {
action := Action{
ID: p.Id,
Jobs: make([]Job, len(p.Jobs)),
Variables: p.Variables,
Requirements: make([]Requirement, len(p.Requirements)),
Metadata: p.Metadata,
}
for requirementIndex, requirement := range p.Requirements {
action.Requirements[requirementIndex] = Requirement{
Expr: requirement.Expr,
Description: requirement.Description,
}
}
for jobIndex, job := range p.Jobs {
j := Job{
ID: job.Id,
Steps: make([]Step, len(job.Steps)),
Variables: job.Variables,
}
for stepIndex, step := range job.Steps {
s, err := exactStepFromProto(step)
if err != nil {
return Action{}, fmt.Errorf("failed to convert step %s from proto: %w", step.Id, err)
}
j.Steps[stepIndex] = Step{
ID: step.Id,
Name: step.Name,
RunnableStep: s,
Variables: step.Variables,
}
}
action.Jobs[jobIndex] = j
}
return action, nil
}
func exactStepToProto(step Step, out *proto.Step) error {
runnableStep := step.RunnableStep
switch runnableStep := runnableStep.(type) {
case CommandStep:
out.Step = &proto.Step_Command{
Command: &proto.CommandStep{
Command: runnableStep.Command,
},
}
case ProcStep:
out.Step = &proto.Step_Proc{
Proc: &proto.ProcStep{
Uses: runnableStep.Uses,
With: runnableStep.With,
},
}
default:
return fmt.Errorf("unknown step type received: %T (%+v)", runnableStep, runnableStep)
}
return nil
}
func exactStepFromProto(p *proto.Step) (RunnableStep, error) {
var step RunnableStep
switch p := p.Step.(type) {
case *proto.Step_Command:
step = CommandStep{
Command: p.Command.Command,
}
case *proto.Step_Proc:
step = ProcStep{
Uses: p.Proc.Uses,
With: p.Proc.With,
}
default:
return Step{}, fmt.Errorf("unknown step type received: %T (%+v)", p, p)
}
return step, nil
}