示例#1
0
func printCmd(c *cli.Context) {
	contents, err := ioutil.ReadFile(c.Args().First())
	if err != nil {
		fmt.Printf(hotomata.Colorize("Can't read file: %s", hotomata.ColorRed), c.Args().First())
	}

	machines, err := hotomata.ParseInventory(contents)
	if err != nil {
		fmt.Printf(hotomata.Colorize("%s\n", hotomata.ColorRed), err.Error())
	} else {
		for _, machine := range machines {
			fmt.Printf(hotomata.Colorize("Machine: %s\n", hotomata.ColorMagenta), machine.Name)
			fmt.Printf(hotomata.Colorize("Groups: %v\n", hotomata.ColorBlue), machine.Groups.Names())
			fmt.Print("Variables:\n")

			// Here's the tricky part, lets sort them alphabeticlay
			var pairs = PropertyPairs{}
			for k, v := range machine.Vars() {
				pairs = append(pairs, PropertyPair{k, v})
			}

			sort.Sort(&pairs)
			for _, pair := range pairs {
				valString, err := pair.Value.MarshalJSON()
				if err != nil {
					panic(err) // should never happen it's unmarshaled json
				}
				fmt.Printf("    %s: %s\n", pair.Property, string(valString))
			}

			fmt.Println("")
		}
	}
}
示例#2
0
文件: run.go 项目: snowsnail/hotomata
func run(c *cli.Context) {
	var contents []byte
	var err error

	// Parse inventory arg
	var inventoryFile = c.GlobalString("inventory")
	if inventoryFile == "" {
		writeError("Error: An inventory file is required. e.g. `hotomata --inventory inventory.json`", nil)
	}

	// Parse actual inventory
	contents, err = ioutil.ReadFile(inventoryFile)
	if err != nil {
		writeError("Error: Unable to read inventory file at "+inventoryFile, err)
	}
	inventory, err := hotomata.ParseInventory(contents)
	if err != nil {
		writeError("Error: Unable to parse inventory file, verify your JSON syntax", err)
	}

	// Parse masterplan arg
	var masterPlanFile = c.GlobalString("masterplan")
	if c.Args().First() != "" {
		masterPlanFile = c.Args().First()
	}

	// Parse actual masterplan
	contents, err = ioutil.ReadFile(masterPlanFile)
	if err != nil {
		writeError("Error: Unable to read masterplan file at "+masterPlanFile, err)
	}
	masterplans, err := hotomata.ParseMasterPlan(contents)
	if err != nil {
		writeError("Error: Unable to parse masterplan file, verify your YAML syntax", err)
	}

	// Create a run and parse plans
	run := hotomata.NewRun()
	err = run.DiscoverPlans(c.GlobalString("core-plans-folder"))
	if err != nil {
		writeError("Error: could not load core plans folder at "+c.GlobalString("core-plans-folder"), err)
	}
	err = run.DiscoverPlans(c.GlobalString("plans-folder"))
	if err != nil {
		writeError("Error: could not load plans folder at "+c.GlobalString("plans-folder"), err)
	}

	// load inventory and limit groups
	run.LoadInventory(inventory)
	run.FilterGroups(c.String("group"))

	logger := hotomata.NewLogger(os.Stderr, c.GlobalString("color") == "true", c.GlobalBool("verbose"))
	run.RunMasterPlans(logger, masterplans)
}