Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ws-manager-api/go/exposed_ports.go
2496 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package api
6
7
import (
8
"encoding/base64"
9
10
"golang.org/x/xerrors"
11
"google.golang.org/protobuf/proto"
12
)
13
14
// ToBase64 marshals the image spec using protobuf and encodes it in base64
15
func (spec *ExposedPorts) ToBase64() (res string, err error) {
16
if spec == nil {
17
return
18
}
19
20
rspec, err := proto.Marshal(spec)
21
if err != nil {
22
return "", xerrors.Errorf("cannot marshal image spec: %w", err)
23
}
24
25
return base64.StdEncoding.EncodeToString(rspec), nil
26
}
27
28
// ExposedPortsFromBase64 decodes an image specification from a base64 encoded protobuf message
29
func ExposedPortsFromBase64(input string) (res *ExposedPorts, err error) {
30
if len(input) == 0 {
31
return
32
}
33
34
specPB, err := base64.StdEncoding.DecodeString(input)
35
if err != nil {
36
return nil, xerrors.Errorf("cannot decode image spec: %w", err)
37
}
38
39
var spec ExposedPorts
40
err = proto.Unmarshal(specPB, &spec)
41
if err != nil {
42
return nil, xerrors.Errorf("cannot unmarshal image spec: %w", err)
43
}
44
45
return &spec, nil
46
}
47
48