Add Avatar service and cli for updating avatar on a broker (#116)
This PR also: - Tidies up the default options and config - Tidies up the service user code Co-authored-by: James Mills <prologic@shortcircuit.net.au> Reviewed-on: #116pull/121/head
parent
e2f2efda13
commit
3fccb3ae5f
@ -0,0 +1,67 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
"os" |
||||
"strings" |
||||
|
||||
"github.com/spf13/cobra" |
||||
"github.com/spf13/viper" |
||||
"go.mills.io/saltyim" |
||||
) |
||||
|
||||
var setavatarCmd = &cobra.Command{ |
||||
Use: "setavatar <file>", |
||||
Short: "Creates or updates your avatar with a Salty Broker", |
||||
Long: `This command creates or updates your avatar with a regisetered account |
||||
on a Salty Broker (an instance of saltyd). |
||||
|
||||
NOTE: This is only spported on a Salty Broker.`, |
||||
Args: cobra.ExactArgs(1), |
||||
Run: func(cmd *cobra.Command, args []string) { |
||||
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()
|
||||
|
||||
setavatar(me, identity, args[0]) |
||||
}, |
||||
} |
||||
|
||||
func init() { |
||||
rootCmd.AddCommand(setavatarCmd) |
||||
} |
||||
|
||||
func setavatar(me *saltyim.Addr, identity, fn string) { |
||||
cli, err := saltyim.NewClient(me, saltyim.WithIdentityPath(identity)) |
||||
if err != nil { |
||||
fmt.Fprintf(os.Stderr, "error initializing client: %s\n", err) |
||||
os.Exit(2) |
||||
} |
||||
|
||||
data, err := os.ReadFile(fn) |
||||
if err != nil { |
||||
fmt.Fprintf(os.Stderr, "error reading avatar file %s: %s", fn, err) |
||||
os.Exit(2) |
||||
} |
||||
|
||||
if err := cli.SetAvatar(data); err != nil { |
||||
fmt.Fprintf(os.Stderr, "error updating avatar: %s\n", err) |
||||
os.Exit(2) |
||||
} |
||||
|
||||
fmt.Println("Success! 🥳") |
||||
} |
@ -0,0 +1,14 @@ |
||||
$TTL 60 |
||||
$ORIGIN home.arpa. |
||||
@ IN SOA home.arpa. admin.home.arpa. ( |
||||
2022040100 ; Serial |
||||
4H ; Refresh |
||||
1H ; Retry |
||||
7D ; Expire |
||||
4H ) ; Negative Cache TTL |
||||
|
||||
|
||||
@ IN A 127.0.0.1 |
||||
salty.home.arpa. IN A 127.0.0.1 |
||||
_salty._tcp.home.arpa. IN SRV 0 0 443 salty.home.arpa. |
||||
_avatars._tcp.home.arpa. IN SRV 0 0 443 salty.home.arpa. |
Binary file not shown.
Loading…
Reference in new issue