Skip to content

Commit d27f407

Browse files
committed
feat: add flag to choose path of configuration file
1 parent 670e7ce commit d27f407

File tree

7 files changed

+15
-9
lines changed

7 files changed

+15
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ By default, [.req.json](https://github.com/ItsJimi/req/blob/master/.req.json) mu
101101
```
102102
### Commands
103103
All commands can use `--help` or `-h` to display its specific help.
104+
All commands can use `--config` or `-c` to use a custom `.json` path. (By default req use a `.req.json` in your home directory)
105+
104106
#### help
105107
Display helper
106108
```shell

cmd/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var listCmd = &cobra.Command{
1616
Short: "List all requests names",
1717
Long: `List all requests names`,
1818
Run: func(cmd *cobra.Command, args []string) {
19-
results, err := req.List()
19+
results, err := req.List(Path)
2020
if err != nil {
2121
panic(err)
2222
}

cmd/root.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ var rootCmd = &cobra.Command{
1616
},
1717
}
1818

19+
// Path expose config path
20+
var Path string
21+
22+
// Execute cli
1923
func Execute() {
24+
rootCmd.PersistentFlags().StringVarP(&Path, "config", "c", os.Getenv("HOME")+"/.req.json", "Path of config file (Must be a JSON file)")
25+
2026
if err := rootCmd.Execute(); err != nil {
2127
fmt.Println(err)
2228
os.Exit(1)

cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var runCmd = &cobra.Command{
2020
Long: `Run a request`,
2121
Args: cobra.MinimumNArgs(1),
2222
Run: func(cmd *cobra.Command, args []string) {
23-
results, err := req.Send(args)
23+
results, err := req.Send(args, Path)
2424
if err != nil {
2525
panic(err)
2626
}

cmd/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ var versionCmd = &cobra.Command{
1515
Short: "Print the version number of Req",
1616
Long: `Print the version number of Req`,
1717
Run: func(cmd *cobra.Command, args []string) {
18-
fmt.Println("0.1.0")
18+
fmt.Println("0.2.0")
1919
},
2020
}

req/list.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package req
33
import (
44
"encoding/json"
55
"io/ioutil"
6-
"os"
76
)
87

98
// List a requests with config file
10-
func List() ([]string, error) {
11-
fileData, err := ioutil.ReadFile(os.Getenv("HOME") + "/.req.json")
9+
func List(path string) ([]string, error) {
10+
fileData, err := ioutil.ReadFile(path)
1211
if err != nil {
1312
return nil, err
1413
}

req/send.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"io/ioutil"
88
"net/http"
9-
"os"
109
"strings"
1110
)
1211

@@ -20,8 +19,8 @@ func contains(text string, elements []string) bool {
2019
}
2120

2221
// Send a request with config file
23-
func Send(names []string) ([]string, error) {
24-
fileData, err := ioutil.ReadFile(os.Getenv("HOME") + "/.req.json")
22+
func Send(names []string, path string) ([]string, error) {
23+
fileData, err := ioutil.ReadFile(path)
2524
if err != nil {
2625
return nil, err
2726
}

0 commit comments

Comments
 (0)