diff --git a/cmd/root.go b/cmd/root.go index 44f5268..7f8b0ca 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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" ) @@ -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)}, } ) diff --git a/cmd/run.go b/cmd/run.go index 506c1cb..b5028bf 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -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) } diff --git a/pkg/request/authentication/api.go b/pkg/authentication/api.go similarity index 86% rename from pkg/request/authentication/api.go rename to pkg/authentication/api.go index 81a7913..549b686 100644 --- a/pkg/request/authentication/api.go +++ b/pkg/authentication/api.go @@ -5,9 +5,10 @@ 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 { @@ -15,7 +16,7 @@ type ApiAuth struct { 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"` } diff --git a/pkg/request/authentication/api_test.go b/pkg/authentication/api_test.go similarity index 76% rename from pkg/request/authentication/api_test.go rename to pkg/authentication/api_test.go index ffcc27b..01c21e7 100644 --- a/pkg/request/authentication/api_test.go +++ b/pkg/authentication/api_test.go @@ -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 @@ -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", @@ -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) @@ -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" } @@ -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") } diff --git a/pkg/request/authentication/authentication.go b/pkg/authentication/authentication.go similarity index 88% rename from pkg/request/authentication/authentication.go rename to pkg/authentication/authentication.go index 329066d..7ffb0d6 100644 --- a/pkg/request/authentication/authentication.go +++ b/pkg/authentication/authentication.go @@ -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 @@ -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"` } diff --git a/pkg/request/authentication/authentication_test.go b/pkg/authentication/authentication_test.go similarity index 86% rename from pkg/request/authentication/authentication_test.go rename to pkg/authentication/authentication_test.go index 21f9b3a..8f32714 100644 --- a/pkg/request/authentication/authentication_test.go +++ b/pkg/authentication/authentication_test.go @@ -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 `) @@ -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" @@ -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" } @@ -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 @@ -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 @@ -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 diff --git a/pkg/request/authentication/basic.go b/pkg/authentication/basic.go similarity index 87% rename from pkg/request/authentication/basic.go rename to pkg/authentication/basic.go index d033f33..7bc9678 100644 --- a/pkg/request/authentication/basic.go +++ b/pkg/authentication/basic.go @@ -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 BasicAuth struct { @@ -16,7 +16,7 @@ type BasicAuth struct { 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"` } diff --git a/pkg/request/authentication/basic_test.go b/pkg/authentication/basic_test.go similarity index 82% rename from pkg/request/authentication/basic_test.go rename to pkg/authentication/basic_test.go index 8ef9d78..d6c8869 100644 --- a/pkg/request/authentication/basic_test.go +++ b/pkg/authentication/basic_test.go @@ -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) { @@ -17,7 +17,7 @@ auth: password: world ` - template := request.Template(data) + template := template.Template(data) auth, err := NewBasicAuth(&template) assert.NoError(t, err) @@ -35,7 +35,7 @@ auth: } } ` - template := request.Template(data) + template := template.Template(data) auth, err := NewBasicAuth(&template) assert.NoError(t, err) @@ -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) @@ -61,7 +61,7 @@ auth: } ` - template := request.Template(data) + template := template.Template(data) auth, err := NewBasicAuth(&template) assert.NoError(t, err) @@ -76,7 +76,7 @@ auth: } } ` - template := request.Template(data) + template := template.Template(data) auth, err := NewBasicAuth(&template) assert.Error(t, err) @@ -92,7 +92,7 @@ auth: } } ` - template := request.Template(data) + template := template.Template(data) auth, err := NewBasicAuth(&template) assert.Error(t, err) @@ -109,7 +109,7 @@ auth: } } ` - template := request.Template(data) + template := template.Template(data) auth, err := NewBasicAuth(&template) assert.Error(t, err) @@ -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) diff --git a/pkg/request/authentication/bearer.go b/pkg/authentication/bearer.go similarity index 86% rename from pkg/request/authentication/bearer.go rename to pkg/authentication/bearer.go index f54a1e9..90fc5bd 100644 --- a/pkg/request/authentication/bearer.go +++ b/pkg/authentication/bearer.go @@ -5,17 +5,17 @@ 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 BearerAuth struct { Token string `json:"token" yaml:"token"` } -func NewBearerAuth(t *request.Template) (*BearerAuth, error) { +func NewBearerAuth(t *template.Template) (*BearerAuth, error) { var wrapper struct { Auth *BearerAuth `json:"auth" yaml:"auth"` } diff --git a/pkg/request/authentication/bearer_test.go b/pkg/authentication/bearer_test.go similarity index 73% rename from pkg/request/authentication/bearer_test.go rename to pkg/authentication/bearer_test.go index f4ceafb..dfe953c 100644 --- a/pkg/request/authentication/bearer_test.go +++ b/pkg/authentication/bearer_test.go @@ -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 TestNewBearerAuth(t *testing.T) { t.Run("yaml", func(t *testing.T) { - template := request.Template(` + template := template.Template(` auth: token: helloworld `) @@ -23,7 +23,7 @@ func TestNewBearerAuth(t *testing.T) { }) t.Run("json", func(t *testing.T) { - template := request.Template(` + template := template.Template(` { "auth": { "token": "helloworld" @@ -39,7 +39,7 @@ func TestNewBearerAuth(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 := NewBearerAuth(&template) @@ -48,7 +48,7 @@ func TestNewBearerAuth(t *testing.T) { }) t.Run("template without auth", func(t *testing.T) { - template := request.Template(` + template := template.Template(` { "hello": "world" } @@ -66,12 +66,12 @@ func TestBearerAuthApply(t *testing.T) { Token: "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) - bearer.Apply(request) + bearer.Apply(template) - assert.Equal(t, request.Header.Get("Authorization"), "Bearer helloworld") + assert.Equal(t, template.Header.Get("Authorization"), "Bearer helloworld") } diff --git a/pkg/builder/builder.go b/pkg/builder/builder.go new file mode 100644 index 0000000..8d35cef --- /dev/null +++ b/pkg/builder/builder.go @@ -0,0 +1,60 @@ +package builder + +import ( + "github.com/eynopv/lac/pkg/authentication" + "github.com/eynopv/lac/pkg/client" + "github.com/eynopv/lac/pkg/printer" + "github.com/eynopv/lac/pkg/request" + "github.com/eynopv/lac/pkg/template" + "github.com/eynopv/lac/pkg/utils" + "github.com/eynopv/lac/pkg/variables" +) + +type Builder struct { + ClientConfig client.ClientConfig + TemplatePath string + Variables variables.Variables + Headers map[string]request.StringOrStringList +} + +func (b *Builder) BuildClient() *client.Client { + return client.NewClient(&b.ClientConfig) +} + +func (b *Builder) BuildTemplate() (*template.Template, error) { + return template.NewTemplate(b.TemplatePath) +} + +func (b *Builder) BuildRequest() (*request.Request, error) { + t, err := b.BuildTemplate() + if err != nil { + return nil, err + } + + t = t.Interpolate(b.Variables, true) + + r, err := t.Parse() + if err != nil { + return nil, err + } + + r.Headers = utils.CombineMaps(b.Headers, r.Headers) + + return r, nil +} + +func (b *Builder) BuildAuth() (authentication.Auth, error) { + t, err := b.BuildTemplate() + if err != nil { + return nil, err + } + + t = t.Interpolate(b.Variables, true) + + return authentication.NewAuth(t) +} + +func (b *Builder) BuildPrinter() *printer.Printer { + p := printer.NewPrinter(b.ClientConfig.PrinterConfig) + return &p +} diff --git a/pkg/client/client.go b/pkg/client/client.go index b76a3b9..e567b55 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -5,9 +5,9 @@ import ( "net/http" "time" + "github.com/eynopv/lac/pkg/authentication" "github.com/eynopv/lac/pkg/printer" "github.com/eynopv/lac/pkg/request" - "github.com/eynopv/lac/pkg/request/authentication" "github.com/eynopv/lac/pkg/result" ) diff --git a/pkg/request/template.go b/pkg/template/template.go similarity index 82% rename from pkg/request/template.go rename to pkg/template/template.go index 6571d85..ffbb1dd 100644 --- a/pkg/request/template.go +++ b/pkg/template/template.go @@ -1,4 +1,4 @@ -package request +package template import ( "encoding/json" @@ -8,6 +8,7 @@ import ( yaml "gopkg.in/yaml.v3" "github.com/eynopv/lac/pkg/param" + "github.com/eynopv/lac/pkg/request" "github.com/eynopv/lac/pkg/utils" "github.com/eynopv/lac/pkg/variables" ) @@ -34,8 +35,8 @@ func (t *Template) Interpolate(vars variables.Variables, useEnv bool) *Template return &result } -func (t *Template) Parse() (*Request, error) { - var requestData RequestData +func (t *Template) Parse() (*request.Request, error) { + var requestData request.RequestData err := json.Unmarshal([]byte(*t), &requestData) if err != nil { @@ -46,7 +47,7 @@ func (t *Template) Parse() (*Request, error) { return nil, fmt.Errorf("%w: %v", ErrTemplateParse, err) } - nr := NewRequest(requestData) + nr := request.NewRequest(requestData) return &nr, nil } diff --git a/pkg/request/template_test.go b/pkg/template/template_test.go similarity index 99% rename from pkg/request/template_test.go rename to pkg/template/template_test.go index 1f4d120..0a517f9 100644 --- a/pkg/request/template_test.go +++ b/pkg/template/template_test.go @@ -1,4 +1,4 @@ -package request +package template import ( "os"