Ejemplo n.º 1
0
// Populate the commit buffer with our desired state for collctors
func update_collectors(m2m *xrm2m.M2MClient, collectors *[]Collector) {
	const path_root = "RootCfg.Telemetry.JSON.PolicyGroup(['main']).IPv4Address"

	// Indicate we're going to replace the whole subtree for this Policy
	m2m.Replace(path_root)

	// Set each collector's IP address and port
	for _, collector := range *collectors {
		key, _ := json.Marshal(collector)
		m2m.Set(fmt.Sprintf("%s(%s)", path_root, key), true)
	}

	// For educational purposes, show what changes are about to be committed
	xrm2m_util.PrintChanges(m2m)

	// Commit any changes if there are any
	xrm2m_util.CommitAndPrintOutcome(m2m, "telemetry bot v0.1")
}
Ejemplo n.º 2
0
func enter_shell(m2m *xrm2m.M2MClient) {
	const done = "<done>"
	shell := ishell.NewShell()
	shell.Println("Welcome to the XR M2M CLI")

	// CLI transition functions

	shell.Register("cli_exec", func(args ...string) (string, error) {
		return m2m.CliExec(strings.Join(args, " ")), m2m.Error
	})
	shell.Register("cli_get", func(args ...string) (string, error) {
		return pretty_print(m2m.CliGet(strings.Join(args, " ")), m2m.Error)
	})
	shell.Register("cli_set", func(args ...string) (string, error) {
		m2m.CliSet(strings.Join(args, " "))
		return done, m2m.Error
	})
	shell.Register("write_file", func(args ...string) (string, error) {
		shell.Println("Input multiple lines and end with semicolon ';'.")
		data := shell.ReadMultiLines(";")
		data = strings.TrimSuffix(data, ";") // @@@ bug in ishell
		m2m.WriteFile(args[0], []byte(data))
		return done, m2m.Error
	})

	// Basic schema ops

	shell.Register("get", func(args ...string) (string, error) {
		return pretty_print(m2m.Get(strings.Join(args, " ")), m2m.Error)
	})

	shell.Register("get_children", func(args ...string) (string, error) {
		return pretty_print(m2m.GetChildren(strings.Join(args, " ")), m2m.Error)
	})

	shell.Register("set", func(args ...string) (string, error) {
		m2m.Set(args[0], args[1])
		return done, m2m.Error
	})

	shell.Register("delete", func(args ...string) (string, error) {
		m2m.Delete(strings.Join(args, " "))
		return done, m2m.Error
	})

	shell.Register("replace", func(args ...string) (string, error) {
		m2m.Replace(strings.Join(args, " "))
		return done, m2m.Error
	})

	// Commit operations. Both "comment" and "label" are optional

	shell.Register("commit", func(args ...string) (string, error) {
		comment, label := optional(args)
		return m2m.Commit(comment, label), m2m.Error
	})

	shell.Register("commit_replace", func(args ...string) (string, error) {
		comment, label := optional(args)
		return m2m.CommitReplace(comment, label), m2m.Error
	})

	shell.Register("discard_changes", func(args ...string) (string, error) {
		m2m.DiscardChanges()
		return done, m2m.Error
	})

	shell.Register("get_changes", func(args ...string) (string, error) {
		return pretty_print(m2m.GetChanges(), m2m.Error)
	})

	// Schema

	shell.Register("get_schema", func(args ...string) (string, error) {
		path := args[0]
		var fields string
		if len(args) == 2 {
			fields = args[1]
		}
		return pretty_print(m2m.GetSchema(path, fields), m2m.Error)
	})

	shell.Register("get_version", func(args ...string) (string, error) {
		return pretty_print(m2m.GetVersion(), m2m.Error)
	})

	shell.Start()
}