From b16650ec625f86a8bdece9c59fd0bbca74697f9c Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Sun, 15 Dec 2024 09:36:33 +0300 Subject: [PATCH] style: add formatters --- Makefile | 14 +++++++++++ internal/app/app.go | 14 ++++++++--- internal/config/config.go | 14 +++++------ internal/controllers/taskcontroller/create.go | 3 ++- internal/controllers/taskcontroller/upload.go | 5 ++-- internal/middlewares/auth.go | 3 ++- internal/router/router.go | 3 +-- internal/services/reposervice/service.go | 23 +++++++++++++++---- internal/services/taskservice/service.go | 3 ++- internal/services/taskservice/upload.go | 5 ++-- 10 files changed, 63 insertions(+), 24 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1b8cc68 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +GOFUMPT := go run mvdan.cc/gofumpt@v0.7.0 +GOIMPORTS := go run golang.org/x/tools/cmd/goimports@v0.28.0 +GCI := go run github.com/daixiang0/gci@v0.13.5 +GOLINES := go run github.com/segmentio/golines@v0.12.2 + +format: + @echo "🛠️ Format code" + $(GOIMPORTS) -w . + $(GCI) write -s standard -s default -s "prefix(code.alt-gnome.ru/aides-infra/aides-repo-api)" . + $(GOLINES) -w . + $(GOFUMPT) -w . + @echo "✅ Format done." + +.PHONY: format \ No newline at end of file diff --git a/internal/app/app.go b/internal/app/app.go index 5b21c59..63ac4d1 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -5,6 +5,9 @@ import ( "log" "net/http" + "gorm.io/driver/postgres" + "gorm.io/gorm" + "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/controllers/taskcontroller" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models" @@ -12,8 +15,6 @@ import ( "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/cronservice" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/reposervice" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice" - "gorm.io/driver/postgres" - "gorm.io/gorm" ) type App struct { @@ -29,7 +30,14 @@ type App struct { } func createDBConnection(cfg *config.Config) (*gorm.DB, error) { - dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", cfg.DBHost, cfg.DBPort, cfg.DBUser, cfg.DBPassword, cfg.DBName) + dsn := fmt.Sprintf( + "host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", + cfg.DBHost, + cfg.DBPort, + cfg.DBUser, + cfg.DBPassword, + cfg.DBName, + ) return gorm.Open(postgres.Open(dsn), &gorm.Config{}) } diff --git a/internal/config/config.go b/internal/config/config.go index 1d30590..cc0cf7b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -8,15 +8,15 @@ import ( type Config struct { Token string `env:"TOKEN"` - UploadDir string `env:"UPLOAD_DIR" envDefault:"./uploads"` - Port string `env:"PORT" envDefault:"8080"` - MaxSizeUpload int64 `env:"MAX_SIZE_UPLOAD" envDefault:"104857600"` //100 MB + UploadDir string `env:"UPLOAD_DIR" envDefault:"./uploads"` + Port string `env:"PORT" envDefault:"8080"` + MaxSizeUpload int64 `env:"MAX_SIZE_UPLOAD" envDefault:"104857600"` // 100 MB - DBHost string `env:"DB_HOST" envDefault:"localhost"` - DBPort string `env:"DB_PORT" envDefault:"5432"` - DBUser string `env:"DB_USER" envDefault:"user"` + DBHost string `env:"DB_HOST" envDefault:"localhost"` + DBPort string `env:"DB_PORT" envDefault:"5432"` + DBUser string `env:"DB_USER" envDefault:"user"` DBPassword string `env:"DB_PASSWORD" envDefault:"password"` - DBName string `env:"DB_NAME" envDefault:"app_db"` + DBName string `env:"DB_NAME" envDefault:"app_db"` } func New() *Config { diff --git a/internal/controllers/taskcontroller/create.go b/internal/controllers/taskcontroller/create.go index 72cb296..d58efaf 100644 --- a/internal/controllers/taskcontroller/create.go +++ b/internal/controllers/taskcontroller/create.go @@ -4,9 +4,10 @@ import ( "encoding/json" "net/http" + "github.com/go-chi/render" + "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/common/errors" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models" - "github.com/go-chi/render" ) type CreateTaskDTO struct { diff --git a/internal/controllers/taskcontroller/upload.go b/internal/controllers/taskcontroller/upload.go index d60e507..d6c629d 100644 --- a/internal/controllers/taskcontroller/upload.go +++ b/internal/controllers/taskcontroller/upload.go @@ -4,10 +4,11 @@ import ( "fmt" "net/http" - "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/common/errors" - "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice" "github.com/go-chi/chi/v5" "github.com/go-chi/render" + + "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/common/errors" + "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice" ) type TaskUploadResponse struct { diff --git a/internal/middlewares/auth.go b/internal/middlewares/auth.go index 06730a7..251885a 100644 --- a/internal/middlewares/auth.go +++ b/internal/middlewares/auth.go @@ -3,9 +3,10 @@ package middlewares import ( "net/http" + "github.com/go-chi/render" + "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/common/errors" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config" - "github.com/go-chi/render" ) func CreateAuthGuard(cfg *config.Config) func(next http.Handler) http.Handler { diff --git a/internal/router/router.go b/internal/router/router.go index 555e5fc..b85b9e5 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -5,11 +5,10 @@ import ( "github.com/go-chi/chi/v5/middleware" httpSwagger "github.com/swaggo/http-swagger" + _ "code.alt-gnome.ru/aides-infra/aides-repo-api/docs" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/controllers/taskcontroller" "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/middlewares" - - _ "code.alt-gnome.ru/aides-infra/aides-repo-api/docs" ) type Router struct { diff --git a/internal/services/reposervice/service.go b/internal/services/reposervice/service.go index b498392..959da77 100644 --- a/internal/services/reposervice/service.go +++ b/internal/services/reposervice/service.go @@ -7,8 +7,9 @@ import ( "path" "strconv" - "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models" "gorm.io/gorm" + + "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models" ) type Config interface { @@ -42,7 +43,14 @@ func createSymlink(target, link string) error { } func runGenbasedir(repoDir, arch, repoName string) { - cmd := exec.Command("genbasedir", "--bloat", "--progress", fmt.Sprintf("--topdir=%s", repoDir), arch, repoName) + cmd := exec.Command( + "genbasedir", + "--bloat", + "--progress", + fmt.Sprintf("--topdir=%s", repoDir), + arch, + repoName, + ) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err := cmd.Run() @@ -114,7 +122,14 @@ func (s *Service) ForceUpdate() { localFilePath := path.Join( strconv.FormatUint(uint64(el.ID), 10), fileInfo.Name, ) - symLink := path.Join(s.config.GetUploadDir(), "future_repo", "Sisyphus", fileInfo.Arch, "RPMS.aides", fileInfo.Name) + symLink := path.Join( + s.config.GetUploadDir(), + "future_repo", + "Sisyphus", + fileInfo.Arch, + "RPMS.aides", + fileInfo.Name, + ) targetPath := path.Join("../../../../tasks/", localFilePath) createSymlink(targetPath, symLink) @@ -146,7 +161,6 @@ func (s *Service) ForceUpdate() { if err := os.Rename(bPath, cPath); err != nil { } } else if !os.IsNotExist(err) { - } if err := os.Rename(aPath, bPath); err != nil { @@ -154,7 +168,6 @@ func (s *Service) ForceUpdate() { } if err := os.RemoveAll(cPath); err != nil { - } os.RemoveAll(path.Join(s.config.GetUploadDir(), "future_repo")) diff --git a/internal/services/taskservice/service.go b/internal/services/taskservice/service.go index 9f4a916..60997b5 100644 --- a/internal/services/taskservice/service.go +++ b/internal/services/taskservice/service.go @@ -5,8 +5,9 @@ import ( "path" "strconv" - "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models" "gorm.io/gorm" + + "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models" ) type Config interface { diff --git a/internal/services/taskservice/upload.go b/internal/services/taskservice/upload.go index 3263bff..ec28927 100644 --- a/internal/services/taskservice/upload.go +++ b/internal/services/taskservice/upload.go @@ -11,8 +11,9 @@ import ( "strconv" "strings" - "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models" "gorm.io/gorm" + + "code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models" ) type TaskUploadInput struct { @@ -73,7 +74,7 @@ func (s *Service) Upload(input *TaskUploadInput) error { // Полный путь для файла filePath := path.Join(taskFolderPath, fileHeader.Filename) - //Удаляем файл если такой уже существует + // Удаляем файл если такой уже существует if _, err := os.Stat(filePath); err == nil { err = os.Remove(filePath) if err != nil {