Ejemplo n.º 1
0
// runCommands reads and runs all the commands from a file. Return the
// concatenation of all the Responses or an error.
func runCommands(mm *miniclient.Conn, file string) (string, error) {
	var res string
	var err error

	f, err := os.Open(file)
	if err != nil {
		return "", err
	}

	s := bufio.NewScanner(f)

	for s.Scan() {
		// Can't use Compile since we didn't minimega, not minitest, registers
		// handlers with minicli
		cmd := &minicli.Command{Original: s.Text()}

		if len(cmd.Original) > 0 {
			res += fmt.Sprintf("## %v\n", cmd.Original)
		} else {
			res += "\n"
		}

		for resps := range mm.Run(cmd) {
			if err != nil {
				continue
			}

			for _, resp := range resps.Resp {
				if resp.Error != "" {
					res += fmt.Sprintf("E: %v\n", resp.Error)
				}
			}

			if len(resps.Rendered) > 0 {
				res += resps.Rendered + "\n"
			}
		}
	}

	if err := s.Err(); err != nil {
		return "", err
	}

	return res, nil
}
Ejemplo n.º 2
0
// run a command against a live minimega instance, logging the command and the
// response for inspection.
func run(mm *miniclient.Conn, s string) {
	log.Info("minimega: `%v`", s)

	cmd := &minicli.Command{Original: s}

	var res string

	for resps := range mm.Run(cmd) {
		for _, resp := range resps.Resp {
			if resp.Error != "" {
				res += fmt.Sprintf("E: %v\n", resp.Error)
			}
		}

		if len(resps.Rendered) > 0 {
			res += resps.Rendered + "\n"
		}
	}

	log.Debug(res)
}