-
Notifications
You must be signed in to change notification settings - Fork 12
/
generate.go
87 lines (72 loc) · 1.9 KB
/
generate.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
// SPDX-License-Identifier: Apache-2.0
package config
import (
"path/filepath"
"github.com/spf13/afero"
yaml "gopkg.in/yaml.v3"
"github.com/sirupsen/logrus"
)
// create filesystem based on the operating system
//
// https://godoc.org/github.com/spf13/afero#NewOsFs
var appFS = afero.NewOsFs()
// Generate produces a config file based off the provided configuration.
func (c *Config) Generate() error {
logrus.Debug("executing generate for config file configuration")
// create the config file content
//
// https://pkg.go.dev/github.com/go-vela/cli/action/config?tab=doc#ConfigFile
config := &ConfigFile{
API: &API{
Address: c.Addr,
Token: c.Token,
AccessToken: c.AccessToken,
RefreshToken: c.RefreshToken,
Version: c.Version,
},
Log: &Log{
Level: c.LogLevel,
},
Secret: &Secret{
Engine: c.Engine,
Type: c.Type,
},
Compiler: &Compiler{
GitHub: &GitHub{
Token: c.GitHub.Token,
URL: c.GitHub.URL,
},
},
Org: c.Org,
Repo: c.Repo,
Output: c.Output,
NoGit: c.NoGit,
}
logrus.Trace("creating file content for config file")
// create output for config file
//
// https://pkg.go.dev/gopkg.in/yaml.v3?tab=doc#Marshal
out, err := yaml.Marshal(config)
if err != nil {
return err
}
// use custom filesystem which enables us to test
//
// https://pkg.go.dev/github.com/spf13/afero?tab=doc#Afero
a := &afero.Afero{
Fs: appFS,
}
logrus.Tracef("creating directory structure to %s", c.File)
// send Filesystem call to create directory path for config file
//
// https://pkg.go.dev/github.com/spf13/afero?tab=doc#OsFs.MkdirAll
err = a.Fs.MkdirAll(filepath.Dir(c.File), 0777)
if err != nil {
return err
}
logrus.Tracef("writing file content to %s", c.File)
// send Filesystem call to create config file
//
// https://pkg.go.dev/github.com/spf13/afero?tab=doc#Afero.WriteFile
return a.WriteFile(c.File, out, 0600)
}