Skip to content

Add dashboard list command #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add dashboard list command
  • Loading branch information
polldo committed Oct 5, 2021
commit 862e1809a7b3f7ec394b4bb31b593bb1e3ee760b
2 changes: 2 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cloud-cli/cli/config"
"github.com/arduino/arduino-cloud-cli/cli/dashboard"
"github.com/arduino/arduino-cloud-cli/cli/device"
"github.com/arduino/arduino-cloud-cli/cli/ota"
"github.com/arduino/arduino-cloud-cli/cli/thing"
Expand All @@ -51,6 +52,7 @@ func Execute() {
cli.AddCommand(config.NewCommand())
cli.AddCommand(device.NewCommand())
cli.AddCommand(thing.NewCommand())
cli.AddCommand(dashboard.NewCommand())
cli.AddCommand(ota.NewCommand())

cli.PersistentFlags().BoolVarP(&cliFlags.verbose, "verbose", "v", false, "Print the logs on the standard output.")
Expand Down
34 changes: 34 additions & 0 deletions cli/dashboard/dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package dashboard

import (
"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
dashboardCommand := &cobra.Command{
Use: "dashboard",
Short: "Dashboard commands.",
Long: "Dashboard commands.",
}

dashboardCommand.AddCommand(initListCommand())

return dashboardCommand
}
90 changes: 90 additions & 0 deletions cli/dashboard/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package dashboard

import (
"os"
"strings"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/table"
"github.com/arduino/arduino-cloud-cli/command/dashboard"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var listFlags struct {
showSharing bool
}

func initListCommand() *cobra.Command {
listCommand := &cobra.Command{
Use: "list",
Short: "List dashboards",
Long: "List dashboards on Arduino IoT Cloud",
Run: runListCommand,
}

listCommand.Flags().BoolVarP(&listFlags.showSharing, "show-sharing", "s", false, "Show dashboard sharing information")
return listCommand
}

func runListCommand(cmd *cobra.Command, args []string) {
logrus.Info("Listing dashboards")

dash, err := dashboard.List()
if err != nil {
feedback.Errorf("Error during dashboard list: %v", err)
os.Exit(errorcodes.ErrGeneric)
}

feedback.PrintResult(listResult{dash})
}

type listResult struct {
dashboards []dashboard.DashboardInfo
}

func (r listResult) Data() interface{} {
return r.dashboards
}

func (r listResult) String() string {
if len(r.dashboards) == 0 {
return "No dashboard found."
}
t := table.New()

head := []interface{}{"Name", "ID", "Widgets", "UpdatedAt"}
if listFlags.showSharing {
head = append(head, "SharedBy", "SharedWith")
}
t.SetHeader(head...)

for _, dash := range r.dashboards {
row := []interface{}{dash.Name, dash.ID}
row = append(row, strings.Join(dash.Widgets, ", "))
if listFlags.showSharing {
row = append(row, dash.SharedBy)
row = append(row, strings.Join(dash.SharedWith, ", "))
}
t.AddRow(row...)
}
return t.Render()
}
51 changes: 51 additions & 0 deletions command/dashboard/dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package dashboard

import iotclient "github.com/arduino/iot-client-go"

// DashboardInfo contains the most interesting
// information, in string format, of an Arduino IoT Cloud dashboard.
type DashboardInfo struct {
Name string `json:"name"`
ID string `json:"id"`
SharedBy string `json:"shared_by"`
SharedWith []string `json:"shared_with"`
UpdatedAt string `json:"updated_at"`
Widgets []string `json:"widgets"`
}

func getDashboardInfo(dashboard *iotclient.ArduinoDashboardv2) *DashboardInfo {
var shares []string
for _, s := range dashboard.SharedWith {
shares = append(shares, s.Username)
}
var widgets []string
for _, w := range dashboard.Widgets {
widgets = append(widgets, w.Name)
}
info := &DashboardInfo{
Name: dashboard.Name,
ID: dashboard.Id,
SharedBy: dashboard.SharedBy.Username,
SharedWith: shares,
UpdatedAt: dashboard.UpdatedAt.String(),
Widgets: widgets,
}
return info
}
49 changes: 49 additions & 0 deletions command/dashboard/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package dashboard

import (
"github.com/arduino/arduino-cloud-cli/internal/config"
"github.com/arduino/arduino-cloud-cli/internal/iot"
)

// List command is used to list
// the dashboards of Arduino IoT Cloud.
func List() ([]DashboardInfo, error) {
conf, err := config.Retrieve()
if err != nil {
return nil, err
}
iotClient, err := iot.NewClient(conf.Client, conf.Secret)
if err != nil {
return nil, err
}

foundDashboards, err := iotClient.DashboardList()
if err != nil {
return nil, err
}

var dashboards []DashboardInfo
for _, found := range foundDashboards {
info := getDashboardInfo(&found)
dashboards = append(dashboards, *info)
}

return dashboards, nil
}
11 changes: 11 additions & 0 deletions internal/iot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Client interface {
ThingDelete(id string) error
ThingShow(id string) (*iotclient.ArduinoThing, error)
ThingList(ids []string, device *string, props bool) ([]iotclient.ArduinoThing, error)
DashboardList() ([]iotclient.ArduinoDashboardv2, error)
}

type client struct {
Expand Down Expand Up @@ -202,6 +203,16 @@ func (cl *client) ThingList(ids []string, device *string, props bool) ([]iotclie
return things, nil
}

// DashboardList returns a list of dashboards on Arduino IoT Cloud.
func (cl *client) DashboardList() ([]iotclient.ArduinoDashboardv2, error) {
dashboards, _, err := cl.api.DashboardsV2Api.DashboardsV2List(cl.ctx, nil)
if err != nil {
err = fmt.Errorf("listing dashboards: %w", errorDetail(err))
return nil, err
}
return dashboards, nil
}

func (cl *client) setup(client, secret string) error {
// Get the access token in exchange of client_id and client_secret
tok, err := token(client, secret)
Expand Down