Skip to content

Commit

Permalink
Merge pull request #55 from SimonBaeumer/remove-cli-dependency
Browse files Browse the repository at this point in the history
Replace cli.Context with app.CliContext
  • Loading branch information
SimonBaeumer authored Jul 8, 2019
2 parents 4e92c91 + da20142 commit 38c338e
Show file tree
Hide file tree
Showing 65 changed files with 1,664 additions and 755 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# v0.6.0

- Fixed a bug where DNS record lookups never were fired wihout a nameserver
- Removed global `--package` option, added `package-manager` property to `package` resource
- Removed code dependency on `*cli.Context`

# v0.5.0

Expand Down
187 changes: 101 additions & 86 deletions add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,42 @@ package goss

import (
"fmt"
"github.com/SimonBaeumer/goss/resource"
"github.com/SimonBaeumer/goss/system"
"github.com/SimonBaeumer/goss/util"
"io"
"os"
"reflect"
"strconv"
"strings"
"time"

"github.com/SimonBaeumer/goss/system"
"github.com/SimonBaeumer/goss/util"
"github.com/urfave/cli"
)

type Add struct {
Writer io.Writer
ExcludeAttr []string
Timeout int
AllowInsecure bool
NoFollowRedirects bool
Server string
Username string
Password string
Header string
Sys *system.System
}

// AddResources is a sSimple wrapper to add multiple resources
func AddResources(fileName, resourceName string, keys []string, c *cli.Context) error {
func (a *Add) AddResources(fileName, resourceName string, keys []string) error {
OutStoreFormat = getStoreFormatFromFileName(fileName)
header := extractHeaderArgument(c.String("header"))
header := extractHeaderArgument(a.Header)

config := util.Config{
IgnoreList: c.GlobalStringSlice("exclude-attr"),
Timeout: int(c.Duration("timeout") / time.Millisecond),
AllowInsecure: c.Bool("insecure"),
NoFollowRedirects: c.Bool("no-follow-redirects"),
Server: c.String("server"),
Username: c.String("username"),
Password: c.String("password"),
IgnoreList: a.ExcludeAttr,
Timeout: a.Timeout,
AllowInsecure: a.AllowInsecure,
NoFollowRedirects: a.NoFollowRedirects,
Server: a.Server,
Username: a.Username,
Password: a.Password,
Header: header,
}

Expand All @@ -35,10 +48,8 @@ func AddResources(fileName, resourceName string, keys []string, c *cli.Context)
gossConfig = *NewGossConfig()
}

sys := system.New(c)

for _, key := range keys {
if err := AddResource(fileName, gossConfig, resourceName, key, c, config, sys); err != nil {
if err := a.AddResource(fileName, gossConfig, resourceName, key, config); err != nil {
return err
}
}
Expand All @@ -58,114 +69,114 @@ func extractHeaderArgument(headerArg string) map[string][]string {
}

// AddResource adds a resource to the configuration file
func AddResource(fileName string, gossConfig GossConfig, resourceName, key string, c *cli.Context, config util.Config, sys *system.System) error {
func (a *Add) AddResource(fileName string, gossConfig GossConfig, resourceName, key string, config util.Config) error {
// Need to figure out a good way to refactor this
switch resourceName {
case "Addr":
res, err := gossConfig.Addrs.AppendSysResource(key, sys, config)
case "addr":
res, err := gossConfig.Addrs.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "Command":
res, err := gossConfig.Commands.AppendSysResource(key, sys, config)
a.resourcePrint(fileName, res)
case "command":
res, err := gossConfig.Commands.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "DNS":
res, err := gossConfig.DNS.AppendSysResource(key, sys, config)
a.resourcePrint(fileName, res)
case "dns":
res, err := gossConfig.DNS.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "File":
res, err := gossConfig.Files.AppendSysResource(key, sys, config)
a.resourcePrint(fileName, res)
case "file":
res, err := gossConfig.Files.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
case "Group":
res, err := gossConfig.Groups.AppendSysResource(key, sys, config)
res, err := gossConfig.Groups.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "Package":
res, err := gossConfig.Packages.AppendSysResource(key, sys, config)
a.resourcePrint(fileName, res)
case "package":
res, err := gossConfig.Packages.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "Port":
res, err := gossConfig.Ports.AppendSysResource(key, sys, config)
a.resourcePrint(fileName, res)
case "port":
res, err := gossConfig.Ports.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "Process":
res, err := gossConfig.Processes.AppendSysResource(key, sys, config)
a.resourcePrint(fileName, res)
case "process":
res, err := gossConfig.Processes.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "Service":
res, err := gossConfig.Services.AppendSysResource(key, sys, config)
a.resourcePrint(fileName, res)
case "service":
res, err := gossConfig.Services.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "User":
res, err := gossConfig.Users.AppendSysResource(key, sys, config)
a.resourcePrint(fileName, res)
case "user":
res, err := gossConfig.Users.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "Gossfile":
res, err := gossConfig.Gossfiles.AppendSysResource(key, sys, config)
a.resourcePrint(fileName, res)
case "gossfile":
res, err := gossConfig.Gossfiles.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "KernelParam":
res, err := gossConfig.KernelParams.AppendSysResource(key, sys, config)
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)
}
resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
case "Mount":
res, err := gossConfig.Mounts.AppendSysResource(key, sys, config)
res, err := gossConfig.Mounts.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "Interface":
res, err := gossConfig.Interfaces.AppendSysResource(key, sys, config)
a.resourcePrint(fileName, res)
case "interface":
res, err := gossConfig.Interfaces.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
case "HTTP":
res, err := gossConfig.HTTPs.AppendSysResource(key, sys, config)
a.resourcePrint(fileName, res)
case "http":
res, err := gossConfig.HTTPs.AppendSysResource(key, a.Sys, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
resourcePrint(fileName, res)
a.resourcePrint(fileName, res)
default:
panic("Undefined resource name: " + resourceName)
}
Expand All @@ -174,12 +185,8 @@ func AddResource(fileName string, gossConfig GossConfig, resourceName, key strin
}

// Simple wrapper to add multiple resources
func AutoAddResources(fileName string, keys []string, c *cli.Context) error {
func (a *Add) AutoAddResources(fileName string, keys []string) error {
OutStoreFormat = getStoreFormatFromFileName(fileName)
config := util.Config{
IgnoreList: c.GlobalStringSlice("exclude-attr"),
Timeout: int(c.Duration("timeout") / time.Millisecond),
}

var gossConfig GossConfig
if _, err := os.Stat(fileName); err == nil {
Expand All @@ -188,10 +195,8 @@ func AutoAddResources(fileName string, keys []string, c *cli.Context) error {
gossConfig = *NewGossConfig()
}

sys := system.New(c)

for _, key := range keys {
if err := AutoAddResource(fileName, gossConfig, key, c, config, sys); err != nil {
if err := a.AutoAddResource(fileName, gossConfig, key); err != nil {
return err
}
}
Expand All @@ -203,32 +208,32 @@ func AutoAddResources(fileName string, keys []string, c *cli.Context) error {
}

// Autoadds all resources to the config file
func AutoAddResource(fileName string, gossConfig GossConfig, key string, c *cli.Context, config util.Config, sys *system.System) error {
func (a *Add) AutoAddResource(fileName string, gossConfig GossConfig, key string) error {
// file
if strings.Contains(key, "/") {
if res, _, ok := gossConfig.Files.AppendSysResourceIfExists(key, sys); ok == true {
resourcePrint(fileName, res)
if res, _, ok := gossConfig.Files.AppendSysResourceIfExists(key, a.Sys); ok == true {
a.resourcePrint(fileName, res)
}
}

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

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

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

// process
if res, sysres, ok := gossConfig.Processes.AppendSysResourceIfExists(key, sys); ok == true {
resourcePrint(fileName, res)
if res, sysres, ok := gossConfig.Processes.AppendSysResourceIfExists(key, a.Sys); ok == true {
a.resourcePrint(fileName, res)
ports := system.GetPorts(true)
pids, _ := sysres.Pids()
for _, pid := range pids {
Expand All @@ -237,8 +242,8 @@ func AutoAddResource(fileName string, gossConfig GossConfig, key string, c *cli.
for _, entry := range entries {
if entry.Pid == pidS {
// port
if res, _, ok := gossConfig.Ports.AppendSysResourceIfExists(port, sys); ok == true {
resourcePrint(fileName, res)
if res, _, ok := gossConfig.Ports.AppendSysResourceIfExists(port, a.Sys); ok == true {
a.resourcePrint(fileName, res)
}
}
}
Expand All @@ -247,14 +252,24 @@ func AutoAddResource(fileName string, gossConfig GossConfig, key string, c *cli.
}

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

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

return nil
}

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

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
Loading

0 comments on commit 38c338e

Please sign in to comment.