Skip to content

Commit

Permalink
main: remove duplicated fs call
Browse files Browse the repository at this point in the history
  • Loading branch information
ggarnier committed Nov 30, 2018
1 parent e05275d commit 1a1d661
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 30 deletions.
14 changes: 11 additions & 3 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func deploy(c tsuru.Client, appName string, fs Filesystem, executor exec.Executo
return err
}
}
yamlData, err := loadTsuruYaml(fs)
rawYamlData, err := loadTsuruYamlRaw(fs)
if err != nil {
return err
}
yamlData, err := parseTsuruYaml(rawYamlData)
if err != nil {
return err
}
Expand All @@ -49,7 +53,7 @@ func deploy(c tsuru.Client, appName string, fs Filesystem, executor exec.Executo
if err != nil {
return err
}
fullYamlData, err := loadAllTsuruYaml(fs)
fullYamlData, err := parseAllTsuruYaml(rawYamlData)
if err != nil {
return err
}
Expand Down Expand Up @@ -115,7 +119,11 @@ func inspect(dockerClient *docker.Client, image string, filesystem Filesystem, w
if err != nil {
return fmt.Errorf("failed to inspect image %q: %v", image, err)
}
tsuruYaml, err := loadAllTsuruYaml(filesystem)
rawYamlData, err := loadTsuruYamlRaw(filesystem)
if err != nil {
return err
}
tsuruYaml, err := parseAllTsuruYaml(rawYamlData)
if err != nil {
return fmt.Errorf("failed to load tsuru yaml: %v", err)
}
Expand Down
37 changes: 13 additions & 24 deletions tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,38 +71,27 @@ func execScript(cmds []string, envs []bind.EnvVar, w io.Writer, fs Filesystem, e
return nil
}

func loadTsuruYaml(fs Filesystem) (tsuru.TsuruYaml, error) {
var tsuruYamlData tsuru.TsuruYaml
func loadTsuruYamlRaw(fs Filesystem) ([]byte, error) {
for _, yamlFile := range tsuruYamlFiles {
filePath := fmt.Sprintf("%s/%s", defaultWorkingDir, yamlFile)
tsuruYaml, err := fs.ReadFile(filePath)
if err != nil {
continue
}
err = yaml.Unmarshal(tsuruYaml, &tsuruYamlData)
if err != nil {
return tsuru.TsuruYaml{}, err
if err == nil {
return tsuruYaml, nil
}
break
}
return tsuruYamlData, nil
return nil, fmt.Errorf("error loading tsuru yaml file")
}

func loadAllTsuruYaml(fs Filesystem) (map[string]interface{}, error) {
func parseTsuruYaml(data []byte) (tsuru.TsuruYaml, error) {
var tsuruYamlData tsuru.TsuruYaml
err := yaml.Unmarshal(data, &tsuruYamlData)
return tsuruYamlData, err
}

func parseAllTsuruYaml(data []byte) (map[string]interface{}, error) {
var tsuruYamlData map[string]interface{}
for _, yamlFile := range tsuruYamlFiles {
filePath := fmt.Sprintf("%s/%s", defaultWorkingDir, yamlFile)
tsuruYaml, err := fs.ReadFile(filePath)
if err != nil {
continue
}
err = yaml.Unmarshal(tsuruYaml, &tsuruYamlData)
if err != nil {
return map[string]interface{}{}, err
}
break
}
return tsuruYamlData, nil
err := yaml.Unmarshal(data, &tsuruYamlData)
return tsuruYamlData, err
}

func buildHooks(yamlData tsuru.TsuruYaml, envs []bind.EnvVar, fs Filesystem, executor exec.Executor) error {
Expand Down
63 changes: 60 additions & 3 deletions tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (s *S) TestExecScriptWorkingDirNotExist(c *check.C) {
c.Assert(dir, check.Equals, "/")
}

func (s *S) TestLoadAppYaml(c *check.C) {
func (s *S) TestLoadTsuruYamlRaw(c *check.C) {
tsuruYmlData := `hooks:
build:
- test
Expand All @@ -82,12 +82,33 @@ healthcheck:
method: GET
status: 200
match: .*OK
allowed_failures: 0`
allowed_failures: 0
unknown: ok`
tsuruYmlPath := fmt.Sprintf("%s/%s", defaultWorkingDir, "tsuru.yml")
s.testFS().FileContent = tsuruYmlData
_, err := s.fs.Create(tsuruYmlPath)
c.Assert(err, check.IsNil)
c.Assert(s.testFS().HasAction(fmt.Sprintf("create %s", tsuruYmlPath)), check.Equals, true)
raw, err := loadTsuruYamlRaw(s.fs)
c.Assert(err, check.IsNil)
c.Assert(string(raw), check.DeepEquals, tsuruYmlData)
}

func (s *S) TestParseAppYaml(c *check.C) {
tsuruYmlData := `hooks:
build:
- test
- another_test
restart:
before:
- static
healthcheck:
path: /test
method: GET
status: 200
match: .*OK
allowed_failures: 0
unknown_field: ignored`
expected := tsuru.TsuruYaml{
Hooks: tsuru.Hook{
BuildHooks: []string{"test", "another_test"},
Expand All @@ -103,7 +124,43 @@ healthcheck:
"allowed_failures": float64(0),
},
}
t, err := loadTsuruYaml(s.fs)
t, err := parseTsuruYaml([]byte(tsuruYmlData))
c.Assert(err, check.IsNil)
c.Assert(t, check.DeepEquals, expected)
}

func (s *S) TestParseAllAppYaml(c *check.C) {
tsuruYmlData := `hooks:
build:
- test
- another_test
restart:
before:
- static
healthcheck:
path: /test
method: GET
status: 200
match: .*OK
allowed_failures: 0
unknown_field: mapped`
expected := map[string]interface{}{
"hooks": map[string]interface{}{
"build": []interface{}{"test", "another_test"},
"restart": map[string]interface{}{
"before": []interface{}{"static"},
},
},
"healthcheck": map[string]interface{}{
"path": "/test",
"method": "GET",
"status": float64(200),
"match": ".*OK",
"allowed_failures": float64(0),
},
"unknown_field": "mapped",
}
t, err := parseAllTsuruYaml([]byte(tsuruYmlData))
c.Assert(err, check.IsNil)
c.Assert(t, check.DeepEquals, expected)
}
Expand Down

0 comments on commit 1a1d661

Please sign in to comment.