Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"github.com/joho/godotenv"
"github.com/spf13/cobra"

"github.com/eynopv/lac/pkg/builder"
"github.com/eynopv/lac/pkg/client"
"github.com/eynopv/lac/pkg/request"
"github.com/eynopv/lac/pkg/utils"
"github.com/eynopv/lac/pkg/variables"
)
Expand All @@ -35,18 +37,24 @@ var (
return nil
},
Run: func(cmd *cobra.Command, args []string) {
runCommandFunction(args, Variables, Headers, &ClientConfig)
b := builder.Builder{
ClientConfig: ClientConfig,
TemplatePath: args[0],
Variables: Variables,
Headers: defaultHeaders,
}
runCommandFunction(&b)
},
}

VariablesInput []string
EnvironmentFilePathInput string
PrintParameters string
ClientConfig client.ClientConfig
Variables variables.Variables

ClientConfig client.ClientConfig
Variables variables.Variables
Headers = map[string]string{
"User-Agent": fmt.Sprintf("lac/%s", version),
defaultHeaders = map[string]request.StringOrStringList{
"User-Agent": []string{fmt.Sprintf("lac/%s", version)},
}
)

Expand Down
51 changes: 8 additions & 43 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,30 @@ import (
"fmt"
"os"

"github.com/eynopv/lac/pkg/client"
"github.com/eynopv/lac/pkg/param"
"github.com/eynopv/lac/pkg/printer"
"github.com/eynopv/lac/pkg/request"
"github.com/eynopv/lac/pkg/request/authentication"
"github.com/eynopv/lac/pkg/utils"
"github.com/eynopv/lac/pkg/variables"
"github.com/eynopv/lac/pkg/builder"
)

func runCommandFunction(
args []string,
variables variables.Variables,
headers map[string]string,
clientConfig *client.ClientConfig,
) {
requestTemplate, err := request.NewTemplate(args[0])
func runCommandFunction(bldr *builder.Builder) {
req, err := bldr.BuildRequest()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

requestTemplate = requestTemplate.Interpolate(variables, true)

req, err := requestTemplate.Parse()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

auth, err := authentication.NewAuth(requestTemplate)
auth, err := bldr.BuildAuth()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

runRequest(req, variables, headers, client.NewClient(clientConfig), auth)
}

func runRequest(
req *request.Request,
variables variables.Variables,
headers map[string]string,
client *client.Client,
auth authentication.Auth,
) {
resolvedHeaders := map[string]request.StringOrStringList{}
for key, value := range headers {
resolvedHeaders[key] = []string{param.Param(value).Resolve(variables, true)}
}

req.Headers = utils.CombineMaps(resolvedHeaders, req.Headers)

result, err := client.Do(req, auth)
c := bldr.BuildClient()

result, err := c.Do(req, auth)
if err != nil {
fmt.Printf("Error sending request: %v\n", err)
fmt.Println(err)
os.Exit(1)
}

prntr := printer.NewPrinter(client.PrinterConfig)
prntr := bldr.BuildPrinter()
prntr.Print(result)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
"fmt"
"net/http"

yaml "gopkg.in/yaml.v3"

"github.com/eynopv/lac/internal/errorsx"
"github.com/eynopv/lac/pkg/request"
"gopkg.in/yaml.v3"
"github.com/eynopv/lac/pkg/template"
)

type ApiAuth struct {
Header string `json:"header" yaml:"header"`
Key string `json:"key" yaml:"key"`
}

func NewApiAuth(t *request.Template) (*ApiAuth, error) {
func NewApiAuth(t *template.Template) (*ApiAuth, error) {
var wrapper struct {
Auth *ApiAuth `json:"auth" yaml:"auth"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"testing"

"github.com/eynopv/lac/internal/assert"
"github.com/eynopv/lac/pkg/request"
"github.com/eynopv/lac/pkg/template"
)

func TestNewApiAuth(t *testing.T) {
t.Run("yaml", func(t *testing.T) {
template := request.Template(`
template := template.Template(`
auth:
header: x-api-key
key: helloworld
Expand All @@ -25,7 +25,7 @@ func TestNewApiAuth(t *testing.T) {
})

t.Run("json", func(t *testing.T) {
template := request.Template(`
template := template.Template(`
{
"auth": {
"header": "x-api-key",
Expand All @@ -43,7 +43,7 @@ func TestNewApiAuth(t *testing.T) {
})

t.Run("invalid", func(t *testing.T) {
template := request.Template("this is invalid template")
template := template.Template("this is invalid template")

auth, err := NewApiAuth(&template)

Expand All @@ -52,7 +52,7 @@ func TestNewApiAuth(t *testing.T) {
})

t.Run("template without auth", func(t *testing.T) {
template := request.Template(`
template := template.Template(`
{
"hello": "world"
}
Expand All @@ -71,12 +71,12 @@ func TestApiAuthApply(t *testing.T) {
Key: "helloworld",
}

request, err := http.NewRequest(http.MethodGet, "", nil)
template, err := http.NewRequest(http.MethodGet, "", nil)

assert.NotNil(t, request)
assert.NotNil(t, template)
assert.NoError(t, err)

apiAuth.Apply(request)
apiAuth.Apply(template)

assert.Equal(t, request.Header.Get("X-Api-Key"), "helloworld")
assert.Equal(t, template.Header.Get("X-Api-Key"), "helloworld")
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"net/http"

"gopkg.in/yaml.v3"
yaml "gopkg.in/yaml.v3"

"github.com/eynopv/lac/internal/errorsx"
"github.com/eynopv/lac/pkg/request"
"github.com/eynopv/lac/pkg/template"
)

type AuthType string
Expand All @@ -28,7 +28,7 @@ type AuthBase struct {
Type AuthType `json:"type" yaml:"type"`
}

func NewAuth(t *request.Template) (Auth, error) {
func NewAuth(t *template.Template) (Auth, error) {
var wrapper struct {
Auth *AuthBase `json:"auth" yaml:"auth"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (

"github.com/eynopv/lac/internal/assert"
"github.com/eynopv/lac/internal/errorsx"
"github.com/eynopv/lac/pkg/request"
"github.com/eynopv/lac/pkg/template"
)

func TestNewAuth(t *testing.T) {
t.Run("yaml", func(t *testing.T) {
template := request.Template(`
template := template.Template(`
auth:
type: unknown
`)
Expand All @@ -23,7 +23,7 @@ func TestNewAuth(t *testing.T) {
})

t.Run("json", func(t *testing.T) {
template := request.Template(`
template := template.Template(`
{
"auth": {
"type": "unknown"
Expand All @@ -39,7 +39,7 @@ func TestNewAuth(t *testing.T) {
})

t.Run("no auth", func(t *testing.T) {
template := request.Template(`
template := template.Template(`
{
"hello": "world"
}
Expand All @@ -52,7 +52,7 @@ func TestNewAuth(t *testing.T) {
})

t.Run("basic", func(t *testing.T) {
template := request.Template(`
template := template.Template(`
auth:
type: basic
username: hello
Expand All @@ -67,7 +67,7 @@ func TestNewAuth(t *testing.T) {
})

t.Run("bearer", func(t *testing.T) {
template := request.Template(`
template := template.Template(`
auth:
type: bearer
token: helloworld
Expand All @@ -81,7 +81,7 @@ func TestNewAuth(t *testing.T) {
})

t.Run("api", func(t *testing.T) {
template := request.Template(`
template := template.Template(`
auth:
type: api
header: x-api-key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"fmt"
"net/http"

"gopkg.in/yaml.v3"
yaml "gopkg.in/yaml.v3"

"github.com/eynopv/lac/internal/errorsx"
"github.com/eynopv/lac/pkg/request"
"github.com/eynopv/lac/pkg/template"
)

type BasicAuth struct {
Username string `json:"username" yaml:"username"`
Password string `json:"password" yaml:"password"`
}

func NewBasicAuth(t *request.Template) (*BasicAuth, error) {
func NewBasicAuth(t *template.Template) (*BasicAuth, error) {
var wrapper struct {
Auth *BasicAuth `json:"auth" yaml:"auth"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/eynopv/lac/internal/assert"
"github.com/eynopv/lac/internal/errorsx"
"github.com/eynopv/lac/pkg/request"
"github.com/eynopv/lac/pkg/template"
)

func TestNewBasicAuth(t *testing.T) {
Expand All @@ -17,7 +17,7 @@ auth:
password: world
`

template := request.Template(data)
template := template.Template(data)
auth, err := NewBasicAuth(&template)

assert.NoError(t, err)
Expand All @@ -35,7 +35,7 @@ auth:
}
}
`
template := request.Template(data)
template := template.Template(data)
auth, err := NewBasicAuth(&template)

assert.NoError(t, err)
Expand All @@ -47,7 +47,7 @@ auth:
t.Run("invalid", func(t *testing.T) {
data := "this is invalid template"

template := request.Template(data)
template := template.Template(data)
auth, err := NewBasicAuth(&template)

assert.Error(t, err)
Expand All @@ -61,7 +61,7 @@ auth:
}
`

template := request.Template(data)
template := template.Template(data)
auth, err := NewBasicAuth(&template)

assert.NoError(t, err)
Expand All @@ -76,7 +76,7 @@ auth:
}
}
`
template := request.Template(data)
template := template.Template(data)
auth, err := NewBasicAuth(&template)

assert.Error(t, err)
Expand All @@ -92,7 +92,7 @@ auth:
}
}
`
template := request.Template(data)
template := template.Template(data)
auth, err := NewBasicAuth(&template)

assert.Error(t, err)
Expand All @@ -109,7 +109,7 @@ auth:
}
}
`
template := request.Template(data)
template := template.Template(data)
auth, err := NewBasicAuth(&template)

assert.Error(t, err)
Expand All @@ -124,14 +124,14 @@ func TestBasicAuthApply(t *testing.T) {
Password: "world",
}

request, err := http.NewRequest(http.MethodGet, "", nil)
req, err := http.NewRequest(http.MethodGet, "", nil)

assert.NotNil(t, request)
assert.NotNil(t, req)
assert.NoError(t, err)

basic.Apply(request)
basic.Apply(req)

username, password, ok := request.BasicAuth()
username, password, ok := req.BasicAuth()
assert.True(t, ok)
assert.Equal(t, basic.Username, username)
assert.Equal(t, basic.Password, password)
Expand Down
Loading