Skip to content

Commit

Permalink
Run go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBaeumer committed Jul 8, 2019
1 parent 1791d2d commit d87322f
Show file tree
Hide file tree
Showing 41 changed files with 1,046 additions and 1,049 deletions.
50 changes: 25 additions & 25 deletions add.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,21 @@ func (a *Add) AddResource(fileName string, gossConfig GossConfig, resourceName,
fmt.Println(err)
os.Exit(1)
}
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
case "dns":
res, err := gossConfig.DNS.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
case "file":
res, err := gossConfig.Files.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
case "Group":
res, err := gossConfig.Groups.AppendSysResource(key, a.Sys, config)
if err != nil {
Expand All @@ -113,56 +113,56 @@ func (a *Add) AddResource(fileName string, gossConfig GossConfig, resourceName,
fmt.Println(err)
os.Exit(1)
}
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
case "port":
res, err := gossConfig.Ports.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
case "process":
res, err := gossConfig.Processes.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
case "service":
res, err := gossConfig.Services.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
case "user":
res, err := gossConfig.Users.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
case "gossfile":
res, err := gossConfig.Gossfiles.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
case "kernel-param":
res, err := gossConfig.KernelParams.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
case "Mount":
res, err := gossConfig.Mounts.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
case "interface":
res, err := gossConfig.Interfaces.AppendSysResource(key, a.Sys, config)
if err != nil {
Expand All @@ -176,7 +176,7 @@ func (a *Add) AddResource(fileName string, gossConfig GossConfig, resourceName,
fmt.Println(err)
os.Exit(1)
}
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
default:
panic("Undefined resource name: " + resourceName)
}
Expand Down Expand Up @@ -212,28 +212,28 @@ func (a *Add) AutoAddResource(fileName string, gossConfig GossConfig, key string
// file
if strings.Contains(key, "/") {
if res, _, ok := gossConfig.Files.AppendSysResourceIfExists(key, a.Sys); ok == true {
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
}
}

// group
if res, _, ok := gossConfig.Groups.AppendSysResourceIfExists(key, a.Sys); ok == true {
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
}

// package
if res, _, ok := gossConfig.Packages.AppendSysResourceIfExists(key, a.Sys); ok == true {
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
}

// port
if res, _, ok := gossConfig.Ports.AppendSysResourceIfExists(key, a.Sys); ok == true {
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
}

// process
if res, sysres, ok := gossConfig.Processes.AppendSysResourceIfExists(key, a.Sys); ok == true {
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
ports := system.GetPorts(true)
pids, _ := sysres.Pids()
for _, pid := range pids {
Expand All @@ -243,7 +243,7 @@ func (a *Add) AutoAddResource(fileName string, gossConfig GossConfig, key string
if entry.Pid == pidS {
// port
if res, _, ok := gossConfig.Ports.AppendSysResourceIfExists(port, a.Sys); ok == true {
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
}
}
}
Expand All @@ -253,23 +253,23 @@ func (a *Add) AutoAddResource(fileName string, gossConfig GossConfig, key string

// Service
if res, _, ok := gossConfig.Services.AppendSysResourceIfExists(key, a.Sys); ok == true {
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
}

// user
if res, _, ok := gossConfig.Users.AppendSysResourceIfExists(key, a.Sys); ok == true {
a.resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
}

return nil
}

func (a *Add) resourcePrint(fileName string, res resource.ResourceRead) {
resMap := map[string]resource.ResourceRead{res.ID(): res}
resMap := map[string]resource.ResourceRead{res.ID(): res}

oj, _ := marshal(resMap)
typ := reflect.TypeOf(res)
typs := strings.Split(typ.String(), ".")[1]
oj, _ := marshal(resMap)
typ := reflect.TypeOf(res)
typs := strings.Split(typ.String(), ".")[1]

fmt.Fprintf(a.Writer, "Adding %s to '%s':\n\n%s\n\n", typs, fileName, string(oj))
}
}
4 changes: 2 additions & 2 deletions add_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package goss

import (
"github.com/stretchr/testify/assert"
"testing"
"github.com/stretchr/testify/assert"
"testing"
)

func Test_ExtractHeaderArgument(t *testing.T) {
Expand Down
116 changes: 58 additions & 58 deletions app.go
Original file line number Diff line number Diff line change
@@ -1,92 +1,92 @@
package goss

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
)

// GossRunTime represents the global runtime configs which can be set in goss
type GossRunTime struct {
//Gossfile which should holds the test config
Gossfile string
//Vars file which holds the variabesl
Vars string
//Package defines which package manager you want to use, i.e. yum, apt, ...
Package string //this does not belong here imho
//Debug on true will create a more verbose output
Debug bool
//Gossfile which should holds the test config
Gossfile string
//Vars file which holds the variabesl
Vars string
//Package defines which package manager you want to use, i.e. yum, apt, ...
Package string //this does not belong here imho
//Debug on true will create a more verbose output
Debug bool
}

// Serve serves a new health endpoint
func (g *GossRunTime) Serve(endpoint string, handler *HealthHandler) {
handler.Serve(endpoint)
handler.Serve(endpoint)
}

// Validate starts the validation process
func (g *GossRunTime) Validate(v *Validator) int {
return v.Validate(time.Now())
return v.Validate(time.Now())
}

// Render renders a template file
func (g *GossRunTime) Render() (string, error) {
goss, err := os.Open(g.Gossfile)
if err != nil {
return "", fmt.Errorf("Could not open gossfile with error: %s", err.Error())
}
defer goss.Close()
goss, err := os.Open(g.Gossfile)
if err != nil {
return "", fmt.Errorf("Could not open gossfile with error: %s", err.Error())
}
defer goss.Close()

vars, err := os.Open(g.Vars)
if err != nil {
return "", fmt.Errorf("Could not open varsfile with error: %s", err.Error())
}
defer vars.Close()
vars, err := os.Open(g.Vars)
if err != nil {
return "", fmt.Errorf("Could not open varsfile with error: %s", err.Error())
}
defer vars.Close()

return RenderJSON(goss, vars), nil
return RenderJSON(goss, vars), nil
}

// GetGossConfig returns the goss configuration
func (g *GossRunTime) GetGossConfig() GossConfig {
// handle stdin
var fh *os.File
var path, source string
var gossConfig GossConfig
TemplateFilter = NewTemplateFilter(g.Vars)
specFile := g.Gossfile
if specFile == "-" {
source = "STDIN"
fh = os.Stdin
data, err := ioutil.ReadAll(fh)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
OutStoreFormat = getStoreFormatFromData(data)
gossConfig = ReadJSONData(data, true)
} else if specFile == "testing" {
json := []byte(`
// handle stdin
var fh *os.File
var path, source string
var gossConfig GossConfig
TemplateFilter = NewTemplateFilter(g.Vars)
specFile := g.Gossfile
if specFile == "-" {
source = "STDIN"
fh = os.Stdin
data, err := ioutil.ReadAll(fh)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
OutStoreFormat = getStoreFormatFromData(data)
gossConfig = ReadJSONData(data, true)
} else if specFile == "testing" {
json := []byte(`
command:
echo hello:
exit-status: 0
stdout:
- hello
timeout: 10000
`)
gossConfig = ReadJSONData(json, true)
} else {
source = specFile
path = filepath.Dir(specFile)
OutStoreFormat = getStoreFormatFromFileName(specFile)
gossConfig = ReadJSON(specFile)
}
gossConfig = ReadJSONData(json, true)
} else {
source = specFile
path = filepath.Dir(specFile)
OutStoreFormat = getStoreFormatFromFileName(specFile)
gossConfig = ReadJSON(specFile)
}

gossConfig = mergeJSONData(gossConfig, 0, path)
gossConfig = mergeJSONData(gossConfig, 0, path)

if len(gossConfig.Resources()) == 0 {
fmt.Printf("Error: found 0 tests, source: %v\n", source)
os.Exit(1)
}
return gossConfig
}
if len(gossConfig.Resources()) == 0 {
fmt.Printf("Error: found 0 tests, source: %v\n", source)
os.Exit(1)
}
return gossConfig
}
44 changes: 21 additions & 23 deletions app_test.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
package goss

import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"testing"
)

func Test_Render_WithoutGossfile(t *testing.T) {
runtime := GossRunTime{}
result, err := runtime.Render()
runtime := GossRunTime{}
result, err := runtime.Render()

assert.NotNil(t, err)
assert.Equal(t, "Could not open gossfile with error: open : no such file or directory", err.Error())
assert.Empty(t, result)
assert.NotNil(t, err)
assert.Equal(t, "Could not open gossfile with error: open : no such file or directory", err.Error())
assert.Empty(t, result)
}


func Test_Render_WithoutVarsfile(t *testing.T) {
file, err := ioutil.TempFile("", "tmp_gossfile_*.yaml")
defer os.Remove(file.Name())

runtime := GossRunTime{
Gossfile: file.Name(),
Vars: "/invalidpath",
}
result, err := runtime.Render()

assert.NotNil(t, err)
assert.Equal(t, "Could not open varsfile with error: open /invalidpath: no such file or directory", err.Error())
assert.Empty(t, result)
file, err := ioutil.TempFile("", "tmp_gossfile_*.yaml")
defer os.Remove(file.Name())

runtime := GossRunTime{
Gossfile: file.Name(),
Vars: "/invalidpath",
}
result, err := runtime.Render()

assert.NotNil(t, err)
assert.Equal(t, "Could not open varsfile with error: open /invalidpath: no such file or directory", err.Error())
assert.Empty(t, result)
}

Loading

0 comments on commit d87322f

Please sign in to comment.