-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
controller.go
59 lines (54 loc) · 1.43 KB
/
controller.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
package controller
import (
"context"
"io"
"os"
"path/filepath"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/google/go-github/v38/github"
"github.com/sirupsen/logrus"
"github.com/suzuki-shunsuke/go-template-unmarshaler/text"
"golang.org/x/oauth2"
)
type Controller struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
ConfigFinder ConfigFinder
ConfigReader ConfigReader
GitHub *github.Client
RootDir string
}
func New(ctx context.Context, param *Param) (*Controller, error) {
if param.LogLevel != "" {
lvl, err := logrus.ParseLevel(param.LogLevel)
if err != nil {
logrus.WithFields(logrus.Fields{
"log_level": param.LogLevel,
}).WithError(err).Error("the log level is invalid")
}
logrus.SetLevel(lvl)
}
text.SetTemplateFunc(func(s string) (*template.Template, error) {
return template.New("_").Funcs(sprig.TxtFuncMap()).Parse(s) //nolint:wrapcheck
})
ctrl := Controller{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
ConfigFinder: &configFinder{},
ConfigReader: &configReader{},
RootDir: os.Getenv("AQUA_ROOT_DIR"),
}
if ctrl.RootDir == "" {
ctrl.RootDir = filepath.Join(os.Getenv("HOME"), ".aqua")
}
if ghToken := os.Getenv("GITHUB_TOKEN"); ghToken != "" {
ctrl.GitHub = github.NewClient(
oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ghToken},
)))
}
return &ctrl, nil
}