From c11726c929a19168bab6852998a3ca01c9441623 Mon Sep 17 00:00:00 2001 From: et-nik Date: Sun, 21 Apr 2024 14:37:44 +0200 Subject: [PATCH] update fieldalignment --- .golangci.yml | 1 + .../app/components/extendable_executor.go | 4 +- internal/app/components/safebuffer.go | 4 +- internal/app/config/config.go | 1 + internal/app/contracts/contracts.go | 2 +- internal/app/domain/errors.go | 2 +- internal/app/domain/game.go | 18 ++--- internal/app/domain/gdaemon_task.go | 25 +++---- internal/app/domain/server.go | 65 +++++++---------- internal/app/domain/server_task.go | 30 ++++---- internal/app/game_server_commands/commands.go | 5 +- .../app/game_server_commands/delete_server.go | 2 +- .../game_server_commands/install_server.go | 16 ++-- .../game_server_commands/restart_server.go | 3 +- .../app/game_server_commands/start_server.go | 9 +-- .../app/game_server_commands/status_server.go | 2 +- .../app/game_server_commands/stop_server.go | 2 +- .../app/gdaemon_scheduler/task_manager.go | 21 +++--- .../app/repositories/gdtask_repository.go | 6 +- .../app/repositories/server_repository.go | 73 ++++++++----------- .../repositories/server_task_repository.go | 4 +- internal/app/server/files/response.go | 4 +- internal/app/server/response/status.go | 4 +- internal/app/server/server.go | 24 +++--- pkg/limiter/limiter.go | 9 +-- 25 files changed, 150 insertions(+), 186 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 5459e09..64c656f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -52,6 +52,7 @@ issues: - dupl - gosec - scopelint + - govet - path: internal/app/di linters: - govet diff --git a/internal/app/components/extendable_executor.go b/internal/app/components/extendable_executor.go index f021e31..92c9d8f 100644 --- a/internal/app/components/extendable_executor.go +++ b/internal/app/components/extendable_executor.go @@ -20,9 +20,9 @@ type CommandsHandlers map[string]CommandHandler type ExtendableExecutor struct { innerExecutor contracts.Executor + handlers CommandsHandlers - mu sync.RWMutex - handlers CommandsHandlers + mu sync.RWMutex } func NewDefaultExtendableExecutor(executor contracts.Executor) *ExtendableExecutor { diff --git a/internal/app/components/safebuffer.go b/internal/app/components/safebuffer.go index 4ee2d3e..e12f352 100644 --- a/internal/app/components/safebuffer.go +++ b/internal/app/components/safebuffer.go @@ -10,11 +10,11 @@ import ( // SafeBuffer is a goroutine safe bytes.Buffer. type SafeBuffer struct { buffer bytes.Buffer - mu *sync.Mutex + mu sync.Mutex } func NewSafeBuffer() *SafeBuffer { - return &SafeBuffer{bytes.Buffer{}, &sync.Mutex{}} + return &SafeBuffer{buffer: bytes.Buffer{}} } // Write appends the contents of p to the buffer, growing the buffer as needed. It returns diff --git a/internal/app/config/config.go b/internal/app/config/config.go index b80190e..6a858a7 100644 --- a/internal/app/config/config.go +++ b/internal/app/config/config.go @@ -27,6 +27,7 @@ type SteamConfig struct { Password string `yaml:"password"` } +//nolint:govet type Config struct { NodeID uint `yaml:"ds_id"` diff --git a/internal/app/contracts/contracts.go b/internal/app/contracts/contracts.go index 21dcc45..025d505 100644 --- a/internal/app/contracts/contracts.go +++ b/internal/app/contracts/contracts.go @@ -70,9 +70,9 @@ type DomainPrimitiveValidator interface { } type ExecutorOptions struct { + Env map[string]string WorkDir string FallbackWorkDir string UID string GID string - Env map[string]string } diff --git a/internal/app/domain/errors.go b/internal/app/domain/errors.go index 9272fb4..ca8374b 100644 --- a/internal/app/domain/errors.go +++ b/internal/app/domain/errors.go @@ -6,8 +6,8 @@ import ( ) type ErrInvalidResponseFromAPI struct { - code int body []byte + code int } func NewErrInvalidResponseFromAPI(code int, response []byte) ErrInvalidResponseFromAPI { diff --git a/internal/app/domain/game.go b/internal/app/domain/game.go index 4f785d5..b1cbb9f 100644 --- a/internal/app/domain/game.go +++ b/internal/app/domain/game.go @@ -41,10 +41,10 @@ type Game struct { Name string `json:"name"` Engine string `json:"engine"` EngineVersion string `json:"engine_version"` - SteamAppID SteamAppID `json:"steam_app_id"` SteamAppSetConfig string `json:"steam_app_set_config"` RemoteRepository string `json:"remote_repository"` LocalRepository string `json:"local_repository"` + SteamAppID SteamAppID `json:"steam_app_id"` } type SteamSettings struct { @@ -92,13 +92,11 @@ func (g *GameModVarTemplate) UnmarshalJSON(bytes []byte) error { } type GameMod struct { - ID int `json:"id"` - Name string `json:"name"` - RemoteRepository string `json:"remote_repository"` - LocalRepository string `json:"local_repository"` - - Vars []GameModVarTemplate `json:"vars"` - - DefaultStartCMDLinux string `json:"default_start_cmd_linux"` - DefaultStartCMDWindows string `json:"default_start_cmd_windows"` + Name string `json:"name"` + RemoteRepository string `json:"remote_repository"` + LocalRepository string `json:"local_repository"` + DefaultStartCMDLinux string `json:"default_start_cmd_linux"` + DefaultStartCMDWindows string `json:"default_start_cmd_windows"` + Vars []GameModVarTemplate `json:"vars"` + ID int `json:"id"` } diff --git a/internal/app/domain/gdaemon_task.go b/internal/app/domain/gdaemon_task.go index b1912fd..5c32bb0 100644 --- a/internal/app/domain/gdaemon_task.go +++ b/internal/app/domain/gdaemon_task.go @@ -47,14 +47,13 @@ type GDTaskRepository interface { } type GDTask struct { - id int - runAfterID int - server *Server - task GDTaskCommand - cmd string - + server *Server statusMutex *sync.Mutex status GDTaskStatus + task GDTaskCommand + cmd string + id int + runAfterID int } func NewGDTask( @@ -66,13 +65,13 @@ func NewGDTask( status GDTaskStatus, ) *GDTask { return &GDTask{ - id, - runAfterID, - server, - task, - cmd, - &sync.Mutex{}, - status, + id: id, + runAfterID: runAfterID, + server: server, + task: task, + cmd: cmd, + statusMutex: &sync.Mutex{}, + status: status, } } diff --git a/internal/app/domain/server.go b/internal/app/domain/server.go index 7a215e1..3a6c94d 100644 --- a/internal/app/domain/server.go +++ b/internal/app/domain/server.go @@ -53,45 +53,34 @@ type Settings map[string]string //nolint:maligned type Server struct { - id int - enabled bool - installStatus InstallationStatus - blocked bool - - name string - uuid string - uuidShort string - - game Game - gameMod GameMod - - ip string - connectPort int - queryPort int - rconPort int - rconPassword string - - dir string - user string - - startCommand string - stopCommand string - forceStopCommand string - restartCommand string - - processActive bool - lastProcessCheck time.Time - - vars map[string]string - - settings Settings - - updatedAt time.Time + lastProcessCheck time.Time lastTaskCompletedAt time.Time - - changeset *hashset.Set - - mu *sync.RWMutex + updatedAt time.Time + mu *sync.RWMutex + changeset *hashset.Set + settings Settings + vars map[string]string + restartCommand string + uuid string + forceStopCommand string + uuidShort string + stopCommand string + ip string + rconPassword string + dir string + user string + startCommand string + name string + game Game + gameMod GameMod + id int + connectPort int + queryPort int + installStatus InstallationStatus + rconPort int + processActive bool + enabled bool + blocked bool } func NewServer( diff --git a/internal/app/domain/server_task.go b/internal/app/domain/server_task.go index 0f7434f..9e5bb7b 100644 --- a/internal/app/domain/server_task.go +++ b/internal/app/domain/server_task.go @@ -33,15 +33,15 @@ type ServerTaskRepository interface { } type ServerTask struct { + executeDate time.Time + server *Server + mutex *sync.Mutex + command ServerTaskCommand id int status ServerTaskStatus - command ServerTaskCommand - server *Server repeat int repeatPeriod time.Duration counter int - executeDate time.Time - mutex *sync.Mutex } func NewServerTask( @@ -54,15 +54,15 @@ func NewServerTask( executeDate time.Time, ) *ServerTask { return &ServerTask{ - id, - ServerTaskStatusWaiting, - command, - server, - repeat, - repeatPeriod, - counter, - executeDate, - &sync.Mutex{}, + id: id, + status: ServerTaskStatusWaiting, + command: command, + server: server, + repeat: repeat, + repeatPeriod: repeatPeriod, + counter: counter, + executeDate: executeDate, + mutex: &sync.Mutex{}, } } @@ -71,13 +71,13 @@ func (s ServerTask) MarshalJSON() ([]byte, error) { defer s.mutex.Unlock() return json.Marshal(struct { + ExecuteDate string `json:"execute_date"` Repeat int `json:"repeat"` RepeatPeriodInSeconds int `json:"repeat_period"` - ExecuteDate string `json:"execute_date"` }{ + ExecuteDate: s.executeDate.Format("2006-01-02 15:04:05"), Repeat: s.repeat, RepeatPeriodInSeconds: int(s.repeatPeriod.Seconds()), - ExecuteDate: s.executeDate.Format("2006-01-02 15:04:05"), }) } diff --git a/internal/app/game_server_commands/commands.go b/internal/app/game_server_commands/commands.go index 4283f6e..1e4c6e9 100644 --- a/internal/app/game_server_commands/commands.go +++ b/internal/app/game_server_commands/commands.go @@ -260,10 +260,9 @@ func (c *commandList) Execute(ctx context.Context, server *domain.Server) error } type nilCommand struct { - baseCommand bufCommand - - message string + message string + baseCommand resultCode int } diff --git a/internal/app/game_server_commands/delete_server.go b/internal/app/game_server_commands/delete_server.go index 7071096..1cb0484 100644 --- a/internal/app/game_server_commands/delete_server.go +++ b/internal/app/game_server_commands/delete_server.go @@ -15,8 +15,8 @@ import ( var errForbiddenWorkDirectoryPath = errors.New("forbidden game server work directory path") type defaultDeleteServer struct { - baseCommand bufCommand + baseCommand } func newDefaultDeleteServer( diff --git a/internal/app/game_server_commands/install_server.go b/internal/app/game_server_commands/install_server.go index c4e8294..43c4762 100644 --- a/internal/app/game_server_commands/install_server.go +++ b/internal/app/game_server_commands/install_server.go @@ -60,17 +60,15 @@ type installationRule struct { } type installServer struct { + serverRepo domain.ServerRepository + installOutput io.ReadWriter + statusCommand contracts.GameServerCommand + stopCommand contracts.GameServerCommand + startCommand contracts.GameServerCommand + installator *installator baseCommand - - installator *installator - serverRepo domain.ServerRepository - kind installatorKind - - installOutput io.ReadWriter + kind installatorKind serverWasActiveBeforeInstallation bool - statusCommand contracts.GameServerCommand - stopCommand contracts.GameServerCommand - startCommand contracts.GameServerCommand } func newUpdateServer( diff --git a/internal/app/game_server_commands/restart_server.go b/internal/app/game_server_commands/restart_server.go index 55eaf60..beaf5d2 100644 --- a/internal/app/game_server_commands/restart_server.go +++ b/internal/app/game_server_commands/restart_server.go @@ -12,12 +12,11 @@ import ( ) type defaultRestartServer struct { - baseCommand bufCommand - statusServer contracts.GameServerCommand stopServer contracts.GameServerCommand startServer contracts.GameServerCommand + baseCommand } func newDefaultRestartServer( diff --git a/internal/app/game_server_commands/start_server.go b/internal/app/game_server_commands/start_server.go index d2e7163..dea0250 100644 --- a/internal/app/game_server_commands/start_server.go +++ b/internal/app/game_server_commands/start_server.go @@ -12,13 +12,10 @@ import ( ) type defaultStartServer struct { - baseCommand - - startOutput io.ReadWriter - + startOutput io.ReadWriter + updateCommand contracts.GameServerCommand loadServerCommand LoadServerCommandFunc - - updateCommand contracts.GameServerCommand + baseCommand enableUpdatingBefore bool } diff --git a/internal/app/game_server_commands/status_server.go b/internal/app/game_server_commands/status_server.go index b46c0aa..e00ec42 100644 --- a/internal/app/game_server_commands/status_server.go +++ b/internal/app/game_server_commands/status_server.go @@ -10,8 +10,8 @@ import ( ) type statusDefaultServer struct { - baseCommand bufCommand + baseCommand } func newDefaultStatusServer( diff --git a/internal/app/game_server_commands/stop_server.go b/internal/app/game_server_commands/stop_server.go index 4f7d6d4..7848af8 100644 --- a/internal/app/game_server_commands/stop_server.go +++ b/internal/app/game_server_commands/stop_server.go @@ -10,8 +10,8 @@ import ( ) type defaultStopServer struct { - baseCommand bufCommand + baseCommand } func newDefaultStopServer( diff --git a/internal/app/gdaemon_scheduler/task_manager.go b/internal/app/gdaemon_scheduler/task_manager.go index 8e46cf9..15a5b8e 100644 --- a/internal/app/gdaemon_scheduler/task_manager.go +++ b/internal/app/gdaemon_scheduler/task_manager.go @@ -31,17 +31,15 @@ var taskServerCommandMap = map[domain.GDTaskCommand]domain.ServerCommand{ } type TaskManager struct { - config *config.Config + lastUpdated time.Time repository domain.GDTaskRepository executor contracts.Executor + cache contracts.Cache + config *config.Config serverCommandFactory *gameservercommands.ServerCommandFactory - - // Runtime - mutex *sync.Mutex - lastUpdated time.Time - commandsInProgress sync.Map // map[domain.GDTask]contracts.CommandResultReader - queue *taskQueue - cache contracts.Cache + mutex *sync.Mutex + queue *taskQueue + commandsInProgress sync.Map } func NewTaskManager( @@ -453,13 +451,12 @@ func (q *taskQueue) Len() int { } type executeCommand struct { - config *config.Config output io.ReadWriter + executor contracts.Executor + config *config.Config mu *sync.Mutex - complete bool result int - - executor contracts.Executor + complete bool } func newExecuteCommand(config *config.Config, executor contracts.Executor) *executeCommand { diff --git a/internal/app/repositories/gdtask_repository.go b/internal/app/repositories/gdtask_repository.go index 1a7d629..736498b 100644 --- a/internal/app/repositories/gdtask_repository.go +++ b/internal/app/repositories/gdtask_repository.go @@ -19,12 +19,12 @@ type GDTaskRepository struct { } type task struct { - ID int `json:"id"` - RunAfterID int `json:"run_after_id"` - Server int `json:"server_id"` Task string `json:"task"` Cmd string `json:"cmd"` Status string `json:"status"` + ID int `json:"id"` + RunAfterID int `json:"run_after_id"` + Server int `json:"server_id"` } func NewGDTaskRepository( diff --git a/internal/app/repositories/server_repository.go b/internal/app/repositories/server_repository.go index f0a0e77..3c1aaa1 100644 --- a/internal/app/repositories/server_repository.go +++ b/internal/app/repositories/server_repository.go @@ -26,13 +26,11 @@ const ( ) type ServerRepository struct { - innerRepo apiServerRepo - limitScheduler *limiter.CallScheduler - - mu sync.Mutex - servers sync.Map // [int]*domain.Server (serverID => server) - lastUpdated sync.Map // [int]time.Time (serverID => time) + innerRepo apiServerRepo + servers sync.Map + lastUpdated sync.Map + mu sync.Mutex } func NewServerRepository(ctx context.Context, client contracts.APIRequestMaker, logger *log.Logger) *ServerRepository { @@ -141,40 +139,31 @@ func (repo *ServerRepository) Save(_ context.Context, server *domain.Server) err //nolint:maligned type serverStruct struct { - ID int `json:"id"` - Enabled bool `json:"enabled"` - InstallStatus int `json:"installed"` - Blocked bool `json:"blocked"` - - Name string `json:"name"` - UUID string `json:"uuid"` - UUIDShort string `json:"uuid_short"` - - Game domain.Game `json:"game"` - GameMod domain.GameMod `json:"game_mod"` - - IP string `json:"server_ip"` - ConnectPort int `json:"server_port"` - QueryPort int `json:"query_port"` - RconPort int `json:"rcon_port"` - RconPassword string `json:"rcon"` - - Dir string `json:"dir"` - User string `json:"su_user"` - - StartCommand string `json:"start_command"` - StopCommand string `json:"stop_command"` - ForceStopCommand string `json:"force_stop_command"` - RestartCommand string `json:"restart_command"` - - ProcessActive bool `json:"process_active"` - LastProcessCheck string `json:"last_process_check"` - - Vars map[string]string `json:"vars"` - - Settings []map[string]interface{} `json:"settings"` - - UpdatedAt string `json:"updated_at"` + Vars map[string]string `json:"vars"` + ForceStopCommand string `json:"force_stop_command"` + Dir string `json:"dir"` + LastProcessCheck string `json:"last_process_check"` + Name string `json:"name"` + UUID string `json:"uuid"` + UUIDShort string `json:"uuid_short"` + RestartCommand string `json:"restart_command"` + StopCommand string `json:"stop_command"` + IP string `json:"server_ip"` + StartCommand string `json:"start_command"` + UpdatedAt string `json:"updated_at"` + RconPassword string `json:"rcon"` + User string `json:"su_user"` + Game domain.Game `json:"game"` + Settings []map[string]interface{} `json:"settings"` + GameMod domain.GameMod `json:"game_mod"` + ConnectPort int `json:"server_port"` + ID int `json:"id"` + InstallStatus int `json:"installed"` + RconPort int `json:"rcon_port"` + QueryPort int `json:"query_port"` + Enabled bool `json:"enabled"` + ProcessActive bool `json:"process_active"` + Blocked bool `json:"blocked"` } type apiServerRepo struct { @@ -367,10 +356,10 @@ func (apiRepo *apiServerRepo) FindByID(ctx context.Context, id int) (*domain.Ser } type serverSaveStruct struct { - ID int `json:"id"` - ProcessActive uint8 `json:"process_active"` InstallationStatus *int `json:"installed,omitempty"` LastProcessCheck *string `json:"last_process_check,omitempty"` + ID int `json:"id"` + ProcessActive uint8 `json:"process_active"` } func saveStructFromServer(server *domain.Server) serverSaveStruct { diff --git a/internal/app/repositories/server_task_repository.go b/internal/app/repositories/server_task_repository.go index 725e014..ef54e61 100644 --- a/internal/app/repositories/server_task_repository.go +++ b/internal/app/repositories/server_task_repository.go @@ -28,13 +28,13 @@ func NewServerTaskRepository( } type serverTask struct { - ID int `json:"id"` Command string `json:"command"` + ExecuteDate string `json:"execute_date"` + ID int `json:"id"` ServerID int `json:"server_id"` Repeat int `json:"repeat"` RepeatPeriod int `json:"repeat_period"` Counter int `json:"counter"` - ExecuteDate string `json:"execute_date"` } func (repo *ServerTaskRepository) Find(ctx context.Context) ([]*domain.ServerTask, error) { diff --git a/internal/app/server/files/response.go b/internal/app/server/files/response.go index 49e1f6d..e244d2e 100644 --- a/internal/app/server/files/response.go +++ b/internal/app/server/files/response.go @@ -58,13 +58,13 @@ func (fi fileInfoResponse) MarshalBINN() ([]byte, error) { //nolint:maligned type fileDetailsResponse struct { Name string + Mime string Size uint64 - Type uint8 ModificationTime uint64 AccessTime uint64 CreateTime uint64 Perm uint32 - Mime string + Type uint8 } func createfileDetailsResponse(path string) (*fileDetailsResponse, error) { diff --git a/internal/app/server/response/status.go b/internal/app/server/response/status.go index 71b211f..ed337fc 100644 --- a/internal/app/server/response/status.go +++ b/internal/app/server/response/status.go @@ -12,9 +12,9 @@ import ( var errUnknownBinn = errors.New("unknown binn value, cannot be presented as status") type Response struct { - Code Code - Info string Data interface{} + Info string + Code Code } type Code uint8 diff --git a/internal/app/server/server.go b/internal/app/server/server.go index e303083..e4eb1f1 100644 --- a/internal/app/server/server.go +++ b/internal/app/server/server.go @@ -25,27 +25,25 @@ import ( var errInvalidMode = errors.New("invalid server mode") type CredentialsConfig struct { - PasswordAuthentication bool Login string Password string + PasswordAuthentication bool } type Server struct { - ip string - port int - - certFile string - keyFile string - credConfig CredentialsConfig + listener net.Listener + executor contracts.Executor + taskStatsReader domain.GDTaskStatsReader - listener net.Listener - quit chan struct{} - wg sync.WaitGroup + quit chan struct{} + ip string + certFile string + keyFile string + credConfig CredentialsConfig + wg sync.WaitGroup + port int connTimeout time.Duration - - executor contracts.Executor - taskStatsReader domain.GDTaskStatsReader } type componentHandler interface { diff --git a/pkg/limiter/limiter.go b/pkg/limiter/limiter.go index 37b7201..cfef1cc 100644 --- a/pkg/limiter/limiter.go +++ b/pkg/limiter/limiter.go @@ -11,11 +11,11 @@ import ( type CallScheduler struct { q *Queue - duration time.Duration - bulkCallFromNum int singleCallFunc func(ctx context.Context, q *Queue) error bulkCallFunc func(ctx context.Context, q *Queue) error logger *log.Logger + duration time.Duration + bulkCallFromNum int } func NewAPICallScheduler( @@ -69,13 +69,12 @@ func (s *CallScheduler) Put(server *domain.Server) { type Queue struct { q []any - mutex *sync.Mutex + mutex sync.Mutex } func NewQueue() *Queue { return &Queue{ - q: make([]any, 0), - mutex: &sync.Mutex{}, + q: make([]any, 0), } }