Skip to content

Commit

Permalink
Merge pull request #18 from shrijan00003/REST-15/env-with-flag
Browse files Browse the repository at this point in the history
REST-15/env-with-flag: environment is loaded with flag
  • Loading branch information
shrijan00003 authored Sep 4, 2024
2 parents 18a75b9 + 0e438ec commit c7729d8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
45 changes: 39 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,48 @@ curl -s https://raw.githubusercontent.com/shrijan00003/restler/main/install/linu
5. (Optional) Create `config.yaml` file inside the API collection folder for configurations like environment.
6. (Optional) Change folder for API collection in runtime by setting `RESTLER_PATH` environment variable. For example `export RESTLER_PATH=app-prefix-collection`
7. Run `restler <http-method> <request-name>` to run the request. For example `restler psot posts` to run post request and `restler get posts` to run get request.
8. Check the output files in `requests/<request-name>` folder. For example `requests/posts/.post.res.txt` for post response and `requests/posts/.get.res.txt` for get request response.
8. Check the output files in `requests/<request-name>` folder. For example `requests/posts/.<request-name>.post.res.md` for post response and `requests/posts/.get.res.txt` for get request response.
9. for other supports please check the [TODO](./todos/Todo.md) file.

## Commands Available
- `restler post <request-name>`
- `restler get <request-name>`
- `restler patch <request-name>`
- `restler put <request-name>`
- `restler delete <request-name>`
- `restler post <request-name>` or `restler p <request-name>`
- `restler get <request-name>` or `restler g <request-name>`
- `restler patch <request-name>` or `restler m <request-name>`
- `restler put <request-name>` or `restler u <request-name>`
- `restler delete <request-name>` or `restler d <request-name>`
- `restler --help` or `restler -h` or or `restler -h`
- `restler --version` or `restler -v`

## Flag support
Now all our REST method commands supports following flags:

### --env or -e
`env` flag is compatible for selecting environment from the command line. If we don't pass this option in command `Env` from config.yaml is default.

### Usage
```bash
restler <http-command> --env <env-value> <request name>
```
Fog eg,
```bash
restler p -e dev posts
```

### --request or -r
`request` flag is useful for seleting individual request from the request collection if you have multiple requests of same http method. Consider following structure:
```bash
reslter
requests
posts
- posts.post.yaml
- posts-v2.post.yaml


```
For running posts-v2, we can use ` -r ` like following:
```bash
restler p -r posts-v2 posts
```

## Proxy Usage

Expand Down
18 changes: 15 additions & 3 deletions bin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ func main() {
&cli.StringFlag{
Name: "request",
Aliases: []string{"r"},
Usage: "request to execute",
Value: "",
Usage: "Select request to execute",
},
&cli.StringFlag{
Name: "env",
Aliases: []string{"e"},
Usage: "Select env for request",
},
}

Expand Down Expand Up @@ -160,12 +164,20 @@ const (
)

func restAction(cCtx *cli.Context, actionName ActionName, restlerPath string) error {

var req = cCtx.Args().Get(0)
if req == "" {
log.Fatal("[Restler Error]: No request provided! Please provide request name as argument. Request name is the name of the folder in requests folder.")
}

// update env if env flag is set
envFlag := cCtx.String("env")
if envFlag != "" {
err := loadWithYaml(fmt.Sprintf("%s/env/%s.yaml", restlerPath, envFlag), &env)
if err != nil {
return fmt.Errorf("[Restler Error]: Environment you have selected is not found in %s/env folder \n", restlerPath)
}
}

var reqPath = fmt.Sprintf("%s/requests/%s", restlerPath, req)
if _, err := os.Stat(reqPath); os.IsNotExist(err) {
log.Fatal("[Restler Error]: Request directory not found, please check the path. Request Directory Path: ", reqPath)
Expand Down

0 comments on commit c7729d8

Please sign in to comment.