-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (81 loc) · 2.28 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
package main
import (
"context"
"fmt"
oapiclient "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
"github.com/kelseyhightower/envconfig"
netlify "github.com/netlify/open-api/go/porcelain"
ooapicontext "github.com/netlify/open-api/go/porcelain/context"
"github.com/sirupsen/logrus"
)
type config struct {
AuthToken string `envconfig:"auth_token" required:"true"`
SiteID string `envconfig:"site_id" required:"true"`
Directory string `required:"true"`
Draft bool `default:"true"`
DeployMessage string `default:""`
LogLevel string `default:"warn"`
LogFormat string `default:"text"`
}
func (c *config) readFromEnv() error {
return envconfig.Process("netlify", c)
}
func main() {
// Base logger
logger := logrus.New()
// Read config
var c config
if err := c.readFromEnv(); err != nil {
logger.Fatalf("failed to read config: %s", err)
}
// Setup logging
if err := setupLogging(&c, logger); err != nil {
logger.Fatal(err)
}
// Netlify setup
client := setupNetlifyClient(&c)
ctx := setupContext(&c, logger)
// Deploy site
resp, err := client.DoDeploy(ctx, &netlify.DeployOptions{
SiteID: c.SiteID,
Dir: c.Directory,
IsDraft: c.Draft,
Title: c.DeployMessage,
}, nil)
if err != nil {
logger.Fatalf("failed to deploy site: %s", err)
}
// Print the site URL
if resp.DeploySslURL != "" {
fmt.Println(resp.DeploySslURL)
} else if resp.DeployURL != "" {
fmt.Println(resp.DeployURL)
}
}
func setupLogging(c *config, logger *logrus.Logger) error {
logLevel, err := logrus.ParseLevel(c.LogLevel)
if err != nil {
return fmt.Errorf("failed to parse log level: %s", err)
}
logger.SetLevel(logLevel)
switch c.LogFormat {
case "text":
logger.SetFormatter(&logrus.TextFormatter{})
case "json":
logger.SetFormatter(&logrus.JSONFormatter{})
default:
logger.Warnf("invalid log format: %s", c.LogFormat)
}
return nil
}
func setupNetlifyClient(c *config) *netlify.Netlify {
formats := strfmt.NewFormats()
return netlify.NewHTTPClient(formats)
}
func setupContext(c *config, logger *logrus.Logger) ooapicontext.Context {
ctx := ooapicontext.WithLogger(context.Background(), logger.WithFields(logrus.Fields{
"source": "netlify",
}))
return ooapicontext.WithAuthInfo(ctx, oapiclient.BearerToken(c.AuthToken))
}