mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-30 18:51:03 +00:00
7ffefb042e
Co-authored-by: James Mills <prologic@shortcircuit.net.au> Reviewed-on: https://git.mills.io/prologic/saltyim/pulls/46
65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
|
|
"go.mills.io/saltyim"
|
|
)
|
|
|
|
var registerCmd = &cobra.Command{
|
|
Use: "register",
|
|
Aliases: []string{"auth", "reg"},
|
|
Short: "Registers a new account with a broker",
|
|
Long: `This command registers a new account with a broker.
|
|
|
|
TBD`,
|
|
Args: cobra.ExactArgs(0),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
fmt.Fprintln(os.Stderr, "✋ This is being re-designed. Stay tuned! 🤗")
|
|
os.Exit(1)
|
|
|
|
user := viper.GetString("user")
|
|
identity := viper.GetString("identity")
|
|
|
|
var profiles []profile
|
|
viper.UnmarshalKey("profiles", &profiles)
|
|
for _, p := range profiles {
|
|
if user == p.User {
|
|
identity = p.Identity
|
|
}
|
|
}
|
|
|
|
me := &saltyim.Addr{}
|
|
if sp := strings.Split(user, "@"); len(sp) > 1 {
|
|
me.User = sp[0]
|
|
me.Domain = sp[1]
|
|
}
|
|
// XXX: What if me.IsZero()
|
|
|
|
register(me, identity)
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(registerCmd)
|
|
}
|
|
|
|
func register(me *saltyim.Addr, identity string) {
|
|
cli, err := saltyim.NewClient(me, identity)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "error initializing client: %s\n", err)
|
|
os.Exit(2)
|
|
}
|
|
|
|
if err := cli.Register(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "error registering: %s\n", err)
|
|
os.Exit(2)
|
|
}
|
|
fmt.Println("Success!")
|
|
}
|