The provided repository contains a single main.go file with all the code. To align with best Golang practices, I suggest the following refactoring changes and a new file structure:
File Structure:
go Copy code
selenium-grid-cleaner/
cmd/
selenium-cleaner/
main.go
internal/
config/
config.go
loader.go
errors/
errors.go
grid/
cleaner.go
status.go
session.go
pod.go
[Redacted Person]/
logger.go
go.mod
go.sum
Refactoring Changes:
Extract configuration loading into a separate package:
Move the LoadConfig function to internal/config/loader.go.
Create a Config struct in internal/config/config.go to hold the configuration.
Use the encoding/json package to unmarshal the configuration from the file.
internal/config/loader.go:
func LoadConfig(path string) (Config, error) {
// ...
}
code Copy code
`internal/config/config.go`:
```go
type Config struct {
GridHost string `json:"gridHost"`
GridPort string `json:"gridPort"`
Namespace string `json:"namespace"`
MaxAgeSecs int64 `json:"maxAgeSecs"`
MaxRetries int `json:"maxRetries"`
MaxParallel int `json:"maxParallel"`
LogLevel string `json:"logLevel"`
LogFormat string `json:"logFormat"`
}
Create a separate package for error handling:
Move error-related functions and variables to internal/errors/errors.go.
Create a custom error type GridError to handle grid-related errors.
internal/errors/errors.go:
type GridError struct {
err error
}
func (e *GridError) Error() string {
return e.err.Error()
}
func NewGridError(err error) *GridError {
return &GridError{err: err}
}
code Copy code
3. **Extract grid-related functionality into a separate package:**
* Move grid-related functions and structs to `internal/grid/`.
* Create a `Cleaner` struct in `internal/grid/cleaner.go` to handle the grid cleaning process.
* Use the `sync` package to synchronize access to the `Cleaner` instance.
`internal/grid/cleaner.go`:
```go
type Cleaner struct {
config Config
client *http.Client
logger *logrus.Logger
errors []error
mutex sync.Mutex
}
func NewCleaner(config Config) (*Cleaner, error) {
// ...
}
func (gc *Cleaner) CheckAndCleanup(ctx context.Context) error {
// ...
}
Extract session-related functionality into a separate file:
Move session-related functions and structs to internal/grid/session.go.
Create a Session struct to hold session information.
internal/grid/session.go:
type Session struct {
NodeIP string
StartTime time.Time
SessionID string
PodName string
SlotState string
}
func (gc *Cleaner) parseSessionInfo(status *GridStatus) ([]Session, error) {
// ...
}
code Copy code
5. **Extract pod-related functionality into a separate file:**
* Move pod-related functions to `internal/grid/pod.go`.
* Create a `Pod` struct to hold pod information.
`internal/grid/pod.go`:
```go
func (gc *Cleaner) getPodName(nodeIP string) (string, error) {
// ...
}
func (gc *Cleaner) deletePod(ctx context.Context, podName string) error {
// ...
}
Extract logger creation into a separate package:
Move logger creation to internal/logger/logger.go.
Create a Logger struct to hold the logger instance.
internal/logger/logger.go:
func NewLogger(config Config) (*logrus.Logger, error) {
// ...
}
code Copy code
7. **Update the `main.go` file:**
* Import the necessary packages.
* Create a `main` function to run the grid cleaner.
`cmd/selenium-cleaner/main.go`:
```go
package main
import (
"context"
"fmt"
"log"
"github.com/sirupsen/logrus"
"selenium-grid-cleaner/internal/config"
"selenium-grid-cleaner/internal/grid"
"selenium-grid-cleaner/internal/logger"
)
func main() {
// Load configuration
cfg, err := config.LoadConfig("config.yaml")
if err != nil {
log.Fatal(err)
}
// Create logger
logger, err := logger.NewLogger(cfg)
if err != nil {
log.Fatal(err)
}
// Create grid cleaner
cleaner, err := grid.NewCleaner(cfg)
if err != nil {
logger.Fatal(err)
}
// Run grid cleaner
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
if err := cleaner.CheckAndCleanup(ctx); err != nil {
logger.Fatal(err)
}
}
Benefits of the new structure:
Separation of concerns: Each package and file has a single responsibility, making the code easier to understand and maintain.
Reusability: The extracted packages and functions can be reused in other parts of the project or even in other projects.
Readability: The code is more organized, and the file structure is easier to navigate.
Testability: The extracted packages and functions are easier to test independently.
Scalability: The new structure allows for easier addition of new features and functionality.