// Upddate a telemetry policy. Assume we use the policy name as filename func UpdateTelemetryPolicy(m2m *xrm2m.M2MClient, policy *TelemetryPolicy) { if m2m.Error != nil { return } path := "/telemetry/policies/" + policy.Name + ".policy" data, err := json.MarshalIndent(policy, "", " ") if err != nil { m2m.Error = err m2m.LastOp = "UpdateTelemetryPolicy" return } m2m.WriteFile(path, data) }
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() }