-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(daemonless): initial wip to install deps
- Loading branch information
Showing
11 changed files
with
211 additions
and
92 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,27 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var path string | ||
var installCmd = &cobra.Command{ | ||
Use: "install", | ||
Short: "Install required tools", | ||
Long: "Installs underlying Pact standalone tools for use by the library", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
setLogLevel(verbose, logLevel) | ||
|
||
// Run the installer | ||
fmt.Println("[INFO] Installing required tools...", path) | ||
fmt.Println("[INFO] Checking installation succeeded...") | ||
fmt.Println("[INFO] Tooling installed and up to date!") | ||
}, | ||
} | ||
|
||
func init() { | ||
installCmd.Flags().StringVarP(&path, "path", "p", "/opt/pact", "Local daemon port to listen on") | ||
RootCmd.AddCommand(installCmd) | ||
} |
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,15 @@ | ||
package command | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestInstallCommand(t *testing.T) { | ||
os.Args = []string{"install"} | ||
err := installCmd.Execute() | ||
if err != nil { | ||
t.Fatalf("Error: %v", err) | ||
} | ||
installCmd.Run(nil, os.Args) | ||
} |
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
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,3 @@ | ||
package install | ||
|
||
var defaultPath = "c:/bin" |
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,3 @@ | ||
package install | ||
|
||
var defaultPath = "c:/bin" |
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,3 @@ | ||
package install | ||
|
||
var defaultPath = "/opt" |
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,90 @@ | ||
// Package install contains functions necessary for installing and checking | ||
// if the necessary underlying Ruby tools have been properly installed | ||
package install | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os/exec" | ||
"strings" | ||
|
||
goversion "github.com/hashicorp/go-version" | ||
) | ||
|
||
// Installer manages the underlying Ruby installation | ||
type Installer struct { | ||
} | ||
|
||
const ( | ||
mockServiceRange = ">= 2.6.4, < 3.0.0" | ||
verifierRange = ">= 1.11.0, < 2.0.0" | ||
brokerRange = ">= 1.14.0, < 2.0.0" | ||
) | ||
|
||
var versionMap = map[string]string{ | ||
"pact-mock-service": mockServiceRange, | ||
"pact-provider-verifier": verifierRange, | ||
"pact-broker": brokerRange, | ||
} | ||
|
||
// CheckInstallation checks installation of all of the tools | ||
func (i *Installer) CheckInstallation() error { | ||
|
||
for binary, versionRange := range versionMap { | ||
log.Println("[INFO] checking", binary, "within range", versionRange) | ||
|
||
version, err := i.GetVersionForBinary(binary) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = i.CheckVersion(binary, version); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// CheckVersion checks installation of a given binary using semver-compatible | ||
// comparisions | ||
func (i *Installer) CheckVersion(binary, version string) error { | ||
log.Println("[DEBUG] checking version for binary", binary, "version", version) | ||
v, err := goversion.NewVersion(version) | ||
if err != nil { | ||
log.Println("[DEBUG] err", err) | ||
return err | ||
} | ||
|
||
versionRange, ok := versionMap[binary] | ||
if !ok { | ||
return fmt.Errorf("unable to find version range for binary %s", binary) | ||
} | ||
|
||
log.Println("[DEBUG] checking if version", v, "within semver range", versionRange) | ||
constraints, err := goversion.NewConstraint(versionRange) | ||
if constraints.Check(v) { | ||
log.Println("[DEBUG]", v, "satisfies constraints", v, constraints) | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("version %s does not match constraint %s", version, versionRange) | ||
} | ||
|
||
// InstallTools installs the CLI tools onto the host system | ||
func (i *Installer) InstallTools() error { | ||
log.Println("[INFO] Installing tools") | ||
|
||
return nil | ||
} | ||
|
||
// GetVersionForBinary gets the version of a given Ruby binary | ||
func (i *Installer) GetVersionForBinary(binary string) (version string, err error) { | ||
log.Println("[DEBUG] running binary", binary) | ||
|
||
cmd := exec.Command(binary, "version") | ||
content, err := cmd.Output() | ||
version = string(content) | ||
|
||
return strings.TrimSpace(version), err | ||
} |
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,44 @@ | ||
package install | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestInstaller_CheckVersion(t *testing.T) { | ||
i := Installer{} | ||
err := i.CheckVersion("pact-mock-service", "2.7.3") | ||
|
||
if err != nil { | ||
t.Fatal("error:", err) | ||
} | ||
} | ||
|
||
func TestInstaller_CheckVersionFail(t *testing.T) { | ||
i := Installer{} | ||
err := i.CheckVersion("pact-mock-service", "3.7.3") | ||
|
||
if err == nil { | ||
t.Fatal("want error, got none") | ||
} | ||
} | ||
|
||
func TestInstaller_getVersionForBinary(t *testing.T) { | ||
i := Installer{} | ||
v, err := i.GetVersionForBinary("pact-mock-service") | ||
|
||
if err != nil { | ||
t.Fatal("error:", err) | ||
} | ||
|
||
fmt.Println("version: ", v) | ||
} | ||
|
||
func TestInstaller_CheckInstallation(t *testing.T) { | ||
i := Installer{} | ||
err := i.CheckInstallation() | ||
|
||
if err != nil { | ||
t.Fatal("error:", err) | ||
} | ||
} |