Esempio n. 1
0
func TestArguments(t *testing.T) {
	Convey("Given a clean command line environment", t, func() {
		flag.CommandLine = flag.NewFlagSet("vmlcm", flag.ContinueOnError)

		Convey("whitespace flags should be parsed correctly", func() {
			os.Args = []string{
				"vmlcm",
				"-f", "./agents.json",
				"verify"}

			args, err := util.ParseArguments()
			So(err, ShouldBeNil)
			So(args, ShouldNotBeNil)
			So(args.ConfigPath, ShouldNotBeNil)
			So(*args.ConfigPath, ShouldEqual, "./agents.json")
			So(args.Command, ShouldEqual, util.VerifyCommand)
		})

		Convey("non-whitespace flags should be parsed correctly", func() {
			os.Args = []string{
				"vmlcm",
				"-f=./agents.json",
				"use", "3"}

			args, err := util.ParseArguments()
			So(err, ShouldBeNil)
			So(args, ShouldNotBeNil)
			So(args.ConfigPath, ShouldNotBeNil)
			So(*args.ConfigPath, ShouldEqual, "./agents.json")
			So(args.Command, ShouldEqual, util.UseCommand)
			So(args.CommandIntParameter, ShouldEqual, 3)
		})

		Convey("no arguments should result in an error", func() {
			os.Args = []string{"vmlcm"}

			args, err := util.ParseArguments()
			So(err, ShouldNotBeNil)
			So(args, ShouldBeNil)
		})
	})
}
Esempio n. 2
0
File: main.go Progetto: ankoh/vmlcm
func main() {
	// Create spinner
	spinner := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
	spinner.Color("cyan")
	spinner.Start()

	// First read arguments
	args, argError := util.ParseArguments()
	if argError != nil {
		spinner.Stop()
		fmt.Println(argError.Error())
		return
	}

	// Then read configuration
	config, configError := util.ParseConfiguration(*args.ConfigPath)
	if configError != nil {
		spinner.Stop()
		fmt.Println(configError.Error())
		return
	}

	// Create vmrun wrapper
	var vmrun vmware.VmrunWrapper
	vmrun = vmware.NewCLIVmrun(config.Vmrun)

	// Create output buffer
	buffer := new(bytes.Buffer)

	// Switch commands
	switch args.Command {
	case util.VerifyCommand:
		err := controller.Verify(buffer, vmrun, config)
		if err != nil {
			spinner.Stop()
			fmt.Println(err.Error())
			return
		}
	case util.StatusCommand:
		if controller.Verify(nil, vmrun, config) != nil {
			spinner.Stop()
			fmt.Println("Failed to verify settings. Please run 'verify' to get more details")
			return
		}
		err := controller.Status(buffer, vmrun, config)
		if err != nil {
			spinner.Stop()
			fmt.Println(err.Error())
			return
		}
	case util.UseCommand:
		if controller.Verify(nil, vmrun, config) != nil {
			spinner.Stop()
			fmt.Println("Failed to verify settings. Please run 'verify' to get more details")
			return
		}
		err := controller.Use(buffer, vmrun, config, args.CommandIntParameter)
		if err != nil {
			spinner.Stop()
			fmt.Print(buffer.String())
			fmt.Println(err.Error())
			return
		}
	case util.StartCommand:
		if controller.Verify(nil, vmrun, config) != nil {
			spinner.Stop()
			fmt.Println("Failed to verify settings. Please run 'verify' to get more details")
			return
		}
		err := controller.Start(buffer, vmrun, config)
		if err != nil {
			spinner.Stop()
			fmt.Print(buffer.String())
			fmt.Println(err.Error())
			return
		}
	case util.StopCommand:
		if controller.Verify(nil, vmrun, config) != nil {
			spinner.Stop()
			fmt.Println("Failed to verify settings. Please run 'verify' to get more details")
			return
		}
		err := controller.Stop(buffer, vmrun, config)
		if err != nil {
			spinner.Stop()
			fmt.Print(buffer.String())
			fmt.Println(err.Error())
			return
		}
	case util.SnapshotCommand:
		if controller.Verify(nil, vmrun, config) != nil {
			spinner.Stop()
			fmt.Println("Failed to verify settings. Please run 'verify' to get more details")
			return
		}
		err := controller.Snapshot(buffer, vmrun, config)
		if err != nil {
			spinner.Stop()
			fmt.Print(buffer.String())
			fmt.Println(err.Error())
			return
		}
	}

	// Stop spinner
	spinner.Stop()

	// Print output buffer
	fmt.Print(buffer.String())
}