From b3c7cb10990bd0d5a10aac6dc7931ab7526f10a0 Mon Sep 17 00:00:00 2001 From: Softonik Date: Tue, 25 Jun 2024 22:08:15 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=BB=D0=B5=D0=BD=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20=D0=BA=D0=BE=D0=BD=D1=84?= =?UTF-8?q?=D0=B8=D0=B3=D0=B0=20=D0=B8=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=B7=20=D0=BD=D0=B5?= =?UTF-8?q?=D0=B3=D0=BE=20=D0=B4=D0=BE=D1=85=D0=BE=D0=B4=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/schet/main.go | 29 ++++++++++++++++ go.mod | 1 + go.sum | 2 ++ pkg/input/conf/conf.go | 48 +++++++++++++++++++++++++++ pkg/input/conf/conf_test.go | 31 ++++++++++++++++++ pkg/input/conf/config.go | 15 +++++++++ pkg/input/conf/features/app.feature | 3 ++ pkg/input/conf/init_test.go | 51 +++++++++++++++++++++++++++++ 8 files changed, 180 insertions(+) create mode 100644 pkg/input/conf/conf.go create mode 100644 pkg/input/conf/conf_test.go create mode 100644 pkg/input/conf/config.go create mode 100644 pkg/input/conf/features/app.feature create mode 100644 pkg/input/conf/init_test.go diff --git a/cmd/schet/main.go b/cmd/schet/main.go index 16feaf2..005b239 100644 --- a/cmd/schet/main.go +++ b/cmd/schet/main.go @@ -1,8 +1,11 @@ package main import ( + "fmt" + "my/schet/pkg/input/conf" "my/schet/pkg/output/cli" "my/schet/pkg/schet" + "os" ) const ( @@ -11,6 +14,32 @@ const ( func main() { g := schet.NewГод(Год) + + err := загрузитьКонфиг(g) + if err != nil { + return + } + + вывестиГод(g) +} + +func загрузитьКонфиг(g *schet.Год) error { + config_path := os.Args[1] + c := conf.NewConf(config_path) + err := c.Считать() + if err != nil { + fmt.Printf("Чтение конфига: %v\n", err) + return err + } + err = c.Загрузить(g) + if err != nil { + fmt.Printf("Загрузка данных: %v\n", err) + return err + } + return nil +} + +func вывестиГод(g *schet.Год) { output := cli.NewCLI() g.Вывести(output) } diff --git a/go.mod b/go.mod index c3b36a3..79d3cac 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/hashicorp/golang-lru v0.5.4 // indirect github.com/onsi/gomega v1.27.4 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/yosuke-furukawa/json5 v0.1.1 // indirect golang.org/x/net v0.8.0 // indirect golang.org/x/text v0.8.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 63f857b..9b2e182 100644 --- a/go.sum +++ b/go.sum @@ -46,6 +46,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yosuke-furukawa/json5 v0.1.1 h1:0F9mNwTvOuDNH243hoPqvf+dxa5QsKnZzU20uNsh3ZI= +github.com/yosuke-furukawa/json5 v0.1.1/go.mod h1:sw49aWDqNdRJ6DYUtIQiaA3xyj2IL9tjeNYmX2ixwcU= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= diff --git a/pkg/input/conf/conf.go b/pkg/input/conf/conf.go new file mode 100644 index 0000000..b1f1e3e --- /dev/null +++ b/pkg/input/conf/conf.go @@ -0,0 +1,48 @@ +package conf + +import ( + "io" + "my/schet/pkg/schet" + "os" + + "github.com/yosuke-furukawa/json5/encoding/json5" +) + +type Conf struct { + path string + config *Config + год *schet.Год +} + +func NewConf(path string) *Conf { + c := &Conf{ + config: NewConfig(), + path: path, + } + return c +} + +func (c *Conf) Считать() error { + f, err := os.Open(c.path) + if err != nil { + return err + } + defer f.Close() + + data, err := io.ReadAll(f) + if err != nil { + return err + } + + return json5.Unmarshal(data, c.config) +} + +func (c *Conf) Загрузить(год *schet.Год) error { + c.год = год + + for _, d := range c.config.Доходы { + c.год.ДобавитьДоход(d.Сумма, d.Квартал) + } + + return nil +} diff --git a/pkg/input/conf/conf_test.go b/pkg/input/conf/conf_test.go new file mode 100644 index 0000000..f668856 --- /dev/null +++ b/pkg/input/conf/conf_test.go @@ -0,0 +1,31 @@ +package conf + +import ( + . "github.com/onsi/gomega" +) + +type testData struct { +} + +var ( + t *testData +) + +func resetTestData() { + t = &testData{} +} + +func beforeSuite() { +} +func afterSuite() { +} + +func beforeScenario() { + resetTestData() + _ = Ω + +} +func afterScenario() { +} + +// ----------------------- diff --git a/pkg/input/conf/config.go b/pkg/input/conf/config.go new file mode 100644 index 0000000..f1553aa --- /dev/null +++ b/pkg/input/conf/config.go @@ -0,0 +1,15 @@ +package conf + +type Config struct { + Доходы []Доход `json:"Доходы"` +} + +type Доход struct { + Сумма float64 + Квартал int + Клиент string +} + +func NewConfig() *Config { + return &Config{} +} diff --git a/pkg/input/conf/features/app.feature b/pkg/input/conf/features/app.feature new file mode 100644 index 0000000..6089cb3 --- /dev/null +++ b/pkg/input/conf/features/app.feature @@ -0,0 +1,3 @@ +# Во имя Бога Милостивого, Милосердного!!! +# language: ru +Функциональность: Конфиг diff --git a/pkg/input/conf/init_test.go b/pkg/input/conf/init_test.go new file mode 100644 index 0000000..81b0eb6 --- /dev/null +++ b/pkg/input/conf/init_test.go @@ -0,0 +1,51 @@ +package conf + +import ( + "context" + "os" + "testing" + + . "my/schet/pkg/testlib" + + "github.com/cucumber/godog" + "github.com/cucumber/godog/colors" + + . "github.com/onsi/gomega" +) + +func InitializeScenario(ctx *godog.ScenarioContext) { + + // ----------------------- + ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) { + beforeScenario() + return ctx, nil + }) + ctx.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) { + afterScenario() + return ctx, nil + }) + InitializeGomegaForGodog(ctx) + _ = Ω +} + +func InitializeSuite(tsc *godog.TestSuiteContext) { + tsc.BeforeSuite(beforeSuite) + tsc.AfterSuite(afterSuite) +} + +func TestMain(t *testing.T) { + var opts = godog.Options{ + Output: colors.Colored(os.Stdout), + Strict: true, + StopOnFailure: true, + TestingT: t, + } + + godog.BindCommandLineFlags("godog.", &opts) + godog.TestSuite{ + Name: "app", + TestSuiteInitializer: InitializeSuite, + ScenarioInitializer: InitializeScenario, + Options: &opts, + }.Run() +}