func topicTruncate(topic string) { j := tat.TopicNameJSON{Topic: topic} if force { out, err := internal.Client().TopicTruncate(j) internal.Check(err) if internal.Verbose { internal.Print(out) } } else { fmt.Print("Are you really sure ? You will delete all messages even if a user has a message in his tasks. Please enter again topic name to confirm: ") var confirmTopic string fmt.Scanln(&confirmTopic) if confirmTopic == topic { fmt.Printf("Please enter 'yes' to confirm removing all messages from %s: ", topic) var confirmYes string fmt.Scanln(&confirmYes) if confirmYes == "yes" { out, err := internal.Client().TopicTruncate(j) internal.Check(err) internal.Print(out) return } } else { fmt.Printf("Error. You enter %s instead of %s\n", confirmTopic, topic) } fmt.Println("Nothing done") } }
func writeTemplate() { var templateJSON TemplateJSONType if viper.GetString("username") != "" { templateJSON.Username = viper.GetString("username") } if viper.GetString("password") != "" { templateJSON.Password = viper.GetString("password") } if viper.GetString("url") != "" { templateJSON.URL = viper.GetString("url") } if viper.GetString("tatwebui-url") != "" { templateJSON.TatwebuiURL = viper.GetString("tatwebui-url") } jsonStr, err := json.MarshalIndent(templateJSON, "", " ") internal.Check(err) jsonStr = append(jsonStr, '\n') filename := internal.ConfigFile dir := path.Dir(filename) if _, err := os.Stat(dir); os.IsNotExist(err) { internal.Check(os.Mkdir(dir, 0740)) } internal.Check(ioutil.WriteFile(filename, jsonStr, 0600)) fmt.Printf("%s is written\n", filename) }
func socketRead(c *websocket.Conn) { for { _, r, err := c.ReadMessage() internal.Check(err) fmt.Print(color(string(r))) } }
func newClient() *websocket.Conn { internal.ReadConfig() if viper.GetString("url") == "" { fmt.Fprintf(os.Stderr, "Invalid Configuration: invalid URL. See tatcli config --help\n") os.Exit(1) } i := strings.Index(viper.GetString("url"), ":") endURL := viper.GetString("url")[i:] + "/socket/ws" url := "ws" + endURL dialer := websocket.DefaultDialer if internal.IsHTTPS() { url = "wss" + endURL dialer = &websocket.Dialer{ TLSClientConfig: internal.GetTLSConfig(), HandshakeTimeout: time.Duration(time.Duration(time.Second) * 9), } } r, _ := http.NewRequest("GET", url, nil) r.Header.Add("Content-Type", "application/json") c, _, err := dialer.Dial(url, r.Header) internal.Check(err) fmt.Printf("Connected to %s [Ctrl+c to quit]\n", url) wsActionConnect(c) return c }
func topicDeleteParameter(topic string, keys []string) { for _, key := range keys { t := topicParameterJSON{Topic: topic, Key: key, Recursive: recursive} jsonStr, err := json.Marshal(t) internal.Check(err) internal.PutWant("/topic/remove/parameter", jsonStr) } }
func topicAddParameter(topic string, parameters []string) { for _, param := range parameters { parameterSplitted := strings.Split(param, ":") if len(parameterSplitted) != 2 { continue } _, err := internal.Client().TopicAddParameter(topic, parameterSplitted[0], parameterSplitted[1], recursive) internal.Check(err) } }
func userVerify(username, tokenVerify string) { verifyJSON, err := internal.Client().UserVerify(username, tokenVerify) internal.Check(err) // Display return to user (contains clear password) internal.Print(verifyJSON) if save && verifyJSON.Password != "" && verifyJSON.Username != "" && verifyJSON.URL != "" { verifyJSON.Message = "" jsonStr, err := json.MarshalIndent(verifyJSON, "", " ") internal.Check(err) jsonStr = append(jsonStr, '\n') filename := internal.ConfigFile dir := path.Dir(filename) if _, err := os.Stat(dir); os.IsNotExist(err) { internal.Check(os.Mkdir(dir, 0740)) } internal.Check(ioutil.WriteFile(filename, jsonStr, 0600)) } }
func topicAddDeleteGroups(method string, topic string, groups []string, path string) { for _, groupname := range groups { t := topicGroupnameJSON{topic, groupname, recursive} jsonStr, err := json.Marshal(t) internal.Check(err) if method == "PUT" { internal.PutWant("/topic"+path, jsonStr) } else { internal.DeleteWant("/topic"+path, jsonStr) } } }
func groupAddDeleteUsers(method string, groupname string, users []string, path string) { for _, username := range users { t := groupUsernameJSON{groupname, username} jsonStr, err := json.Marshal(t) internal.Check(err) if method == "PUT" { internal.PutWant("/group"+path, jsonStr) } else { internal.DeleteWant("/group"+path, jsonStr) } } }
func userReset(username, email string) { ssl := "" if internal.SSLInsecureSkipVerify { ssl = "--sslInsecureSkipVerify=true" } m := userJSON{ Username: username, Email: email, Callback: fmt.Sprintf("tatcli %s --url=:scheme://:host::port:path user verify --save :username :token", ssl), } jsonStr, err := json.Marshal(m) internal.Check(err) fmt.Printf(internal.PostWant("/user/reset", jsonStr)) }
func userVerify(username, tokenVerify string) { url := fmt.Sprintf("/user/verify/%s/%s", username, tokenVerify) r := internal.GetWantReturn(url) var verifyJSON verifyJSON err := json.Unmarshal([]byte(r), &verifyJSON) internal.Check(err) // Display return to user (contains clear password) fmt.Printf(r) if save && verifyJSON.Password != "" && verifyJSON.Username != "" && verifyJSON.URL != "" { verifyJSON.Message = "" jsonStr, err := json.MarshalIndent(verifyJSON, "", " ") internal.Check(err) jsonStr = append(jsonStr, '\n') filename := internal.ConfigFile dir := path.Dir(filename) if _, err := os.Stat(dir); os.IsNotExist(err) { internal.Check(os.Mkdir(dir, 0740)) } internal.Check(ioutil.WriteFile(filename, jsonStr, 0600)) } }
func messageAction(action, topic, idReference, message, option string) { m := messageActionJSON{message, idReference, action, option} jsonStr, err := json.Marshal(m) internal.Check(err) if action == "remove" { internal.DeleteWant("/message/"+idReference, nil) } else if action == "like" || action == "unlike" || action == "label" || action == "unlabel" || action == "task" || action == "untask" || action == "tag" || action == "untag" || action == "update" { internal.PutWant("/message"+topic, jsonStr) } else { fmt.Print(internal.PostWant(fmt.Sprintf("/message%s", topic), jsonStr)) } }
func topicParam(topic string, maxLength int, canForceDate, canUpdateMsg, canDeleteMsg, canUpdateAllMsg, canDeleteAllMsg, isROPublic bool) { t := paramJSON{ Topic: topic, MaxLength: maxLength, CanForceDate: canForceDate, CanUpdateMsg: canUpdateMsg, CanDeleteMsg: canDeleteMsg, CanUpdateAllMsg: canUpdateAllMsg, CanDeleteAllMsg: canDeleteAllMsg, IsROPublic: isROPublic, Recursive: recursive, } jsonStr, err := json.Marshal(t) internal.Check(err) internal.PutWant("/topic/param", jsonStr) }
func socketInteractive() { c := newClient() done := make(chan bool) go socketRead(c) r := bufio.NewReader(os.Stdin) go func() { for { line, err := r.ReadString('\n') if err != nil && err.Error() != "unexpected newline" { internal.Check(err) } line = analyzeLine(c, line) if line != "" { if err = c.WriteMessage(1, []byte(line)); err != nil { fmt.Fprintf(os.Stderr, "%s\n", err.Error()) } } } }() <-done }
func setAdminUserAction(username string) { m := usernameUserJSON{username} jsonStr, err := json.Marshal(m) internal.Check(err) internal.PutWant("/user/setadmin", jsonStr) }
func groupCreate(group, description string) { m := groupJSON{group, description} jsonStr, err := json.Marshal(m) internal.Check(err) internal.PostWant("/group", jsonStr) }
func checkUserAction(username string, fixPrivateTopics, fixDefaultGroup bool) { m := checkUserJSON{username, fixPrivateTopics, fixDefaultGroup} jsonStr, err := json.Marshal(m) internal.Check(err) fmt.Println(internal.ReqWant("PUT", http.StatusCreated, "/user/check", jsonStr)) }
func renameUserAction(username, newUsername string) { m := renameUserJSON{username, newUsername} jsonStr, err := json.Marshal(m) internal.Check(err) internal.PutWant("/user/rename", jsonStr) }
func userAdd(username, email, fullname string) { m := userJSON{username, fullname, email, "tatcli --url=:scheme://:host::port:path user verify --save :username :token"} jsonStr, err := json.Marshal(m) internal.Check(err) fmt.Printf(internal.PostWant("/user", jsonStr)) }
func topicCreate(topic, description string) { m := topicJSON{topic, description} jsonStr, err := json.Marshal(m) internal.Check(err) internal.PostWant("/topic", jsonStr) }
func resetSystemUserAction(username string) { m := resetSystemUserJSON{username} jsonStr, err := json.Marshal(m) internal.Check(err) fmt.Print(internal.ReqWant("PUT", http.StatusCreated, "/user/resetsystem", jsonStr)) }
func convertUserAction(username, canWriteNotifications string) { m := convertUserJSON{username, canWriteNotifications} jsonStr, err := json.Marshal(m) internal.Check(err) fmt.Print(internal.ReqWant("PUT", http.StatusCreated, "/user/convert", jsonStr)) }
Use: "parameter", Short: "Update param on one topic: tatcli topic param [--recursive] <topic> <maxLength> <canForceDate> <canUpdateMsg> <canDeleteMsg> <canUpdateAllMsg> <canDeleteAllMsg> <adminCanUpdateAllMsg> <adminCanDeleteAllMsg> <isAutoComputeTags> <isAutoComputeLabels>", Aliases: []string{"param"}, Run: func(cmd *cobra.Command, args []string) { if len(args) != 11 { internal.Exit("Invalid parameter to tatcli topic param. See tatcli topic param --help\n") } var err error p := tat.TopicParameters{ Topic: args[0], } p.MaxLength, err = strconv.Atoi(args[1]) internal.Check(err) p.CanForceDate, err = strconv.ParseBool(args[2]) internal.Check(err) p.CanUpdateMsg, err = strconv.ParseBool(args[3]) internal.Check(err) p.CanDeleteMsg, err = strconv.ParseBool(args[4]) internal.Check(err) p.CanUpdateAllMsg, err = strconv.ParseBool(args[5]) internal.Check(err) p.CanDeleteAllMsg, err = strconv.ParseBool(args[6]) internal.Check(err) p.AdminCanUpdateAllMsg, err = strconv.ParseBool(args[7]) internal.Check(err) p.AdminCanDeleteAllMsg, err = strconv.ParseBool(args[8]) internal.Check(err) p.IsAutoComputeTags, err = strconv.ParseBool(args[9])
func archiveUserAction(username string) { m := usernameUserJSON{username} jsonStr, err := json.Marshal(m) internal.Check(err) internal.PutWant("/user/archive", jsonStr) }