diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..33b3db1 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,9 @@ +.env +.git/ +docker-compose.yml +Dockerfile +.dockerignore +tmp/ +.air.toml +aparserserver/ +.vscode \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8fae8bb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,25 @@ +FROM registry.home.4it.me/golang:1.21 as builder + +LABEL maintainer="dilap54 " + +WORKDIR /app + +COPY go.mod go.sum ./ + +RUN go mod download + +COPY . . + +RUN make build + +FROM alpine:latest + +RUN apk --no-cache add ca-certificates + +WORKDIR /app/ + +COPY migrations ./migrations +COPY ./categories.json ./ +COPY --from=builder /app/cli ./ + +ENTRYPOINT [ "./cli" ] \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..57007c1 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +#!/bin/bash + +docker-build: + docker build . -t registry.home.4it.me/dilap54/platiparser + +docker-push: + docker push registry.home.4it.me/dilap54/platiparser + +build: + CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o cli ./cmd/cli/ diff --git a/cmd/plati/plati.go b/cmd/cli/plati.go similarity index 64% rename from cmd/plati/plati.go rename to cmd/cli/plati.go index ec0bc4b..2e88096 100644 --- a/cmd/plati/plati.go +++ b/cmd/cli/plati.go @@ -11,40 +11,45 @@ import ( "gitea.home.4it.me/dilap54/platiparser/gorm" "gitea.home.4it.me/dilap54/platiparser/plati" - "github.com/joho/godotenv" uuid "github.com/satori/go.uuid" + "github.com/urfave/cli/v2" ) func init() { - godotenv.Load(".env") + commands = append(commands, platiCommand) } -func main() { - categories := openCategories("./categories.json").Content - categories = filterBySubstring("Gift", categories) +var platiCommand = &cli.Command{ + Name: "plati", + Action: func(c *cli.Context) error { + categories := openCategories("./categories.json").Content + categories = filterBySubstring("Gift", categories) - ctx := context.Background() + ctx := context.Background() - db := gorm.GetDB() - platiCli := plati.New() + db := gorm.GetDB() + platiCli := plati.New() - // printNames(categories) + // printNames(categories) - for i, c := range categories { - log.Printf("fetching goods [%d/%d] for %s\n", i, len(categories), c.FlatName) - goods, err := platiCli.GetBlockGoodsCategory(ctx, c.ID, c.ParentID, "cntSellDESC", 1, 100, "RUR", "ru-RU") - if err != nil { - log.Fatal(err) + for i, c := range categories { + log.Printf("fetching goods [%d/%d] for %s\n", i, len(categories), c.FlatName) + goods, err := platiCli.GetBlockGoodsCategory(ctx, c.ID, c.ParentID, "cntSellDESC", 1, 100, "RUR", "ru-RU") + if err != nil { + return fmt.Errorf("getblockgoodscategory: %w", err) + } + if len(goods) == 0 { + continue + } + gormGoods := convertGoodsToGorm(c, goods) + if err := db.Create(gormGoods).Error; err != nil { + return fmt.Errorf("db Create: %w", err) + } + json.NewEncoder(os.Stdout).Encode(goods) } - if len(goods) == 0 { - continue - } - gormGoods := convertGoodsToGorm(c, goods) - if err := db.Create(gormGoods).Error; err != nil { - log.Fatal(err) - } - json.NewEncoder(os.Stdout).Encode(goods) - } + + return nil + }, } func convertGoodsToGorm(cat *plati.Category, goods []*plati.Good) []*gorm.Good {