From ec276e54113b360b6e22cc6cb78df038ecb157a1 Mon Sep 17 00:00:00 2001 From: finn Date: Fri, 25 Mar 2022 22:26:47 -0700 Subject: [PATCH] add group preview subcommand --- cmd/signaldctl/cmd/group/preview/preview.go | 78 +++++++++++++++++++++ cmd/signaldctl/cmd/group/root.go | 2 + 2 files changed, 80 insertions(+) create mode 100644 cmd/signaldctl/cmd/group/preview/preview.go diff --git a/cmd/signaldctl/cmd/group/preview/preview.go b/cmd/signaldctl/cmd/group/preview/preview.go new file mode 100644 index 0000000..43cf888 --- /dev/null +++ b/cmd/signaldctl/cmd/group/preview/preview.go @@ -0,0 +1,78 @@ +// Copyright © 2021 Finn Herzfeld +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +package preview + +import ( + "encoding/json" + "errors" + "log" + "os" + "strings" + + "github.com/spf13/cobra" + + "gitlab.com/signald/signald-go/cmd/signaldctl/common" + "gitlab.com/signald/signald-go/cmd/signaldctl/config" + "gitlab.com/signald/signald-go/signald/client-protocol/v1" +) + +var ( + account string + joinURL string + PreviewGroupCmd = &cobra.Command{ + Use: "preview https://signal.group/...", + Short: "preview information about a group without joining", + PreRunE: func(cmd *cobra.Command, args []string) error { + if account == "" { + account = config.Config.DefaultAccount + } + if account == "" { + common.Must(cmd.Help()) + log.Fatal("No account specified. Please specify with --account or set a default") + } + + if len(args) == 0 { + return errors.New("please specify signald.group URL") + } + joinURL = args[0] + + if !strings.HasPrefix(joinURL, "https://signal.group/#") { + return errors.New("invalid group link (must start with https://signal.group/#") + } + + return nil + }, + RunE: func(_ *cobra.Command, args []string) error { + go common.Signald.Listen(nil) + req := v1.GroupLinkInfoRequest{ + Account: account, + Uri: joinURL, + } + + info, err := req.Submit(common.Signald) + if err != nil { + return err + } + + switch common.OutputFormat { + case common.OutputFormatJSON, common.OutputFormatDefault: + return json.NewEncoder(os.Stdout).Encode(info) + } + + return nil + }, + } +) diff --git a/cmd/signaldctl/cmd/group/root.go b/cmd/signaldctl/cmd/group/root.go index 3f7d0ea..8be78ea 100644 --- a/cmd/signaldctl/cmd/group/root.go +++ b/cmd/signaldctl/cmd/group/root.go @@ -24,6 +24,7 @@ import ( "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/join" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/leave" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/list" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/preview" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/removemember" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/show" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group/update" @@ -41,6 +42,7 @@ func init() { GroupCmd.AddCommand(join.JoinGroupCmd) GroupCmd.AddCommand(leave.LeaveGroupCmd) GroupCmd.AddCommand(list.ListGroupCmd) + GroupCmd.AddCommand(preview.PreviewGroupCmd) GroupCmd.AddCommand(removemember.RemoveMemberCmd) GroupCmd.AddCommand(show.ShowGroupCmd) GroupCmd.AddCommand(update.UpdateGroupCmd)