Skip to content

Commit

Permalink
rebase after #463
Browse files Browse the repository at this point in the history
  • Loading branch information
stevendborrelli committed Nov 2, 2016
1 parent 5f7ebc5 commit 52a4e06
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 247 deletions.
61 changes: 6 additions & 55 deletions resource/package/rpm/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,58 +16,24 @@ package rpm

import (
"fmt"
"os/exec"
"syscall"

"github.com/pkg/errors"
"github.com/asteris-llc/converge/resource/package"
)

// PackageVersion is a type alias for a string, since various packages may use
// different naming conventions
type PackageVersion string

// PackageManager describes an interface for managing packages and helps make
// `Check` and `Apply` testable.
type PackageManager interface {
// If the package is installed, returns the version and true, otherwise
// returns an empty string and false.
InstalledVersion(string) (PackageVersion, bool)

// Installs a package, returning an error if something went wrong
InstallPackage(string) (string, error)

// Removes a package, returning an error if something went wrong
RemovePackage(string) (string, error)
}

// SysCaller allows us to mock exec.Command
type SysCaller interface {
Run(string) ([]byte, error)
}

// ExecCaller is a dummy struct to handle wrapping exec.Command in the SysCaller
// interface.
type ExecCaller struct{}

// Run executs `cmd` as a /bin/sh script and returns the output and error
func (e ExecCaller) Run(cmd string) ([]byte, error) {
return exec.Command("sh", "-c", cmd).Output()
}

// YumManager provides a concrete implementation of PackageManager for yum
// packages.
type YumManager struct {
Sys SysCaller
Sys pkg.SysCaller
}

// InstalledVersion gets the installed version of package, if available
func (y *YumManager) InstalledVersion(pkg string) (PackageVersion, bool) {
result, err := y.Sys.Run(fmt.Sprintf("rpm -q %s", pkg))
exitCode, _ := getExitCode(err)
func (y *YumManager) InstalledVersion(p string) (pkg.PackageVersion, bool) {
result, err := y.Sys.Run(fmt.Sprintf("rpm -q %s", p))
exitCode, _ := pkg.GetExitCode(err)
if exitCode != 0 {
return "", false
}
return (PackageVersion)(result), true
return (pkg.PackageVersion)(result), true
}

// InstallPackage installs a package, returning an error if something went wrong
Expand All @@ -84,18 +50,3 @@ func (y *YumManager) RemovePackage(pkg string) (string, error) {
res, err := y.Sys.Run(fmt.Sprintf("yum remove -y %s", pkg))
return string(res), err
}

func getExitCode(err error) (uint32, error) {
if err == nil {
return 0, nil
}
exitErr, ok := err.(*exec.ExitError)
if !ok {
return 255, errors.Wrap(err, "not a valid exitError")
}
status, ok := exitErr.Sys().(syscall.WaitStatus)
if !ok {
return 255, errors.Wrap(err, "failed to get sys waitstatus")
}
return uint32(status.ExitStatus()), nil
}
7 changes: 4 additions & 3 deletions resource/package/rpm/preparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package rpm
import (
"github.com/asteris-llc/converge/load/registry"
"github.com/asteris-llc/converge/resource"
"github.com/asteris-llc/converge/resource/package"
)

// Preparer for RPM Package
Expand All @@ -33,7 +34,7 @@ type Preparer struct {
State State `hcl:"state" valid_values:"present,absent"`
}

// Prepare a new packge
// Prepare a new package
func (p *Preparer) Prepare(render resource.Renderer) (resource.Task, error) {
if p.State == "" {
p.State = "present"
Expand All @@ -42,10 +43,10 @@ func (p *Preparer) Prepare(render resource.Renderer) (resource.Task, error) {
return &Package{
Name: p.Name,
State: p.State,
PkgMgr: &YumManager{Sys: ExecCaller{}},
PkgMgr: &YumManager{Sys: pkg.ExecCaller{}},
}, nil
}

func init() {
registry.Register("package.rpm", (*Preparer)(nil), (*Package)(nil))
registry.Register("package.rpm", (*Preparer)(nil), (*pkg.Package)(nil))
}
82 changes: 0 additions & 82 deletions resource/package/rpm/rpm.go

This file was deleted.

107 changes: 0 additions & 107 deletions resource/package/rpm/rpm_test.go

This file was deleted.

0 comments on commit 52a4e06

Please sign in to comment.