Exemplo n.º 1
0
func ValidateTaskFile(filename string) {
	task, err := parser.ParseFile(filename)
	if err != nil {
		fmt.Printf("\nErrors found in \"%s\"\n", filename)
		fmt.Printf("   %s\n", err.Error())
		return
	}

	// Compile the script text
	_, err = parser.CompileCommandText(task.DefaultCommandBlock, task)
	if err != nil {
		if !parser.IsScriptRun(err) {
			fmt.Printf("\nErrors found in \"%s\"\n", filename)
			fmt.Printf("   %s\n", err.Error())
			return
		}
	}

	if verbose {
		fmt.Printf("\nInformation for Task \"%s\"\n", task.GetMetadata("name"))
		fmt.Printf("  Description: %s\n", task.GetMetadata("description"))
		fmt.Printf("  Author: %s\n", task.GetMetadata("author"))
		fmt.Printf("  Last Changed: %s\n", task.GetMetadata("date"))
		fmt.Printf("  Version: %s\n", task.GetMetadata("version"))

		fmt.Printf("  Concurrent Devices: %d\n", task.Concurrent)
		fmt.Printf("  Template: %s\n", task.Template)
		fmt.Printf("  Inventory File: %s\n\n", task.Inventory)

		fmt.Print("  ----Custom Data----\n")
		for k, v := range task.Metadata {
			if k[0] != '_' {
				continue
			}
			fmt.Printf("  %s: %s\n", k[1:], v)
		}

		fmt.Print("\n  ----Task Device Block----\n")
		for _, d := range task.Devices {
			fmt.Printf("  Device(s): %s\n", d)
		}

		fmt.Print("\n  ----Task Command Blocks----\n")
		for _, c := range task.Commands {
			fmt.Printf("  Command block Name: %s\n", c.Name)
			fmt.Printf("  Command block Type: %s\n", c.Type)
			fmt.Printf("  Commands:\n")
			for _, cmd := range c.Commands {
				fmt.Printf("     %s\n", cmd)
			}
			fmt.Println("  ---------------")
		}
	}
	fmt.Printf("The task named \"%s\" has no syntax errors.\n", task.GetMetadata("name"))
}
Exemplo n.º 2
0
func main() {
	start := time.Now()
	flag.Parse()

	// Set taskmanager package settings
	taskmanager.SetVerbose(verbose)
	taskmanager.SetDebug(debug)
	taskmanager.SetDryRun(dryRun)

	cliArgs := flag.Args()
	cliArgsc := len(cliArgs)

	// Determine what we're doing
	if cliArgsc == 0 {
		printUsage()
		os.Exit(0)
	}

	if err := checkDependencies(); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	command := cliArgs[0]
	if command == "run" && cliArgsc >= 2 { // Run a task file
		for _, file := range cliArgs[1:] {
			// Parse the task file
			task, err := parser.ParseFile(file)
			if err != nil {
				fmt.Println(err.Error())
				continue
			}
			// Inventory from -i flag, overrides task file
			if inventoryFile != "" {
				task.Inventory = inventoryFile
			}
			if task.Inventory == "" {
				task.Inventory = "devices.conf"
			}
			// Set variables given in the command line into the task
			for k, v := range cliVars {
				task.SetUserData(k, v)
			}
			taskmanager.RunTaskFile(task)
		}
	} else if command == "test" && cliArgsc >= 2 { // Test a task file for errors
		for _, file := range cliArgs[1:] {
			taskmanager.ValidateTaskFile(file)
		}
	} else if command == "version" { // Show version info
		os.Exit(0)
	} else if command == "help" { // Show help info
		printUsage()
		os.Exit(0)
	} else if command == "dev" && cliArgsc == 2 { // Dev stuff
		d, err := devices.ParseFile(cliArgs[1])
		if err != nil {
			fmt.Println(err.Error())
		} else {
			for _, group := range d.Groups {
				fmt.Printf("Group: %s\n", group.Name)
				fmt.Printf("Group Settings: %#v\n", group.GetSettings())
				for _, dev := range group.Devices {
					fmt.Printf("   DeviceName: %s\n", dev.Name)
					fmt.Printf("   Device Settings: %#v\n", dev.GetSettings())
				}
				fmt.Println("")
			}
		}
		os.Exit(0)
	} else {
		printUsage()
		os.Exit(0)
	}

	end := time.Since(start).String()
	fmt.Printf("\nExecution completed in %s\n", end)
}