Repository navigation
Expand file tree
/
Copy pathexport.go
More file actions
130 lines (118 loc) · 3.53 KB
/
Copy pathexport.go
File metadata and controls
130 lines (118 loc) · 3.53 KB
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
package workflowtemplate
import (
"archive/tar"
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"github.com/rockbears/log"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/exportentities"
)
func exportTemplate(wt sdk.WorkflowTemplate) (exportentities.Template, error) {
e, err := exportentities.NewTemplate(wt)
if err != nil {
return exportentities.Template{}, err
}
return e, nil
}
// Pull writes the content of a template inside the given writer.
func Pull(ctx context.Context, wt *sdk.WorkflowTemplate, f exportentities.Format, w io.Writer) error {
tw := tar.NewWriter(w)
defer func() {
if err := tw.Close(); err != nil {
log.Error(ctx, "%v", sdk.WrapError(err, "unable to close tar writer"))
}
}()
ewt, err := exportTemplate(*wt)
if err != nil {
return sdk.WrapError(err, "unable to export template")
}
bufft, err := exportentities.Marshal(ewt, f)
if err != nil {
return err
}
hdr := &tar.Header{
Name: fmt.Sprintf(exportentities.PullWorkflowName, wt.Slug),
Mode: 0644,
Size: int64(len(bufft)),
}
if err := tw.WriteHeader(hdr); err != nil {
return sdk.WrapError(err, "Unable to write tmpl header %+v", hdr)
}
if _, err := io.Copy(tw, bytes.NewReader(bufft)); err != nil {
return sdk.WrapError(err, "Unable to copy tmpl buffer")
}
data, err := base64.StdEncoding.DecodeString(wt.Workflow)
if err != nil {
return sdk.WrapError(err, "Unable to decode workflow value")
}
buffw := bytes.NewBuffer(data)
hdr = &tar.Header{
Name: fmt.Sprintf(exportentities.TemplateWorkflowName),
Mode: 0644,
Size: int64(buffw.Len()),
}
if err := tw.WriteHeader(hdr); err != nil {
return sdk.WrapError(err, "Unable to write workflow header %+v", hdr)
}
if _, err := io.Copy(tw, buffw); err != nil {
return sdk.WrapError(err, "Unable to copy workflow buffer")
}
for i, p := range wt.Pipelines {
data, err := base64.StdEncoding.DecodeString(p.Value)
if err != nil {
return sdk.WrapError(err, "Unable to decode pipeline value")
}
buff := bytes.NewBuffer(data)
hdr := &tar.Header{
Name: fmt.Sprintf(exportentities.TemplatePipelineName, i+1),
Mode: 0644,
Size: int64(buff.Len()),
}
if err := tw.WriteHeader(hdr); err != nil {
return sdk.WrapError(err, "Unable to write pipeline header %+v", hdr)
}
if _, err := io.Copy(tw, buff); err != nil {
return sdk.WrapError(err, "Unable to copy pipeline buffer")
}
}
for i, a := range wt.Applications {
data, err := base64.StdEncoding.DecodeString(a.Value)
if err != nil {
return sdk.WrapError(err, "Unable to decode application value")
}
buff := bytes.NewBuffer(data)
hdr := &tar.Header{
Name: fmt.Sprintf(exportentities.TemplateApplicationName, i+1),
Mode: 0644,
Size: int64(buff.Len()),
}
if err := tw.WriteHeader(hdr); err != nil {
return sdk.WrapError(err, "Unable to write application header %+v", hdr)
}
if _, err := io.Copy(tw, buff); err != nil {
return sdk.WrapError(err, "Unable to copy application buffer")
}
}
for i, e := range wt.Environments {
data, err := base64.StdEncoding.DecodeString(e.Value)
if err != nil {
return sdk.WrapError(err, "Unable to decode environment value")
}
buff := bytes.NewBuffer(data)
hdr := &tar.Header{
Name: fmt.Sprintf(exportentities.TemplateEnvironmentName, i+1),
Mode: 0644,
Size: int64(buff.Len()),
}
if err := tw.WriteHeader(hdr); err != nil {
return sdk.WrapError(err, "Unable to write environment header %+v", hdr)
}
if _, err := io.Copy(tw, buff); err != nil {
return sdk.WrapError(err, "Unable to copy environment buffer")
}
}
return nil
}