Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
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
9 changes: 8 additions & 1 deletion cmd/device-hub-cli/cmd_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var createCommand = &cobra.Command{
Configuration: map[string]string{},
}

err := roundTrip(sample, func(cli proto.HubClient, in iocodec.Decoder, out iocodec.Encoder) error {
err := roundTrip(sample, "create", func(cli proto.HubClient, in rawConf, out iocodec.Encoder) error {

v := proto.CreateRequest{}

Expand All @@ -49,6 +49,13 @@ var createCommand = &cobra.Command{
params, err = register.DescribeListener(v.Kind)
case "endpoint":
params, err = register.DescribeEndpoint(v.Kind)
case "process":
request := proto.StartRequest{
Endpoints: []string{},
Tags: map[string]string{},
}
tags := []string{}
return startCall(args, request, tags, cli, in, out)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really not keen on this case statement - lines 52 to 58 :

  • the other two cases are for validation whilst this specializes for the end of the function e.g. does almost the same bits of code just different.

}

if err != nil {
Expand Down
15 changes: 14 additions & 1 deletion cmd/device-hub-cli/cmd_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var deleteCommand = &cobra.Command{

sample := proto.DeleteRequest{}

err := roundTrip(sample, func(cli proto.HubClient, in iocodec.Decoder, out iocodec.Encoder) error {
err := roundTrip(sample, "delete", func(cli proto.HubClient, in rawConf, out iocodec.Encoder) error {

v := proto.DeleteRequest{}

Expand All @@ -26,6 +26,19 @@ var deleteCommand = &cobra.Command{
return err
}

if v.Type == "process" {

req := proto.StopRequest{}

err := in.Decode(&req)
if err != nil {
return err
}

return stopCall(args, req, cli, in, out)

}

resp, err := cli.Delete(context.Background(), &v)

if err != nil {
Expand Down
117 changes: 0 additions & 117 deletions cmd/device-hub-cli/cmd_pipe.go

This file was deleted.

4 changes: 1 addition & 3 deletions cmd/device-hub-cli/cmd_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ var showCommand = &cobra.Command{
Filter: strings.Join(args, ","),
}

err := roundTrip(v, func(cli proto.HubClient, in iocodec.Decoder, out iocodec.Encoder) error {

err := roundTrip(v, "show", func(cli proto.HubClient, in rawConf, out iocodec.Encoder) error {
resp, err := cli.Show(context.Background(), &v)

if err != nil {
return err
}
Expand Down
173 changes: 173 additions & 0 deletions cmd/device-hub-cli/cmd_start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// Copyright © 2017 thingful

package main

import (
"context"
"errors"
"fmt"
"strings"

"github.com/fiorix/protoc-gen-cobra/iocodec"
"github.com/spf13/cobra"
"github.com/thingful/device-hub/proto"
)

type clientConfig struct {
URI string `yaml:"uri"`
Type string `yaml:"type"`
EndpointUIDs []string `yaml:"endpoint-uids"`
ListenerUID string `yaml:"listener-uid"`
ProfileUID string `yaml:"profile-uid"`
Tags []string `yaml:"tags"`
}

func startCommand() *cobra.Command {

request := proto.StartRequest{
Endpoints: []string{},
Tags: map[string]string{},
}

tags := []string{}

startCommand := &cobra.Command{
Use: "start",
Short: "Start processing messages on a uri",
RunE: func(cmd *cobra.Command, args []string) error {
// if no profile is provided as arg, then the profile field in the yaml file
// will be loaded.
if len(args) > 0 {
request.Profile = args[0]
}
err := roundTrip(request, "start", func(cli proto.HubClient, in rawConf, out iocodec.Encoder) error {

err := startCall(args, request, tags, cli, in, out)
if err != nil {
return err
}
return nil
})
return err
},
}

startCommand.Flags().StringVarP(&request.Listener, "listener", "l", request.Listener, "listener uid to accept messages on")
startCommand.Flags().StringVarP(&request.Uri, "uri", "u", request.Uri, "uri to listen on")
startCommand.Flags().StringSliceVarP(&request.Endpoints, "endpoint", "e", request.Endpoints, "endpoint uid to push messages to, may be specified multiple times")
startCommand.Flags().StringSliceVarP(&tags, "tags", "t", tags, "colon separated (k:v) runtime tags to attach to requests, may be specified multiple times")

return startCommand
}

func startCall(args []string, request proto.StartRequest, tags []string, cli proto.HubClient, in rawConf, out iocodec.Encoder) error {

cfg := clientConfig{}

if _config.RequestFile == "" {
err := in.Decode(&cfg)
if err != nil {
return err
}
} else {
err := yamlDecoder(_config.RequestFile, &cfg)
if err != nil {
return err
}
}

// No sure about the name yet (process, pipe, etc.)
if cfg.Type != "process" {
return fmt.Errorf("file doesn't have the needed type [%v]", cfg.Type)
}

if request.Profile == "" {
request.Profile = cfg.ProfileUID
}

request.Uri = cfg.URI
request.Listener = cfg.ListenerUID
request.Endpoints = cfg.EndpointUIDs

if request.Profile == "" {
return errors.New("no profile specified")
}
// review tags
tags = cfg.Tags
for _, m := range tags {

bits := strings.Split(m, ":")

if len(bits) != 2 {
return fmt.Errorf("metadata not colon (:) separated : %s", m)
}

request.Tags[bits[0]] = bits[1]
}

resp, err := cli.Start(context.Background(), &request)

if err != nil {
return err
}

return out.Encode(resp)
}

var stopCommand = &cobra.Command{
Use: "stop",
Short: "Stop processing messages on a uri",
RunE: func(cmd *cobra.Command, args []string) error {

v := proto.StopRequest{}

err := roundTrip(v, "stop", func(cli proto.HubClient, in rawConf, out iocodec.Encoder) error {
return stopCall(args, v, cli, in, out)
})
return err
},
}

func stopCall(args []string, request proto.StopRequest, cli proto.HubClient, in rawConf, out iocodec.Encoder) error {

err := in.Decode(&request)
if err != nil {
return err
}
if len(args) == 0 && request.Uri == "" {
return errors.New("specify a uri to stop")
}
if len(args) > 0 {
request.Uri = strings.TrimSpace(args[0])
}

resp, err := cli.Stop(context.Background(), &request)

if err != nil {
return err
}

return out.Encode(resp)
}

var statusCommand = &cobra.Command{
Use: "status",
Short: "List running pipes",
RunE: func(cmd *cobra.Command, args []string) error {

v := proto.StatusRequest{}

err := roundTrip(v, "status", func(cli proto.HubClient, in rawConf, out iocodec.Encoder) error {

resp, err := cli.Status(context.Background(), &v)

if err != nil {
return err
}

return out.Encode(resp)

})
return err
},
}
Loading