Добален модуль конфига и добавление из него доходов
Этот коммит содержится в:
родитель
60be248897
коммит
b3c7cb1099
8 изменённых файлов: 180 добавлений и 0 удалений
|
@ -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)
|
||||
}
|
||||
|
|
1
go.mod
1
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
|
||||
|
|
2
go.sum
2
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=
|
||||
|
|
48
pkg/input/conf/conf.go
Обычный файл
48
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
|
||||
}
|
31
pkg/input/conf/conf_test.go
Обычный файл
31
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() {
|
||||
}
|
||||
|
||||
// -----------------------
|
15
pkg/input/conf/config.go
Обычный файл
15
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{}
|
||||
}
|
3
pkg/input/conf/features/app.feature
Обычный файл
3
pkg/input/conf/features/app.feature
Обычный файл
|
@ -0,0 +1,3 @@
|
|||
# Во имя Бога Милостивого, Милосердного!!!
|
||||
# language: ru
|
||||
Функциональность: Конфиг
|
51
pkg/input/conf/init_test.go
Обычный файл
51
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()
|
||||
}
|
Загрузка…
Создание таблицы
Сослаться в новой задаче