-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (71 loc) · 1.97 KB
/
main.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
package main
import (
"archci/gitgen"
"net/http"
"os"
"strings"
"github.com/drone/drone-go/plugin/config"
_ "github.com/joho/godotenv/autoload"
"github.com/kelseyhightower/envconfig"
"github.com/sirupsen/logrus"
)
// spec provides the plugin settings.
type spec struct {
Bind string `envconfig:"DRONE_BIND"`
Debug bool `envconfig:"DRONE_DEBUG"`
Secret string `envconfig:"DRONE_SECRET"`
GitRepoDirectory string `envconfig:"DRONE_GIT_REPO_DIRECTORY"`
}
func main() {
spec := new(spec)
err := envconfig.Process("", spec)
if err != nil {
logrus.Fatal(err)
}
if spec.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
if spec.Secret == "" {
logrus.Fatalln("missing secret key")
}
if spec.GitRepoDirectory == "" {
// use a tmp directory if no directory is provided
tempDir, err := os.MkdirTemp("", "drone-git-")
if err != nil {
logrus.Fatal(err)
}
spec.GitRepoDirectory = tempDir
}
if spec.Bind == "" {
spec.Bind = ":3000"
}
// Tracked git repos are set in `DRONE_TRACKED_REPOS` env var, splited by `;`
// Example: `DRONE_TRACKED_REPOS=github.com/owner/repo1;github.com/owner/repo2`
// This is used to track the git repos that are cloned in the `DRONE_GIT_REPO_DIRECTORY`
// and to avoid cloning the same repo multiple times.
trackedRepos := os.Getenv("DRONE_TRACKED_REPOS")
if trackedRepos == "" {
logrus.Warn("no tracked repos provided")
}
conf := gitgen.Config{}
conf.Repos = strings.Split(trackedRepos, ";")
// purge empty strings
for i := 0; i < len(conf.Repos); i++ {
if conf.Repos[i] == "" {
conf.Repos = append(conf.Repos[:i], conf.Repos[i+1:]...)
i--
}
}
// clone the tracked repos
for _, repo := range conf.Repos {
gitgen.UpdateRepo(repo, spec.GitRepoDirectory)
}
handler := config.Handler(
gitgen.New(conf),
spec.Secret,
logrus.StandardLogger(),
)
logrus.Infof("server listening on address %s", spec.Bind)
http.Handle("/", handler)
logrus.Fatal(http.ListenAndServe(spec.Bind, nil))
}