Add device list and remove

This commit is contained in:
Finn 2021-02-02 02:27:54 -08:00
parent d87a24bf2c
commit 4d82a2093d
5 changed files with 182 additions and 3 deletions

View file

@ -0,0 +1,84 @@
// Copyright © 2021 Finn Herzfeld <finn@janky.solutions>
//
// 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 <http://www.gnu.org/licenses/>.
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")
}
},
}
)

View file

@ -0,0 +1,61 @@
// Copyright © 2021 Finn Herzfeld <finn@janky.solutions>
//
// 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 <http://www.gnu.org/licenses/>.
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"))
}

View file

@ -0,0 +1,33 @@
// Copyright © 2021 Finn Herzfeld <finn@janky.solutions>
//
// 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 <http://www.gnu.org/licenses/>.
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)
}

View file

@ -28,7 +28,6 @@ import (
var (
account string
to string
ReadMessageCmd = &cobra.Command{
Use: "mark-read",

View file

@ -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.