main.go 2.28 KB
Newer Older
Jaakko Pallari's avatar
Jaakko Pallari committed
1
2
3
package main

import (
Jaakko Pallari's avatar
Jaakko Pallari committed
4
	"context"
Jaakko Pallari's avatar
Jaakko Pallari committed
5
6
7
8
9
10
	"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"
Jaakko Pallari's avatar
Jaakko Pallari committed
11
	ooapicontext "github.com/netlify/open-api/go/porcelain/context"
Jaakko Pallari's avatar
Jaakko Pallari committed
12
13
14
15
	"github.com/sirupsen/logrus"
)

type config struct {
Jaakko Pallari's avatar
Jaakko Pallari committed
16
17
18
19
20
21
22
	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"`
Jaakko Pallari's avatar
Jaakko Pallari committed
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
}

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,
Jaakko Pallari's avatar
Jaakko Pallari committed
53
		Title:   c.DeployMessage,
Jaakko Pallari's avatar
Jaakko Pallari committed
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
	}, 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)
}

Jaakko Pallari's avatar
Jaakko Pallari committed
90
91
func setupContext(c *config, logger *logrus.Logger) ooapicontext.Context {
	ctx := ooapicontext.WithLogger(context.Background(), logger.WithFields(logrus.Fields{
Jaakko Pallari's avatar
Jaakko Pallari committed
92
93
		"source": "netlify",
	}))
Jaakko Pallari's avatar
Jaakko Pallari committed
94
	return ooapicontext.WithAuthInfo(ctx, oapiclient.BearerToken(c.AuthToken))
Jaakko Pallari's avatar
Jaakko Pallari committed
95
}