This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
orb.go
82 lines (64 loc) · 1.54 KB
/
orb.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
package orb
import (
"fmt"
"io/ioutil"
"os"
"github.com/caos/orbos/internal/helpers"
"github.com/caos/orbos/internal/secret"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
type Orb struct {
Path string `yaml:"-"`
URL string
Repokey string
Masterkey string
}
func (o *Orb) IsConnectable() (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("repository is not connectable: %w", err)
}
}()
if o.URL == "" {
err = helpers.Concat(err, errors.New("repository url is missing"))
}
if o.Repokey == "" {
err = helpers.Concat(err, errors.New("repository key is missing"))
}
return err
}
func (o *Orb) IsComplete() (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("orbconfig is incomplete: %w", err)
}
}()
if o.Masterkey == "" {
err = helpers.Concat(err, errors.New("master key is missing"))
}
if o.Path == "" {
err = helpers.Concat(err, errors.New("file path is missing"))
}
return helpers.Concat(err, o.IsConnectable())
}
func ParseOrbConfig(orbConfigPath string) (*Orb, error) {
gitOrbConfig, err := ioutil.ReadFile(orbConfigPath)
if err != nil {
return nil, errors.Wrap(err, "unable to read orbconfig")
}
orb := &Orb{}
if err := yaml.Unmarshal(gitOrbConfig, orb); err != nil {
return nil, errors.Wrap(err, "unable to parse orbconfig")
}
orb.Path = orbConfigPath
secret.Masterkey = orb.Masterkey
return orb, nil
}
func (o *Orb) WriteBackOrbConfig() error {
data, err := yaml.Marshal(o)
if err != nil {
return err
}
return ioutil.WriteFile(o.Path, data, os.ModePerm)
}