Skip to content

Commit e18b314

Browse files
akajla09kkajla12
andauthored
Make api endpoint configurable (#13)
* Make api endpoint configurable * Update internal/cmd/init.go Co-authored-by: Karan Kajla <kkajla12@users.noreply.github.com> --------- Co-authored-by: Karan Kajla <kkajla12@users.noreply.github.com>
1 parent f370617 commit e18b314

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.19
55
require (
66
github.com/spf13/cobra v1.4.0
77
github.com/spf13/viper v1.11.0
8-
github.com/warrant-dev/warrant-go/v3 v3.0.1
8+
github.com/warrant-dev/warrant-go/v3 v3.1.0
99
)
1010

1111
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
171171
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
172172
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
173173
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
174-
github.com/warrant-dev/warrant-go/v3 v3.0.1 h1:80+7AlGGGkN/kSrVzg2fAAjkSwj+ga92yMjtswK3H8I=
175-
github.com/warrant-dev/warrant-go/v3 v3.0.1/go.mod h1:3AJyw63YkuJqy+dXwTXVOcy/HcVfTiXL2D6fEo7g07c=
174+
github.com/warrant-dev/warrant-go/v3 v3.1.0 h1:5WvmeOJMBwOhoi+Y6ztC20+RETeogUX7KC7Pwg4iSnE=
175+
github.com/warrant-dev/warrant-go/v3 v3.1.0/go.mod h1:3AJyw63YkuJqy+dXwTXVOcy/HcVfTiXL2D6fEo7g07c=
176176
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
177177
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
178178
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

internal/cmd/init.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,42 @@ func init() {
1616
}
1717

1818
type ConfigFile struct {
19-
Key string `json:"key"`
19+
Key string `json:"key"`
20+
ApiEndpoint string `json:"apiEndpoint"`
2021
}
2122

2223
var initCmd = &cobra.Command{
2324
Use: "init",
2425
Short: "Initialize the CLI for use",
25-
Long: "Initialize the CLI for use, including configuring the Warrant API key.",
26+
Long: "Initialize the CLI for use, including configuring server endpoint and API key.",
2627
Example: `
2728
warrant init`,
2829
Args: cobra.NoArgs,
2930
RunE: func(cmd *cobra.Command, args []string) error {
30-
fmt.Println("Please navigate to https://app.warrant.dev/account in your browser, login and retrieve your API key (prod or test) and enter it here:")
31+
fmt.Println("Warrant endpoint override (leave blank to use https://api.warrant.dev default):")
3132
fmt.Print("> ")
3233
buf := bufio.NewReader(os.Stdin)
3334
input, err := buf.ReadBytes('\n')
3435
if err != nil {
3536
return err
3637
}
37-
fmt.Println("Creating ~/.warrant.json")
38+
endpoint := strings.TrimSpace(string(input))
39+
if endpoint == "" {
40+
endpoint = "https://api.warrant.dev"
41+
}
42+
fmt.Println("API Key:")
43+
fmt.Print("> ")
44+
buf = bufio.NewReader(os.Stdin)
45+
input, err = buf.ReadBytes('\n')
46+
if err != nil {
47+
return err
48+
}
3849
key := strings.TrimSpace(string(input))
50+
51+
fmt.Println("Creating ~/.warrant.json")
3952
config := ConfigFile{
40-
Key: key,
53+
ApiEndpoint: endpoint,
54+
Key: key,
4155
}
4256
fileContents, err := json.MarshalIndent(config, "", " ")
4357
if err != nil {

internal/config/config.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package config
22

33
import (
4-
"fmt"
5-
64
"github.com/spf13/viper"
75
"github.com/warrant-dev/warrant-go/v3"
86
)
97

108
func InitClient() error {
11-
apiKey := viper.GetString("key")
12-
if apiKey == "" {
13-
return fmt.Errorf("missing API key. Please run `warrant init`")
14-
}
15-
warrant.ApiKey = apiKey
9+
warrant.ApiKey = viper.GetString("key")
10+
endpoint := viper.GetString("apiEndpoint")
11+
warrant.ApiEndpoint = endpoint
12+
warrant.AuthorizeEndpoint = endpoint
1613
return nil
1714
}

0 commit comments

Comments
 (0)