// runnable ensures all dependencies are satisfied before running box commands func runnable(ccmd *cobra.Command, args []string) { // ensure vagrant exists if exists := Vagrant.Exists(); !exists { fmt.Println("Missing dependency 'Vagrant'. Please download and install it to continue (https://www.vagrantup.com/).") os.Exit(1) } // ensure virtualbox exists if exists := util.VboxExists(); !exists { fmt.Println("Missing dependency 'Virtualbox'. Please download and install it to continue (https://www.virtualbox.org/wiki/Downloads).") os.Exit(1) } }
// boot func boot(ccmd *cobra.Command, args []string) { // ensure vagrant exists before trying to run any nanobox command if exists := Vagrant.Exists(); !exists { fmt.Println("Missing dependency 'Vagrant'. Please download and install it to continue (https://www.vagrantup.com/).") os.Exit(1) } // ensure virtualbox exists before trying to run any nanobox command if exists := util.VboxExists(); !exists { fmt.Println("Missing dependency 'Virtualbox'. Please download and install it to continue (https://www.virtualbox.org/wiki/Downloads).") os.Exit(1) } // ensure a Vagrantfile is available before attempting to boot the VM initialize(nil, args) // get the status to know what needs to happen with the VM status := Vagrant.Status() switch status { // vm is running - do nothing case "running": fmt.Printf(stylish.Bullet("Nanobox is already running")) break // vm is suspended - resume it case "saved": resume(nil, args) // vm is not created - create it case "not created": create(nil, args) // vm is in some unknown state - reload it default: fmt.Printf(stylish.Bullet("Nanobox is in an unknown state (%s). Reloading...", status)) reload(nil, args) } // Server.Lock() // if the background flag is passed, set the mode to "background" if config.Background { config.VMfile.BackgroundIs(true) } }
// test if VboxExists works as intended func TestVboxExists(t *testing.T) { cmd := "vboxmanage" if config.OS == "windows" { if installPath := os.Getenv("VBOX_MSI_INSTALL_PATH"); installPath != "" { cmd = filepath.Join(installPath, cmd) } } exists := false if err := exec.Command(cmd, "-v").Run(); err == nil { exists = true } // testExists := util.VboxExists() if testExists != exists { t.Error("Results don't match!") } }
BoxCmd = &cobra.Command{ Use: "box", Short: "Subcommands for managing the nanobox/boot2docker.box", Long: ``, // ensure all dependencies are satisfied before running box commands PersistentPreRun: func(ccmd *cobra.Command, args []string) { // ensure vagrant exists if exists := Vagrant.Exists(); !exists { fmt.Println("Missing dependency 'Vagrant'. Please download and install it to continue (https://www.vagrantup.com/).") os.Exit(1) } // ensure virtualbox exists if exists := util.VboxExists(); !exists { fmt.Println("Missing dependency 'Virtualbox'. Please download and install it to continue (https://www.virtualbox.org/wiki/Downloads).") os.Exit(1) } }, } Vagrant = vagrant.Default Config = config.Default Util = util.Default ) // func init() { BoxCmd.AddCommand(installCmd) BoxCmd.AddCommand(updateCmd)