bump go version #2

Closed
tiburon wants to merge 8 commits from initial_code into master
4 changed files with 37 additions and 15 deletions
Showing only changes of commit 5b154fd269 - Show all commits

View File

@ -4,10 +4,9 @@ package main
import ( import (
"context" "context"
"flag"
"fmt" "fmt"
"github.com/go-playground/validator/v10" "github.com/tiburon-777/api-crowler/internal/config"
"github.com/tiburon-777/w3back/internal/scenario" "github.com/tiburon-777/api-crowler/internal/scenario"
"log" "log"
"net/http" "net/http"
"strings" "strings"
@ -15,21 +14,18 @@ import (
func main() { func main() {
// Парсим флаги приложения. // Парсим флаги приложения.
file := flag.String("scenario", "./test/test.yaml", "path to file with test scenario") conf, err := config.Get()
flag.Parse()
ctx := context.Background()
// Читаем файл сценария.
sc, err := scenario.New(ctx, *file)
if err != nil { if err != nil {
log.Fatalf("can't understand test scenario: %v ", err) log.Fatalf("invalid config: %v ", err)
} }
// Валидируем сценарий // Читаем файл сценария.
if err := validator.New().Struct(sc); err != nil { sc, err := scenario.Get(conf.Scenario)
if err != nil {
log.Fatalf("invalid scenario: %v ", err) log.Fatalf("invalid scenario: %v ", err)
} }
ctx := context.Background()
cli := http.Client{Timeout: sc.Params.Timeout} cli := http.Client{Timeout: sc.Params.Timeout}
// Запускаем сценарий // Запускаем сценарий

2
go.mod
View File

@ -1,4 +1,4 @@
module github.com/tiburon-777/w3back module github.com/tiburon-777/api-crowler
go 1.18 go 1.18

22
internal/config/config.go Normal file
View File

@ -0,0 +1,22 @@
package config
import (
"flag"
"github.com/go-playground/validator/v10"
)
type Config struct {
Scenario string `validate:"required"`
}
func Get() (*Config, error) {
conf := &Config{}
flag.StringVar(&conf.Scenario, "scenario", "", "URL base")
flag.Parse()
// Валидируем параметры
if err := validator.New().Struct(conf); err != nil {
return nil, err
}
return conf, nil
}

View File

@ -1,12 +1,12 @@
package scenario package scenario
import ( import (
"context" "github.com/go-playground/validator/v10"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"os" "os"
) )
func New(ctx context.Context, file string) (*Object, error) { func Get(file string) (*Object, error) {
var obj Object var obj Object
yamlFile, err := os.ReadFile(file) yamlFile, err := os.ReadFile(file)
if err != nil { if err != nil {
@ -17,5 +17,9 @@ func New(ctx context.Context, file string) (*Object, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := validator.New().Struct(obj); err != nil {
return nil, err
}
return &obj, nil return &obj, nil
} }