Skip to content

Improve output format #32

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 17 commits into from
Sep 13, 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
Improve thing output format
  • Loading branch information
polldo committed Sep 8, 2021
commit 69d71b0378124f6ffd74dfbd9e875bb53c87dffa
20 changes: 18 additions & 2 deletions cli/thing/clone.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package thing

import (
"fmt"

"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/iot-cloud-cli/command/thing"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -33,11 +36,24 @@ func runCloneCommand(cmd *cobra.Command, args []string) error {
CloneID: cloneFlags.cloneID,
}

thingID, err := thing.Clone(params)
thing, err := thing.Clone(params)
if err != nil {
return err
}

logrus.Infof("IoT Cloud thing created with ID: %s\n", thingID)
logrus.Infof("IoT Cloud thing created with ID: %s", thing.ID)
feedback.PrintResult(cloneResult{thing})
return nil
}

type cloneResult struct {
thing *thing.ThingInfo
}

func (r cloneResult) Data() interface{} {
return r.thing
}

func (r cloneResult) String() string {
return fmt.Sprintf("IoT Cloud thing created with ID: %s", r.thing.ID)
}
20 changes: 18 additions & 2 deletions cli/thing/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package thing

import (
"fmt"

"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/iot-cloud-cli/command/thing"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -40,11 +43,24 @@ func runCreateCommand(cmd *cobra.Command, args []string) error {
params.Name = &createFlags.name
}

thingID, err := thing.Create(params)
thing, err := thing.Create(params)
if err != nil {
return err
}

logrus.Infof("IoT Cloud thing created with ID: %s\n", thingID)
logrus.Infof("IoT Cloud thing created with ID: %s\n", thing.ID)
feedback.PrintResult(createResult{thing})
return nil
}

type createResult struct {
thing *thing.ThingInfo
}

func (r createResult) Data() interface{} {
return r.thing
}

func (r createResult) String() string {
return fmt.Sprintf("IoT Cloud thing created with ID: %s", r.thing.ID)
}
18 changes: 12 additions & 6 deletions command/thing/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,35 @@ type CloneParams struct {
}

// Clone allows to create a new thing from an already existing one
func Clone(params *CloneParams) (string, error) {
func Clone(params *CloneParams) (*ThingInfo, error) {
conf, err := config.Retrieve()
if err != nil {
return "", err
return nil, err
}
iotClient, err := iot.NewClient(conf.Client, conf.Secret)
if err != nil {
return "", err
return nil, err
}

thing, err := retrieve(iotClient, params.CloneID)
if err != nil {
return "", err
return nil, err
}

thing.Name = params.Name
force := true
thingID, err := iotClient.ThingCreate(thing, force)
if err != nil {
return "", err
return nil, err
}

return thingID, nil
thingInfo := &ThingInfo{
Name: thing.Name,
ID: thingID,
DeviceID: thing.DeviceId,
Variables: nil,
}
return thingInfo, nil
}

func retrieve(client iot.Client, thingID string) (*iotclient.Thing, error) {
Expand Down
20 changes: 13 additions & 7 deletions command/thing/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ type CreateParams struct {
}

// Create allows to create a new thing
func Create(params *CreateParams) (string, error) {
func Create(params *CreateParams) (*ThingInfo, error) {
conf, err := config.Retrieve()
if err != nil {
return "", err
return nil, err
}
iotClient, err := iot.NewClient(conf.Client, conf.Secret)
if err != nil {
return "", err
return nil, err
}

thing, err := loadTemplate(params.Template)
if err != nil {
return "", err
return nil, err
}

// Name passed as parameter has priority over name from template
Expand All @@ -43,16 +43,22 @@ func Create(params *CreateParams) (string, error) {
}
// If name is not specified in the template, it should be passed as parameter
if thing.Name == "" {
return "", errors.New("thing name not specified")
return nil, errors.New("thing name not specified")
}

force := true
thingID, err := iotClient.ThingCreate(thing, force)
if err != nil {
return "", err
return nil, err
}

return thingID, nil
thingInfo := &ThingInfo{
Name: thing.Name,
ID: thingID,
DeviceID: thing.DeviceId,
Variables: nil,
}
return thingInfo, nil
}

func loadTemplate(file string) (*iotclient.Thing, error) {
Expand Down