-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is part of a long series of changes. It expands on the sentiment of the Command interface, but with the goal of handing everything as a method of the Manager interface. Why? So its easier to test and change the functionality of the package. Currently its nearly impossible for a consumer of the package to test, as everything is operated using functions, and methods on structs such as Machine. The Manager interface will contain every API call. The code is currently 100% compatible with existing version, and simply builds on the existing code. For convenience, Run() function is exposed, which can be used to run any command using the default manager, while the APIs are being migrated. Once the full migration completes, the previous functions will be deprecated and later removed. This most likely will mean v2 of the library, but it is fair distance away.
- Loading branch information
1 parent
5f2cd96
commit a5915e7
Showing
8 changed files
with
310 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package virtualbox | ||
|
||
//go:generate mockgen -package=virtualbox -source=vbcmd.go -destination=mockvbcmd_test.go |
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,18 @@ | ||
package virtualbox | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// Manager allows to get and edit every property of Virtualbox. | ||
type Manager interface { | ||
// Machine gets the machine by its name or UUID | ||
Machine(context.Context, string) (*Machine, error) | ||
|
||
// ListMachines returns the list of all known machines | ||
ListMachines(context.Context) ([]*Machine, error) | ||
|
||
// UpdateMachine takes in the properties of the machine and applies the | ||
// configuration | ||
UpdateMachine(context.Context, *Machine) error | ||
} |
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,43 @@ | ||
package virtualbox | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
var defaultManager = NewManager() | ||
|
||
// manager implements all the functionality of the Manager, and is the default | ||
// one used. | ||
type manager struct { | ||
// Wrap around the existing code until its migrated | ||
cmd Command | ||
// lock the whole manager to only allow one action at a time | ||
// TODO: Decide is this a good idea, or should we have one mutex per | ||
// type of operation | ||
lock sync.Mutex | ||
} | ||
|
||
// NewManager returns the real instance of the manager | ||
func NewManager() *manager { | ||
return &manager{ | ||
cmd: Manage(), | ||
} | ||
} | ||
|
||
// run is the internal function used by other commands. | ||
func (m *manager) run(ctx context.Context, args ...string) (string, string, error) { | ||
return m.cmd.runOutErrContext(ctx, args...) | ||
} | ||
|
||
// Run is a helper function using the defaultManager and can be used to directly | ||
// run commands which are not exposed as part of the Manager API. It returns the | ||
// stdout, stderr and any errors which happened while executing the command. | ||
// The `VBoxManage` argument should not be specified at the beginning as it is | ||
// deducted from the environment. | ||
// | ||
// Notice: Its possible that if we ever cover the API 1:1, this function might | ||
// be deprecated and later removed. | ||
func Run(ctx context.Context, args ...string) (string, string, error) { | ||
return defaultManager.run(ctx, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package mock | ||
|
||
//go:generate mockgen -package=mock -source=../interface.go -destination=interface_mock.go |
Oops, something went wrong.