Esempio n. 1
0
// Print any changes that would be effected with a commit to stdout
func PrintChanges(m2m *xrm2m.M2MClient) {
	changes := m2m.GetChanges()
	if len(changes) > 0 {
		fmt.Println("Changes to be committed:\n")
		for _, change := range changes {
			for key, value := range change {
				fmt.Printf("%10s: %v\n", key, value)
			}
			fmt.Println("")
		}
	}
}
Esempio 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()
}