-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael Persson
committed
May 2, 2016
1 parent
33eaeb7
commit 5874113
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cli | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestHandleExitCoder_nil(t *testing.T) { | ||
exitCode := 0 | ||
called := false | ||
|
||
OsExiter = func(rc int) { | ||
exitCode = rc | ||
called = true | ||
} | ||
|
||
defer func() { OsExiter = os.Exit }() | ||
|
||
HandleExitCoder(nil) | ||
|
||
expect(t, exitCode, 0) | ||
expect(t, called, false) | ||
} | ||
|
||
func TestHandleExitCoder_ExitCoder(t *testing.T) { | ||
exitCode := 0 | ||
called := false | ||
|
||
OsExiter = func(rc int) { | ||
exitCode = rc | ||
called = true | ||
} | ||
|
||
defer func() { OsExiter = os.Exit }() | ||
|
||
HandleExitCoder(NewExitError("galactic perimiter breach", 9)) | ||
|
||
expect(t, exitCode, 9) | ||
expect(t, called, true) | ||
} | ||
|
||
func TestHandleExitCoder_MultiErrorWithExitCoder(t *testing.T) { | ||
exitCode := 0 | ||
called := false | ||
|
||
OsExiter = func(rc int) { | ||
exitCode = rc | ||
called = true | ||
} | ||
|
||
defer func() { OsExiter = os.Exit }() | ||
|
||
exitErr := NewExitError("galactic perimiter breach", 9) | ||
err := NewMultiError(errors.New("wowsa"), errors.New("egad"), exitErr) | ||
HandleExitCoder(err) | ||
|
||
expect(t, exitCode, 9) | ||
expect(t, called, true) | ||
} |
63 changes: 63 additions & 0 deletions
63
vendor/src/github.com/mickep76/iodatafmt/yaml_mapstr/yaml_mapstr.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) 2015-2016 Michael Persson | ||
// Copyright (c) 2012–2015 Elasticsearch <http://www.elastic.co> | ||
// | ||
// Originally distributed as part of "beats" repository (https://github.com/elastic/beats). | ||
// Modified specifically for "iodatafmt" package. | ||
// | ||
// Distributed underneath "Apache License, Version 2.0" which is compatible with the LICENSE for this package. | ||
|
||
package yaml_mapstr | ||
|
||
import ( | ||
// Base packages. | ||
"fmt" | ||
|
||
// Third party packages. | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// Unmarshal YAML to map[string]interface{} instead of map[interface{}]interface{}. | ||
func Unmarshal(in []byte, out interface{}) error { | ||
var res interface{} | ||
|
||
if err := yaml.Unmarshal(in, &res); err != nil { | ||
return err | ||
} | ||
*out.(*interface{}) = cleanupMapValue(res) | ||
|
||
return nil | ||
} | ||
|
||
// Marshal YAML wrapper function. | ||
func Marshal(in interface{}) ([]byte, error) { | ||
return yaml.Marshal(in) | ||
} | ||
|
||
func cleanupInterfaceArray(in []interface{}) []interface{} { | ||
res := make([]interface{}, len(in)) | ||
for i, v := range in { | ||
res[i] = cleanupMapValue(v) | ||
} | ||
return res | ||
} | ||
|
||
func cleanupInterfaceMap(in map[interface{}]interface{}) map[string]interface{} { | ||
res := make(map[string]interface{}) | ||
for k, v := range in { | ||
res[fmt.Sprintf("%v", k)] = cleanupMapValue(v) | ||
} | ||
return res | ||
} | ||
|
||
func cleanupMapValue(v interface{}) interface{} { | ||
switch v := v.(type) { | ||
case []interface{}: | ||
return cleanupInterfaceArray(v) | ||
case map[interface{}]interface{}: | ||
return cleanupInterfaceMap(v) | ||
case string: | ||
return v | ||
default: | ||
return fmt.Sprintf("%v", v) | ||
} | ||
} |