Skip to content

Commit

Permalink
handle errors on main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
andrestc committed Mar 29, 2018
1 parent 8f9ee35 commit 9db2be0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
29 changes: 10 additions & 19 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,43 @@
package main

import (
"log"

"github.com/tsuru/deploy-agent/internal/tsuru"
"github.com/tsuru/tsuru/exec"
)

func build(c tsuru.Client, appName string, cmd []string, fs Filesystem, executor exec.Executor) {
log.SetFlags(0)
func build(c tsuru.Client, appName string, cmd []string, fs Filesystem, executor exec.Executor) error {
envs, err := c.GetAppEnvs(appName)
if err != nil {
log.Fatal(err)
}
err = execScript(cmd, envs, nil, fs, executor)
if err != nil {
log.Fatal(err)
return err
}
return execScript(cmd, envs, nil, fs, executor)
}

func deploy(c tsuru.Client, appName string, fs Filesystem, executor exec.Executor) {
log.SetFlags(0)
func deploy(c tsuru.Client, appName string, fs Filesystem, executor exec.Executor) error {
var yamlData tsuru.TsuruYaml
envs, err := c.RegisterUnit(appName, yamlData)
if err != nil {
log.Fatal(err)
return err
}
diff, firstDeploy, err := readDiffDeploy(fs)
if !firstDeploy || err != nil {
err = c.SendDiffDeploy(diff, appName)
if err != nil {
log.Fatal(err)
return err
}
}
yamlData, err = loadTsuruYaml(fs)
if err != nil {
log.Fatal(err)
return err
}
err = buildHooks(yamlData, envs, fs, executor)
if err != nil {
log.Fatal(err)
return err
}
err = loadProcesses(&yamlData, fs)
if err != nil {
log.Fatal(err)
return err
}
_, err = c.RegisterUnit(appName, yamlData)
if err != nil {
log.Fatal(err)
}
return err
}
6 changes: 4 additions & 2 deletions deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func (s *S) TestBuild(c *check.C) {
URL: server.URL,
Token: "fake-token",
}
build(client, "app1", []string{"ls"}, s.fs, s.exec)
err := build(client, "app1", []string{"ls"}, s.fs, s.exec)
c.Assert(err, check.IsNil)
}

func (s *S) TestDeploy(c *check.C) {
Expand Down Expand Up @@ -72,5 +73,6 @@ func (s *S) TestDeploy(c *check.C) {
URL: server.URL,
Token: "fake-token",
}
deploy(client, "app1", s.fs, s.exec)
err = deploy(client, "app1", s.fs, s.exec)
c.Assert(err, check.IsNil)
}
15 changes: 11 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,25 @@ func main() {
}()
}

var err error
switch command[len(command)-1] {
case "build":
build(c, appName, command[:len(command)-1], filesystem, executor)
err = build(c, appName, command[:len(command)-1], filesystem, executor)
case "deploy-only":
deploy(c, appName, filesystem, executor)
err = deploy(c, appName, filesystem, executor)
case "deploy":
// backward compatibility with tsuru < 1.4.0
command = command[:len(command)-1]
fallthrough
default:
build(c, appName, command, filesystem, executor)
deploy(c, appName, filesystem, executor)
err = build(c, appName, command, filesystem, executor)
if err != nil {
break
}
err = deploy(c, appName, filesystem, executor)
}
if err != nil {
fatal("[deploy-agent] error: %v", err)
}
}

Expand Down

0 comments on commit 9db2be0

Please sign in to comment.