-
Notifications
You must be signed in to change notification settings - Fork 1
Preliminar - Client config file #91
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
Open
chespinoza
wants to merge
17
commits into
master
Choose a base branch
from
client-config-file
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a3e64f3
change name file to be consistent with the command name
chespinoza 4d89f5e
add config file
chespinoza 1f645e4
fix endpoint field
chespinoza 6a19ed0
Merge branch 'master' into client-config-file
chespinoza 40e3735
fix tag field
chespinoza 4c70933
changed type check on config file
chespinoza 95c7d68
use -f flag to load config file
chespinoza 5f92f11
partial commit
chespinoza a4e8e7d
fix config integration test
chespinoza 129a1d6
added feature "delete" resources files and stop "processes" with -d …
chespinoza 02947df
little fixes
chespinoza 8c2b09f
add aditional functionality to cliConfSlice
chespinoza cc54c2a
candidate changes
chespinoza 224faf5
delete describe
chespinoza da96884
update docs
chespinoza adba428
fixes
chespinoza d164960
fix start process using flags
chespinoza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| }, | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 :