Add /config command

This commit is contained in:
bodqhrohro 2019-12-05 21:56:12 +02:00
parent 753a488c9d
commit ec1197f83c
2 changed files with 59 additions and 2 deletions

View file

@ -24,6 +24,7 @@ type SessionsMap struct {
// Session is a key-values subtree // Session is a key-values subtree
type Session struct { type Session struct {
Login string `yaml:":login"` Login string `yaml:":login"`
Timezone string `yaml:":timezone"`
} }
var sessionDB *SessionsYamlDB var sessionDB *SessionsYamlDB
@ -75,3 +76,35 @@ func initYamlDB(path string, dataPtr *SessionsMap) (*SessionsYamlDB, error) {
Data: dataPtr, Data: dataPtr,
}, nil }, nil
} }
// Get retrieves a session value
func (s *Session) Get(key string) (string, error) {
switch key {
case "timezone":
return s.Timezone, nil
}
return "", errors.New("Unknown session property")
}
// ToMap converts the session to a map
func (s *Session) ToMap() map[string]string {
m := make(map[string]string)
for _, configKey := range []string{"timezone"} {
value, _ := s.Get(configKey)
m[configKey] = value
}
return m
}
// Set sets a session value
func (s *Session) Set(key string, value string) (string, error) {
switch key {
case "timezone":
s.Timezone = value
return value, nil
}
return "", errors.New("Unknown session property")
}

View file

@ -1,6 +1,7 @@
package telegram package telegram
import ( import (
"fmt"
"github.com/pkg/errors" "github.com/pkg/errors"
"strconv" "strconv"
"strings" "strings"
@ -22,7 +23,7 @@ var transportCommands = map[string]command{
"setname": command{"first last", "update name"}, "setname": command{"first last", "update name"},
"setbio": command{"", "update about"}, "setbio": command{"", "update about"},
"setpassword": command{"[old] [new]", "set or remove password"}, "setpassword": command{"[old] [new]", "set or remove password"},
//"config": command{"[param] [value]", "view or update configuration options"}, "config": command{"[param] [value]", "view or update configuration options"},
} }
var chatCommands = map[string]command{ var chatCommands = map[string]command{
@ -209,6 +210,29 @@ func (c *Client) ProcessTransportCommand(cmdline string) string {
if err != nil { if err != nil {
return errors.Wrap(err, "Couldn't set password").Error() return errors.Wrap(err, "Couldn't set password").Error()
} }
case "config":
if len(args) > 1 {
value, err := c.Session.Set(args[0], args[1])
if err != nil {
return err.Error()
}
return fmt.Sprintf("%s set to %s", args[0], value)
} else if len(args) > 0 {
value, err := c.Session.Get(args[0])
if err != nil {
return err.Error()
}
return fmt.Sprintf("%s is set to %s", args[0], value)
}
var entries []string
for key, value := range c.Session.ToMap() {
entries = append(entries, fmt.Sprintf("%s is set to %s", key, value))
}
return strings.Join(entries, "\n")
case "help": case "help":
return helpString(helpTypeTransport) return helpString(helpTypeTransport)
} }