-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (131 loc) · 3.58 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"path"
"reflect"
"github.com/jrschumacher/doctl-serverless-go/pkg/projectconfig"
)
const ProjectCfgFile = "project"
const DOConfigDir = ".do"
const DeployTemplateCfgFile = "deploy.template"
var PrivateRepoDir string
var CfgExt = []string{".yaml", ".yml"}
type goPackageFunc func(pkgDirName, actDirName string) error
const usageMessage = `
usage: doctl-serverless-go <command> [<monorepo-path>]
deploy Deploy the go monorepo to digitalocean serverless
clean Clean the go monorepo after deployment
`
var ErrInvalidUsage = errors.New("invalid usage")
func main() {
var err error
flag.StringVar(&PrivateRepoDir, "private-dir", "private", "private repo directory")
flag.Parse()
subcommand := flag.Arg(0)
if subcommand == "" || (subcommand != "deploy" && subcommand != "clean") {
exitInvalidUsage()
}
monorepoPath := flag.Arg(1)
if monorepoPath == "" {
monorepoPath = "."
}
log.Printf("scanning monorepo... path:\"%s\"", monorepoPath)
if _, err := os.Stat(monorepoPath); err != nil {
log.Fatal(errors.Join(ErrNotMonorepo, err))
}
// check project.yaml exists
var projectCfgFile string
for _, ext := range CfgExt {
f := path.Join(monorepoPath, ProjectCfgFile+ext)
if _, err := os.Stat(f); err == nil {
projectCfgFile = f
break
}
}
if projectCfgFile == "" {
log.Fatal(errors.Join(ErrNotMonorepo, errors.Join(ErrNoProjectCfg, err)))
}
// read project yaml
log.Print("parsing project.yaml... ")
projectCfg, err := projectconfig.Parse(projectCfgFile)
if err != nil {
log.Fatal(errors.Join(ErrParseProjectCfg, errors.Join(ErrNotMonorepo, err)))
}
// stat packages
pkgsDirName := path.Join(monorepoPath, "packages")
pkgsDirStat, err := os.Stat(pkgsDirName)
if err != nil || !pkgsDirStat.IsDir() {
log.Fatal(errors.Join(ErrNotMonorepo, err))
}
errs := make(errMap)
switch subcommand {
case "deploy":
errs = forEveryPackage(pkgsDirName, projectCfg,
clonePrivateRepo(projectCfg),
)
case "clean":
errs = forEveryPackage(pkgsDirName, projectCfg,
cleanPrivateRepo(projectCfg),
)
}
if errs.HasErrors() {
errs.Print()
os.Exit(1)
}
log.Print("completed")
}
func exitInvalidUsage() {
fmt.Println("fatal: missing command")
fmt.Print(usageMessage)
os.Exit(1)
}
func forEveryPackage(pkgsDirName string, projectCfg *projectconfig.ProjectSpec, fns ...goPackageFunc) errMap {
pkgErrs := make(map[string]error)
for _, pkg := range projectCfg.Packages {
scope := pkg.Name
pkgDirName := path.Join(pkgsDirName, pkg.Name)
// check if exists and is a directory
pkgDirStat, err := os.Stat(pkgDirName)
if err != nil || !pkgDirStat.IsDir() {
pkgErrs[scope] = errors.Join(ErrNoPackagesFound, err)
continue
}
// for each action
for _, act := range pkg.Actions {
scope := path.Join(pkg.Name, act.Name)
actDirName := path.Join(pkgDirName, act.Name)
// check if exists and is a directory
actDirStat, err := os.Stat(actDirName)
if err != nil || !actDirStat.IsDir() {
pkgErrs[scope] = errors.Join(ErrNoPackagesFound, err)
continue
}
// is go package? check for go.mod
modPath := path.Join(actDirName, "go.mod")
modStat, err := os.Stat(modPath)
if err != nil {
if !os.IsNotExist(err) {
continue
}
pkgErrs[scope] = errors.Join(ErrCheckingForMod, err)
}
if modStat.IsDir() {
continue
}
// run functions
for _, fn := range fns {
// reflect fn name
fnName := reflect.TypeOf(fn).Name()
scope = scope + " (" + fnName + ")"
if err := fn(pkgDirName, actDirName); err != nil {
pkgErrs[scope] = err
}
}
}
}
return pkgErrs
}