Files
steamcache2/cmd/root.go
Justin Harms bd123bc63a
All checks were successful
Release Tag / release (push) Successful in 9s
Refactor module naming and update references to steamcache2
- Changed module name from `s1d3sw1ped/SteamCache2` to `s1d3sw1ped/steamcache2` for consistency.
- Updated all import paths and references throughout the codebase to reflect the new module name.
- Adjusted README and Makefile to use the updated module name, ensuring clarity in usage instructions.
2025-09-21 23:10:21 -05:00

151 lines
4.9 KiB
Go

// cmd/root.go
package cmd
import (
"fmt"
"os"
"s1d3sw1ped/steamcache2/config"
"s1d3sw1ped/steamcache2/steamcache"
"s1d3sw1ped/steamcache2/steamcache/logger"
"s1d3sw1ped/steamcache2/version"
"strings"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
)
var (
configPath string
logLevel string
logFormat string
maxConcurrentRequests int64
maxRequestsPerClient int64
)
var rootCmd = &cobra.Command{
Use: "steamcache2",
Short: "steamcache2 is a caching solution for Steam game updates and installations",
Long: `steamcache2 is a caching solution designed to optimize the delivery of Steam game updates and installations.
It reduces bandwidth usage and speeds up the download process by caching game files locally.
This tool is particularly useful for environments with multiple Steam users, such as gaming cafes or households with multiple gamers.
By caching game files, SteamCache2 ensures that subsequent downloads of the same files are served from the local cache,
significantly improving download times and reducing the load on the internet connection.`,
Run: func(cmd *cobra.Command, args []string) {
// Configure logging
switch logLevel {
case "debug":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "error":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case "info":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel) // Default to info level if not specified
}
var writer zerolog.ConsoleWriter
if logFormat == "json" {
writer = zerolog.ConsoleWriter{Out: os.Stderr, NoColor: true}
} else {
writer = zerolog.ConsoleWriter{Out: os.Stderr}
}
logger.Logger = zerolog.New(writer).With().Timestamp().Logger()
logger.Logger.Info().
Msg("steamcache2 " + version.Version + " " + version.Date + " starting...")
// Load configuration
cfg, err := config.LoadConfig(configPath)
if err != nil {
// Check if the error is because the config file doesn't exist
// The error is wrapped, so we check the error message
if strings.Contains(err.Error(), "no such file") ||
strings.Contains(err.Error(), "cannot find the file") ||
strings.Contains(err.Error(), "The system cannot find the file") {
logger.Logger.Info().
Str("config_path", configPath).
Msg("Config file not found, creating default configuration")
if err := config.SaveDefaultConfig(configPath); err != nil {
logger.Logger.Error().
Err(err).
Str("config_path", configPath).
Msg("Failed to create default configuration")
fmt.Fprintf(os.Stderr, "Error: Failed to create default config at %s: %v\n", configPath, err)
os.Exit(1)
}
logger.Logger.Info().
Str("config_path", configPath).
Msg("Default configuration created successfully. Please edit the file and run again.")
fmt.Printf("Default configuration created at %s\n", configPath)
fmt.Println("Please edit the configuration file as needed and run the application again.")
os.Exit(0)
} else {
logger.Logger.Error().
Err(err).
Str("config_path", configPath).
Msg("Failed to load configuration")
fmt.Fprintf(os.Stderr, "Error: Failed to load configuration from %s: %v\n", configPath, err)
os.Exit(1)
}
}
logger.Logger.Info().
Str("config_path", configPath).
Msg("Configuration loaded successfully")
// Use command-line flags if provided, otherwise use config values
finalMaxConcurrentRequests := cfg.MaxConcurrentRequests
if maxConcurrentRequests > 0 {
finalMaxConcurrentRequests = maxConcurrentRequests
}
finalMaxRequestsPerClient := cfg.MaxRequestsPerClient
if maxRequestsPerClient > 0 {
finalMaxRequestsPerClient = maxRequestsPerClient
}
sc := steamcache.New(
cfg.ListenAddress,
cfg.Cache.Memory.Size,
cfg.Cache.Disk.Size,
cfg.Cache.Disk.Path,
cfg.Upstream,
cfg.Cache.Memory.GCAlgorithm,
cfg.Cache.Disk.GCAlgorithm,
finalMaxConcurrentRequests,
finalMaxRequestsPerClient,
)
logger.Logger.Info().
Msg("steamcache2 " + version.Version + " started on " + cfg.ListenAddress)
sc.Run()
logger.Logger.Info().Msg("steamcache2 stopped")
os.Exit(0)
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
rootCmd.Flags().StringVarP(&configPath, "config", "c", "config.yaml", "Path to configuration file")
rootCmd.Flags().StringVarP(&logLevel, "log-level", "l", "info", "Logging level: debug, info, error")
rootCmd.Flags().StringVarP(&logFormat, "log-format", "f", "console", "Logging format: json, console")
rootCmd.Flags().Int64Var(&maxConcurrentRequests, "max-concurrent-requests", 0, "Maximum concurrent requests (0 = use config file value)")
rootCmd.Flags().Int64Var(&maxRequestsPerClient, "max-requests-per-client", 0, "Maximum concurrent requests per client IP (0 = use config file value)")
}