mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-16 03:48:24 +00:00
fcc4f53f20
Co-authored-by: mlctrez <mlctrez@gmail.com> Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/157 Reviewed-by: James Mills <james@mills.io> Co-authored-by: mlctrez <mlctrez@noreply@mills.io> Co-committed-by: mlctrez <mlctrez@noreply@mills.io>
65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
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
|
|
}
|
|
}
|