diff --git a/cmd/signaldctl/cmd/device/list/list.go b/cmd/signaldctl/cmd/device/list/list.go new file mode 100644 index 0000000..b12b617 --- /dev/null +++ b/cmd/signaldctl/cmd/device/list/list.go @@ -0,0 +1,84 @@ +// 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 list + +import ( + "encoding/json" + "log" + "os" + + "github.com/jedib0t/go-pretty/v6/table" + "github.com/spf13/cobra" + "gopkg.in/yaml.v2" + + "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 + + ListDeviceCmd = &cobra.Command{ + Use: "list", + Short: "list all devices linked to the account", + PreRun: func(_ *cobra.Command, args []string) { + if account == "" { + account = config.Config.DefaultAccount + } + if account == "" { + log.Fatal("No account specified. Please specify with --account or set a default") + } + }, + Run: func(_ *cobra.Command, args []string) { + go common.Signald.Listen(nil) + + req := v1.GetLinkedDevicesRequest{Account: account} + resp, err := req.Submit(common.Signald) + if err != nil { + log.Fatal("error sending request to signald: ", err) + } + switch common.OutputFormat { + case common.OutputFormatJSON: + err := json.NewEncoder(os.Stdout).Encode(resp.Devices) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatYAML: + err := yaml.NewEncoder(os.Stdout).Encode(resp.Devices) + if err != nil { + log.Fatal(err, "error encoding response to stdout") + } + case common.OutputFormatCSV, common.OutputFormatTable, common.OutputFormatDefault: + t := table.NewWriter() + t.SetOutputMirror(os.Stdout) + t.AppendHeader(table.Row{"ID", "Name", "LastSeen", "Registered"}) + for _, device := range resp.Devices { + t.AppendRow(table.Row{device.ID, device.Name, device.LastSeen, device.Created}) + } + + if common.OutputFormat == common.OutputFormatCSV { + t.RenderCSV() + } else { + t.SetStyle(table.StyleLight) + t.Render() + } + default: + log.Fatal("Unsupported output format") + } + }, + } +) diff --git a/cmd/signaldctl/cmd/device/remove/remove.go b/cmd/signaldctl/cmd/device/remove/remove.go new file mode 100644 index 0000000..4b158ad --- /dev/null +++ b/cmd/signaldctl/cmd/device/remove/remove.go @@ -0,0 +1,61 @@ +// 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 remove + +import ( + "log" + + "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 + device int64 + + RemoveDeviceCmd = &cobra.Command{ + Use: "remove", + Short: "remove a linked device", + PreRun: func(_ *cobra.Command, args []string) { + if account == "" { + account = config.Config.DefaultAccount + } + if account == "" { + log.Fatal("No account specified. Please specify with --account or set a default") + } + }, + Run: func(_ *cobra.Command, args []string) { + go common.Signald.Listen(nil) + req := v1.RemoveLinkedDeviceRequest{ + Account: account, + DeviceId: device, + } + err := req.Submit(common.Signald) + if err != nil { + log.Fatal("error sending request to signald: ", err) + } + }, + } +) + +func init() { + RemoveDeviceCmd.Flags().StringVarP(&account, "account", "a", "", "local account to use") + RemoveDeviceCmd.Flags().Int64VarP(&device, "device", "d", 0, "device ID to remove") + common.Must(RemoveDeviceCmd.MarkFlagRequired("device")) +} diff --git a/cmd/signaldctl/cmd/device/root.go b/cmd/signaldctl/cmd/device/root.go new file mode 100644 index 0000000..11abfe6 --- /dev/null +++ b/cmd/signaldctl/cmd/device/root.go @@ -0,0 +1,33 @@ +// 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 device + +import ( + "github.com/spf13/cobra" + + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/device/list" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/device/remove" +) + +var DeviceCmd = &cobra.Command{ + Use: "device", + Aliases: []string{"msg", "devices"}, +} + +func init() { + DeviceCmd.AddCommand(list.ListDeviceCmd) + DeviceCmd.AddCommand(remove.RemoveDeviceCmd) +} diff --git a/cmd/signaldctl/cmd/message/read/mark-message-as-read.go b/cmd/signaldctl/cmd/message/read/mark-message-as-read.go index 53d0204..fe1c36d 100644 --- a/cmd/signaldctl/cmd/message/read/mark-message-as-read.go +++ b/cmd/signaldctl/cmd/message/read/mark-message-as-read.go @@ -28,7 +28,6 @@ import ( var ( account string - to string ReadMessageCmd = &cobra.Command{ Use: "mark-read", diff --git a/cmd/signaldctl/cmd/root.go b/cmd/signaldctl/cmd/root.go index d55ba32..81b45b6 100644 --- a/cmd/signaldctl/cmd/root.go +++ b/cmd/signaldctl/cmd/root.go @@ -24,6 +24,7 @@ import ( "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/account" configcmd "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/config" + "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/device" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/group" "gitlab.com/signald/signald-go/cmd/signaldctl/cmd/message" "gitlab.com/signald/signald-go/cmd/signaldctl/common" @@ -63,10 +64,11 @@ func init() { RootCmd.PersistentFlags().StringVar(&config.Path, "config", fmt.Sprintf("%s/.config/signaldctl.yaml", os.Getenv("HOME")), "config file (default is ~/.config/signaldctl.yaml)") RootCmd.PersistentFlags().StringVar(&socketPath, "socket", "/var/run/signald/signald.sock", "the path to the signald socket file") RootCmd.PersistentFlags().StringVarP(&common.OutputFormat, "output-format", "o", "default", "the output format. options are usually table, yaml and json, default is usually table. Some commands have other options.") - RootCmd.AddCommand(group.GroupCmd) RootCmd.AddCommand(account.AccountCmd) - RootCmd.AddCommand(message.MessageCmd) RootCmd.AddCommand(configcmd.ConfigCmd) + RootCmd.AddCommand(device.DeviceCmd) + RootCmd.AddCommand(group.GroupCmd) + RootCmd.AddCommand(message.MessageCmd) } // initConfig reads in config file and ENV variables if set.