saltyim is the Go library and reference client and broker implementation for Salty IM it contains a command-line client (cli), a terminal user interface (tui), builtin server/broker and a Mobile / Desktop App PWA (progressive web app) https://salty.im/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
saltyim/options.go

64 lines
1.3 KiB

package saltyim
import (
"bytes"
"fmt"
"os"
log "github.com/sirupsen/logrus"
)
// WithClientIdentity sets the client's identity
func WithClientIdentity(options ...IdentityOption) ClientOption {
return func(cli *Client) error {
id, err := GetIdentity(options...)
if err != nil {
return fmt.Errorf("error loading identity: %w", err)
}
cli.id = id
cli.key = id.key
return nil
}
}
// WithStateFromFile sets the client's state from a file on disk
func WithStateFromFile(fn string) ClientOption {
return func(cli *Client) error {
f, err := os.Open(fn)
if err != nil {
log.WithError(err).Warnf("error opening state file %s, creating an empty state", fn)
cli.state = NewState()
return nil
}
defer f.Close()
s, err := LoadState(f)
if err != nil {
return fmt.Errorf("error loading state: %w", err)
}
cli.state = s
return nil
}
}
// WithStateFromBytes sets the client's state from a byte array
func WithStateFromBytes(data []byte) ClientOption {
return func(cli *Client) error {
s, err := LoadState(bytes.NewBuffer(data))
if err != nil {
return fmt.Errorf("error loading state: %w", err)
}
cli.state = s
return nil
}
}
// WithState sets the client's state from a state object
func WithState(state *State) ClientOption {
return func(cli *Client) error {
cli.state = state
return nil
}
}