Files
steamcache2/cmd/root.go
Justin Harms 3427b8f5bc
All checks were successful
PR Check / check-and-test (pull_request) Successful in 12s
fix: gc was being stupid allowing another thread to take the space it made before it could not anymore
2025-07-13 07:50:22 -05:00

106 lines
3.2 KiB
Go

// cmd/root.go
package cmd
import (
"os"
"runtime"
"s1d3sw1ped/SteamCache2/steamcache"
"s1d3sw1ped/SteamCache2/steamcache/logger"
"s1d3sw1ped/SteamCache2/version"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
)
var (
threads int
memory string
disk string
diskpath string
upstream string
logLevel string
logFormat string
)
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 + " starting...")
address := ":80"
if runtime.GOMAXPROCS(-1) != threads {
runtime.GOMAXPROCS(threads)
logger.Logger.Info().
Int("threads", threads).
Msg("Maximum number of threads set")
}
sc := steamcache.New(
address,
memory,
disk,
diskpath,
upstream,
)
logger.Logger.Info().
Msg("SteamCache2 " + version.Version + " started on " + address)
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().IntVarP(&threads, "threads", "t", runtime.GOMAXPROCS(-1), "Number of worker threads to use for processing requests")
rootCmd.Flags().StringVarP(&memory, "memory", "m", "0", "The size of the memory cache")
rootCmd.Flags().StringVarP(&disk, "disk", "d", "0", "The size of the disk cache")
rootCmd.Flags().StringVarP(&diskpath, "disk-path", "p", "", "The path to the disk cache")
rootCmd.Flags().StringVarP(&upstream, "upstream", "u", "", "The upstream server to proxy requests overrides the host header from the client but forwards the original host header to the upstream server")
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")
}