forked from google/drone-firebase
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.go
111 lines (96 loc) · 2.21 KB
/
plugin.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
package main
import (
"fmt"
"github.com/davecgh/go-spew/spew"
"os"
"os/exec"
"strings"
)
type (
Plugin struct {
Token string
Project string
Message string
Targets string
DryRun bool
Debug bool
}
)
func (p Plugin) Exec() error {
fmt.Printf("Firebase Plugin for Drone built from %s\n", buildCommit)
if p.Debug {
spew.Dump(p)
}
if p.shouldSetProject() {
use := p.buildUse()
if err := execute(use, p.Debug, p.DryRun); err != nil {
return err
}
}
deploy := p.buildDeploy()
if err := execute(deploy, p.Debug, p.DryRun); err != nil {
return err
}
return nil
}
func (p *Plugin) shouldSetProject() bool {
return p.Project != ""
}
func getEnvironment(oldEnv []string, p *Plugin) []string {
var env []string
for _, v := range oldEnv {
if !strings.HasPrefix(v, "DEBUG=") && !strings.HasPrefix(v, "FIREBASE_TOKEN=") {
env = append(env, v)
}
}
env = append(env, fmt.Sprintf("FIREBASE_TOKEN=%s", p.Token))
if p.Debug {
env = append(env, fmt.Sprintf("DEBUG=%s", "true"))
spew.Dump(env)
}
return env
}
// buildUse creates a command on the form:
// $ firebase use ...
func (p *Plugin) buildUse() *exec.Cmd {
var args []string
args = append(args, "use")
if p.Project != "" {
args = append(args, p.Project)
}
cmd := exec.Command("firebase", args...)
cmd.Env = getEnvironment(os.Environ(), p)
return cmd
}
// buildDeploy creates a command on the form:
// $ firebase deploy \
// [--only ...] \
// [--message ...]
func (p *Plugin) buildDeploy() *exec.Cmd {
var args []string
args = append(args, "deploy")
if p.Targets != "" {
args = append(args, "--only")
args = append(args, p.Targets)
}
if p.Message != "" {
args = append(args, "--message")
args = append(args, fmt.Sprintf("\"%s\"", p.Message))
}
cmd := exec.Command("firebase", args...)
cmd.Env = getEnvironment(os.Environ(), p)
return cmd
}
// execute sets the stdout and stderr of the command to be the default, traces
// the command to be executed and returns the result of the command execution.
func execute(cmd *exec.Cmd, debug, dryRun bool) error {
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if dryRun || debug {
fmt.Println("$", strings.Join(cmd.Args, " "))
}
if dryRun {
return nil
}
return cmd.Run()
}