Skip to content

Commit

Permalink
Remove global package option, instead add package-manager property
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBaeumer committed May 16, 2019
1 parent f971032 commit 623e87d
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 75 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 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

# v0.5.0

Expand Down
4 changes: 2 additions & 2 deletions add.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func AddResources(fileName, resourceName string, keys []string, ctx app.CliConte
gossConfig = *NewGossConfig()
}

sys := system.New(ctx.Package)
sys := system.New()

for _, key := range keys {
if err := AddResource(fileName, gossConfig, resourceName, key, config, sys); err != nil {
Expand Down Expand Up @@ -182,7 +182,7 @@ func AutoAddResources(fileName string, keys []string, ctx app.CliContext) error
gossConfig = *NewGossConfig()
}

sys := system.New(ctx.Package)
sys := system.New()

for _, key := range keys {
if err := AutoAddResource(fileName, gossConfig, key, sys); err != nil {
Expand Down
11 changes: 1 addition & 10 deletions cmd/goss/goss.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func main() {
Cache: cache.New(ctx.Cache, 30*time.Second),
ListenAddr: ctx.ListenAddr,
Outputer: outputs.GetOutputer(ctx.Format),
Sys: system.New(ctx.Package),
Sys: system.New(),
GossMu: &sync.Mutex{},
MaxConcurrent: ctx.MaxConcurrent,
GossConfig: gossRunTime.GetGossConfig(),
Expand Down Expand Up @@ -214,15 +214,6 @@ func main() {
},
},
Subcommands: []cli.Command{
{
Name: "package",
Usage: "add new package",
Action: func(c *cli.Context) error {
ctx := app2.NewCliContext(c)
goss.AddResources(c.GlobalString("gossfile"), "Package", c.Args(), ctx)
return nil
},
},
{
Name: "file",
Usage: "add new file",
Expand Down
3 changes: 1 addition & 2 deletions docs/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -702,12 +702,11 @@ package:
# required attributes
installed: true
# optional attributes
package: pacman #set a specific package manager
versions:
- 2.2.15
```

**NOTE:** this check uses the `--package <format>` parameter passed on the command line.


### port
Validates the state of a local port.
Expand Down
1 change: 0 additions & 1 deletion outputs/rspecish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@ Count: 1, Failed: 0, Skipped: 0
assert.Equal(t, expectedJson, b.String())
assert.Equal(t, 0, r)
}

13 changes: 7 additions & 6 deletions resource/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
)

type Package struct {
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Name string `json:"-" yaml:"-"`
Installed matcher `json:"installed" yaml:"installed"`
Versions matcher `json:"versions,omitempty" yaml:"versions,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Name string `json:"-" yaml:"-"`
Installed matcher `json:"installed" yaml:"installed"`
Versions matcher `json:"versions,omitempty" yaml:"versions,omitempty"`
PackageManager string `json:"package-manager,omitempty" yaml:"package-manager,omitempty"`
}

func (p *Package) ID() string { return p.Name }
Expand All @@ -21,7 +22,7 @@ func (p *Package) GetMeta() meta { return p.Meta }

func (p *Package) Validate(sys *system.System) []TestResult {
skip := false
sysPkg := sys.NewPackage(p.Name, sys, util.Config{})
sysPkg := sys.NewPackage(p.Name, p.PackageManager)

var results []TestResult
results = append(results, ValidateValue(p, "installed", p.Installed, sysPkg.Installed, skip))
Expand Down
4 changes: 2 additions & 2 deletions resource/resource_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ func (ret *GroupMap) UnmarshalYAML(unmarshal func(v interface{}) error) error {
type PackageMap map[string]*Package

func (r PackageMap) AppendSysResource(sr string, sys *system.System, config util.Config) (*Package, error) {
sysres := sys.NewPackage(sr, sys, config)
sysres := sys.NewPackage(sr, "")
res, err := NewPackage(sysres, config)
if err != nil {
return nil, err
Expand All @@ -635,7 +635,7 @@ func (r PackageMap) AppendSysResource(sr string, sys *system.System, config util
}

func (r PackageMap) AppendSysResourceIfExists(sr string, sys *system.System) (*Package, system.Package, bool) {
sysres := sys.NewPackage(sr, sys, util.Config{})
sysres := sys.NewPackage(sr, "")
// FIXME: Do we want to be silent about errors?
res, _ := NewPackage(sysres, util.Config{})
if e, _ := sysres.Exists(); e != true {
Expand Down
2 changes: 1 addition & 1 deletion serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (h HealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if found {
resp = tmp.(res)
} else {
h.Sys = system.New(h.C.Package)
h.Sys = system.New()
log.Printf("%v: Stale Cache, running tests", r.RemoteAddr)
iStartTime := time.Now()
out := validate(h.Sys, h.GossConfig, h.MaxConcurrent)
Expand Down
43 changes: 43 additions & 0 deletions system/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,46 @@ func (p *NullPackage) Installed() (bool, error) {
func (p *NullPackage) Versions() ([]string, error) {
return nil, ErrNullPackage
}

// DetectPackageManager attempts to detect whether or not the system is using
// "deb", "rpm", "apk", or "pacman" package managers. It first attempts to
// detect the distro. If that fails, it falls back to finding package manager
// executables. If that fails, it returns the empty string.
func DetectPackageManager() string {
switch DetectDistro() {
case "ubuntu":
return "deb"
case "redhat":
return "rpm"
case "alpine":
return "apk"
case "arch":
return "pacman"
case "debian":
return "deb"
}
for _, manager := range []string{"deb", "rpm", "apk", "pacman"} {
if HasCommand(manager) {
return manager
}
}
return ""
}

// NewPackage is the constructor method which creates the correct package manager
// If pkgManager is empty the package manager will be automatically detected
func NewPackage(name string, pkgManager string) Package {
if pkgManager != "deb" && pkgManager != "apk" && pkgManager != "pacman" && pkgManager != "rpm" {
pkgManager = DetectPackageManager()
}
switch pkgManager {
case "deb":
return NewDebPackage(name)
case "apk":
return NewAlpinePackage(name)
case "pacman":
return NewPacmanPackage(name)
default:
return NewRpmPackage(name)
}
}
2 changes: 1 addition & 1 deletion system/package_alpine.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type AlpinePackage struct {
installed bool
}

func NewAlpinePackage(name string, system *System, config util.Config) Package {
func NewAlpinePackage(name string) Package {
return &AlpinePackage{name: name}
}

Expand Down
2 changes: 1 addition & 1 deletion system/package_deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type DebPackage struct {
installed bool
}

func NewDebPackage(name string, system *System, config util.Config) Package {
func NewDebPackage(name string) Package {
return &DebPackage{name: name}
}

Expand Down
8 changes: 7 additions & 1 deletion system/package_pacman.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
"github.com/SimonBaeumer/goss/util"
)

//PackmanPackage represents a package inside the pacman manager
type PacmanPackage struct {
name string
versions []string
loaded bool
installed bool
}

func NewPacmanPackage(name string, system *System, config util.Config) Package {
//NewPacmanPackage creates a new pacman manager
func NewPacmanPackage(name string) Package {
return &PacmanPackage{name: name}
}

Expand All @@ -34,18 +36,22 @@ func (p *PacmanPackage) setup() {
p.versions = []string{strings.Fields(cmd.Stdout.String())[1]}
}

// Name returns the name of the package
func (p *PacmanPackage) Name() string {
return p.name
}

// Exists returns if the package is installed
func (p *PacmanPackage) Exists() (bool, error) { return p.Installed() }

// Installed will check and returns if the package is installed
func (p *PacmanPackage) Installed() (bool, error) {
p.setup()

return p.installed, nil
}

// Versions returns all installed versions of the package
func (p *PacmanPackage) Versions() ([]string, error) {
p.setup()
if len(p.versions) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion system/package_rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type RpmPackage struct {
installed bool
}

func NewRpmPackage(name string, system *System, config util.Config) Package {
func NewRpmPackage(name string) Package {
return &RpmPackage{name: name}
}

Expand Down
25 changes: 25 additions & 0 deletions system/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package system

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

func TestNewPackage(t *testing.T) {
deb := NewPackage("package", "deb")
rpm := NewPackage("package", "rpm")
pac := NewPackage("package", "pacman")
apk := NewPackage("package", "apk")

assert.Implements(t, new(Package), deb)
assert.IsType(t, &DebPackage{}, deb)

assert.Implements(t, new(Package), rpm)
assert.IsType(t, &RpmPackage{}, rpm)

assert.Implements(t, new(Package), pac)
assert.IsType(t, &PacmanPackage{}, pac)

assert.Implements(t, new(Package), apk)
assert.IsType(t, &AlpinePackage{}, apk)
}
48 changes: 3 additions & 45 deletions system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Resource interface {

// System holds all constructor functions for each
type System struct {
NewPackage func(string, *System, util2.Config) Package
NewPackage func(string, string) Package
NewFile func(string, *System, util2.Config) File
NewAddr func(string, *System, util2.Config) Addr
NewPort func(string, *System, util2.Config) Port
Expand Down Expand Up @@ -55,8 +55,9 @@ func (s *System) ProcMap() map[string][]ps.Process {
}

//New creates the system object which holds all constructors for the system packages
func New(packageManger string) *System {
func New() *System {
sys := &System{
NewPackage: NewPackage,
NewFile: NewDefFile,
NewAddr: NewDefAddr,
NewPort: NewDefPort,
Expand All @@ -72,27 +73,9 @@ func New(packageManger string) *System {
NewHTTP: NewDefHTTP,
}
sys.detectService()
sys.detectPackage(packageManger)
return sys
}

// DetectPackage adds the correct package creation function to a System struct
func (sys *System) detectPackage(pkgManager string) {
if pkgManager != "deb" && pkgManager != "apk" && pkgManager != "pacman" && pkgManager != "rpm" {
pkgManager = DetectPackageManager()
}
switch pkgManager {
case "deb":
sys.NewPackage = NewDebPackage
case "apk":
sys.NewPackage = NewAlpinePackage
case "pacman":
sys.NewPackage = NewPacmanPackage
default:
sys.NewPackage = NewRpmPackage
}
}

// DetectService adds the correct service creation function to a System struct
func (sys *System) detectService() {
switch DetectService() {
Expand All @@ -107,31 +90,6 @@ func (sys *System) detectService() {
}
}

// DetectPackageManager attempts to detect whether or not the system is using
// "deb", "rpm", "apk", or "pacman" package managers. It first attempts to
// detect the distro. If that fails, it falls back to finding package manager
// executables. If that fails, it returns the empty string.
func DetectPackageManager() string {
switch DetectDistro() {
case "ubuntu":
return "deb"
case "redhat":
return "rpm"
case "alpine":
return "apk"
case "arch":
return "pacman"
case "debian":
return "deb"
}
for _, manager := range []string{"deb", "rpm", "apk", "pacman"} {
if HasCommand(manager) {
return manager
}
}
return ""
}

// DetectService attempts to detect what kind of service management the system
// is using, "systemd", "upstart", "alpineinit", or "init". It looks for systemctl
// command to detect systemd, and falls back on DetectDistro otherwise. If it can't
Expand Down
4 changes: 2 additions & 2 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (v *Validator) Validate(startTime time.Time) int {
FormatOptions: v.FormatOptions,
}

sys := system.New(v.Package)
sys := system.New()

i := 1
for {
Expand All @@ -56,7 +56,7 @@ func (v *Validator) Validate(startTime time.Time) int {
color.Red("Retrying in %s (elapsed/timeout time: %.3fs/%s)\n\n\n", v.Sleep, elapsed.Seconds(), v.RetryTimeout)

// Reset Cache
sys = system.New(v.Package)
sys = system.New()
time.Sleep(v.Sleep)
i++
fmt.Printf("Attempt #%d:\n", i)
Expand Down

0 comments on commit 623e87d

Please sign in to comment.