Exemplo n.º 1
0
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")
	}
}
Exemplo n.º 2
0
Arquivo: write.go Projeto: ovh/tatcli
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)
}
Exemplo n.º 3
0
func socketRead(c *websocket.Conn) {
	for {
		_, r, err := c.ReadMessage()
		internal.Check(err)
		fmt.Print(color(string(r)))
	}
}
Exemplo n.º 4
0
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
}
Exemplo n.º 5
0
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)
	}
}
Exemplo n.º 6
0
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)
	}
}
Exemplo n.º 7
0
Arquivo: verify.go Projeto: ovh/tatcli
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))
	}
}
Exemplo n.º 8
0
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)
		}
	}
}
Exemplo n.º 9
0
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)
		}
	}
}
Exemplo n.º 10
0
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))
}
Exemplo n.º 11
0
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))
	}
}
Exemplo n.º 12
0
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))
	}
}
Exemplo n.º 13
0
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)
}
Exemplo n.º 14
0
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
}
Exemplo n.º 15
0
func setAdminUserAction(username string) {
	m := usernameUserJSON{username}
	jsonStr, err := json.Marshal(m)
	internal.Check(err)
	internal.PutWant("/user/setadmin", jsonStr)
}
Exemplo n.º 16
0
func groupCreate(group, description string) {
	m := groupJSON{group, description}
	jsonStr, err := json.Marshal(m)
	internal.Check(err)
	internal.PostWant("/group", jsonStr)
}
Exemplo n.º 17
0
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))
}
Exemplo n.º 18
0
func renameUserAction(username, newUsername string) {
	m := renameUserJSON{username, newUsername}
	jsonStr, err := json.Marshal(m)
	internal.Check(err)
	internal.PutWant("/user/rename", jsonStr)
}
Exemplo n.º 19
0
Arquivo: add.go Projeto: bnjjj/tatcli
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))
}
Exemplo n.º 20
0
func topicCreate(topic, description string) {
	m := topicJSON{topic, description}
	jsonStr, err := json.Marshal(m)
	internal.Check(err)
	internal.PostWant("/topic", jsonStr)
}
Exemplo n.º 21
0
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))
}
Exemplo n.º 22
0
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))
}
Exemplo n.º 23
0
	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])
Exemplo n.º 24
0
func archiveUserAction(username string) {
	m := usernameUserJSON{username}
	jsonStr, err := json.Marshal(m)
	internal.Check(err)
	internal.PutWant("/user/archive", jsonStr)
}