Skip to content

Commit

Permalink
Feature/service URL as var (#28)
Browse files Browse the repository at this point in the history
* update

* enhance matching

* support from
  • Loading branch information
Clivern authored Aug 4, 2020
1 parent 3bb60fc commit c7e591f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 16 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<p align="center">A fast and beautiful command line tool to build API requests</p>
<p align="center">
<a href="https://travis-ci.com/Clivern/Poodle"><img src="https://travis-ci.com/Clivern/Poodle.svg?branch=master"></a>
<a href="https://github.com/Clivern/Poodle/releases"><img src="https://img.shields.io/badge/Version-2.1.0-red.svg"></a>
<a href="https://goreportcard.com/report/github.com/Clivern/Poodle"><img src="https://goreportcard.com/badge/github.com/Clivern/Poodle?v=2.1.0"></a>
<a href="https://github.com/Clivern/Poodle/releases"><img src="https://img.shields.io/badge/Version-2.2.0-red.svg"></a>
<a href="https://goreportcard.com/report/github.com/Clivern/Poodle"><img src="https://goreportcard.com/badge/github.com/Clivern/Poodle?v=2.2.0"></a>
<a href="https://github.com/Clivern/Poodle/blob/master/LICENSE"><img src="https://img.shields.io/badge/LICENSE-MIT-orange.svg"></a>
</p>
</p>
Expand Down Expand Up @@ -107,6 +107,13 @@ To start calling your services endpoints:
$ poodle call
```

To start calling a custom service endpoints without storing it globally:

```zsh
# Asuming that .poodle.toml exists on current dir
$ poodle call -f ./.poodle.toml
```

To delete a service definition file:

```zsh
Expand Down
25 changes: 24 additions & 1 deletion cmd/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cmd

import (
"fmt"
"path/filepath"
"strings"
"time"

Expand All @@ -18,6 +19,9 @@ import (
"github.com/spf13/cobra"
)

// From var
var From string

var callCmd = &cobra.Command{
Use: "call",
Short: "Interact with one of the configured services",
Expand Down Expand Up @@ -50,7 +54,16 @@ var callCmd = &cobra.Command{
return
}

files, err := util.ListFiles(util.EnsureTrailingSlash(conf.Services.Directory))
files := make(map[string]util.File)

if From == "" || !util.FileExists(From) {
files, err = util.ListFiles(util.EnsureTrailingSlash(conf.Services.Directory))
} else {
files[filepath.Base(From)] = util.File{
Path: From,
Name: filepath.Base(From),
}
}

if err != nil {
fmt.Printf(
Expand Down Expand Up @@ -162,6 +175,16 @@ var callCmd = &cobra.Command{
},
}

func init() {
callCmd.PersistentFlags().StringVarP(
&From,
"from",
"f",
"./.poodle.toml",
"service definition file",
)
}

func init() {
rootCmd.AddCommand(callCmd)
}
50 changes: 37 additions & 13 deletions core/module/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func (c *Caller) GetFields(endpointID string, service *model.Service) map[string
continue
}

// Get Service URL
fields = c.MergeFields(fields, c.ParseFields(service.Main.ServiceURL))

// Get api key if auth is api_key
if service.Security.Scheme == "api_key" && !end.Public {
fields = c.MergeFields(fields, c.ParseFields(service.Security.APIKey.Header[1]))
Expand Down Expand Up @@ -88,28 +91,49 @@ func (c *Caller) GetFields(endpointID string, service *model.Service) map[string
// ParseFields parses a string to fetch fields
func (c *Caller) ParseFields(data string) map[string]Field {
var ita []string
var key string
var value string

m := regexp.MustCompile(`{\$(.*?)}`)
items := m.FindAllString(data, -1)
fields := make(map[string]Field)

for _, item := range items {
item = strings.Replace(item, "$", "", -1)
item = strings.Replace(item, "{", "", -1)
item = strings.Replace(item, "}", "", -1)

if strings.Contains(item, ":") {
ita = strings.Split(item, ":")
fields[ita[0]] = Field{
Prompt: fmt.Sprintf(`$%s (default='%s'):`, ita[0], Yellow(ita[1])),
IsOptional: true,
Default: ita[1],
}
} else {

if !strings.HasPrefix(item, `{$`) || !strings.HasSuffix(item, "}") {
continue
}

if !strings.Contains(item, ":") {
// Incase the item in this format {$var}
item = strings.Replace(item, "$", "", -1)
item = strings.Replace(item, "{", "", -1)
item = strings.Replace(item, "}", "", -1)

fields[item] = Field{
Prompt: fmt.Sprintf(`$%s%s (default=''):`, item, Red("*")),
IsOptional: false,
Default: "",
}
continue
}
// Incase the item in this format {$var:default} or {$var:something:complex}
// this will match and extract {$....: part
m = regexp.MustCompile(`{\$(.*?):`)
ita = m.FindAllString(item, -1)
key = strings.TrimPrefix(ita[0], `{$`)
key = strings.TrimSuffix(key, `:`)

// this will match and extract :.....} part
m = regexp.MustCompile(`:(.*?)}`)
ita = m.FindAllString(item, -1)
value = strings.TrimPrefix(ita[0], `:`)
value = strings.TrimSuffix(value, `}`)

fields[key] = Field{
Prompt: fmt.Sprintf(`$%s (default='%s'):`, key, Yellow(value)),
IsOptional: true,
Default: value,
}
}

Expand All @@ -136,7 +160,7 @@ func (c *Caller) Call(endpointID string, service *model.Service, fields map[stri

url := fmt.Sprintf(
"%s%s",
util.EnsureTrailingSlash(service.Main.ServiceURL),
util.EnsureTrailingSlash(c.ReplaceVars(service.Main.ServiceURL, fields)),
util.RemoveStartingSlash(c.ReplaceVars(end.URI, fields)),
)

Expand Down
1 change: 1 addition & 0 deletions misc/service_definition.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
name = "clivern - poodle"
description = ""
timeout = "30s"
# service_url can also be a variable with default value {$serviceURL:http://127.0.0.1:8080}
service_url = "https://example.com/api/v1"
# These headers will be applied to all endpoints http calls
headers = [ ["Content-Type", "application/json"] ]
Expand Down

0 comments on commit c7e591f

Please sign in to comment.