From 2476a1301d10a459fe01b5ca6ad80656e21bb19d Mon Sep 17 00:00:00 2001 From: AlexDeFoc Date: Sun, 4 Aug 2024 15:01:24 +0300 Subject: [PATCH] rework with golang --- .gitignore | 1 + Source/switch.go | 62 ++++++++++++++++++++++++++++++++++++++++ build.bat | 44 ++++++++++++++++++++++++++++ compress.bat | 57 +++++++++++++++++++++++++++++++++++++ go.mod | 3 ++ main.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 241 insertions(+) create mode 100644 .gitignore create mode 100644 Source/switch.go create mode 100644 build.bat create mode 100644 compress.bat create mode 100644 go.mod create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a012ec2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +archive/ diff --git a/Source/switch.go b/Source/switch.go new file mode 100644 index 0000000..3746ad5 --- /dev/null +++ b/Source/switch.go @@ -0,0 +1,62 @@ +package main + +import ( + "io" + "log" + "os" + "path/filepath" + "runtime" +) + +func main(){ + err := copy("../prefs", getPrefsPath()) + if err != nil { + log.Panic(err) + } +} + +// getPrefsPath returns the path to the prefs file based on the OS. +func getPrefsPath() string { + var prefsPath string + + switch runtime.GOOS { + case "windows": + // On Windows, use the USERPROFILE environment variable and the typical Spotify path + userProfile := os.Getenv("USERPROFILE") + if userProfile != "" { + prefsPath = filepath.Join(userProfile, "AppData", "Roaming", "Spotify", "prefs") + } + case "linux": + // On Linux, use the typical Spotify config directory + prefsPath = filepath.Join(os.Getenv("HOME"), ".config", "spotify", "prefs") + case "darwin": + // On macOS, use the typical Spotify Application Support directory + prefsPath = filepath.Join(os.Getenv("HOME"), "Library", "Application Support", "Spotify", "prefs") + default: + return "Unsupported operating system." + } + + return prefsPath +} + +func copy(srcpath, dstpath string) (err error) { + r, err := os.Open(srcpath) + if err != nil { + return err + } + defer r.Close() + + w, err := os.Create(dstpath) + if err != nil { + return nil + } + + defer func() { + if c := w.Close(); err == nil { + err = c + } + }() + + _, err = io.Copy(w, r) + return err +} diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..a09944e --- /dev/null +++ b/build.bat @@ -0,0 +1,44 @@ +@echo off +cls +setlocal enabledelayedexpansion +:: Define the targets for all relevant platforms +set targets=linux/amd64 linux/386 linux/arm linux/arm64 darwin/amd64 darwin/arm64 windows/amd64 windows/386 windows/arm windows/arm64 +set sources=main.go Source/switch.go + +:: Copy the config file to each platform's Source folder +for %%t in (%targets%) do ( + for /f "tokens=1,2 delims=/" %%a in ("%%t") do ( + set GOOS=%%a + set GOARCH=%%b + + :: Create platform directories if they don't exist + if not exist "export/%%a-%%b" mkdir "export/%%a-%%b" + if not exist "Source/%%a-%%b/Source" mkdir "Source/%%a-%%b/Source" + + :: Copy the config file to the Source folder + :: copy "config" "main/%%a-%%b\config" + + :: Set output extension for Windows + set output_ext= + if "%%a"=="windows" ( + set output_ext=.exe + ) + + :: Build main.go + echo Building main.go for %%a/%%b... + set GOOS=%%a + set GOARCH=%%b + go build -o "export/%%a-%%b/main!output_ext!" main.go + + :: Build switch.go in Source folder + echo Building switch.go in Source folder for %%a/%%b... + pushd Source + set GOOS=%%a + set GOARCH=%%b + go build -o "..\Source\%%a-%%b\Source\switch!output_ext!" switch.go + popd + ) +) + +echo Build process completed. + diff --git a/compress.bat b/compress.bat new file mode 100644 index 0000000..46718c3 --- /dev/null +++ b/compress.bat @@ -0,0 +1,57 @@ +@echo off +setlocal + +:: Define the path to 7z.exe +set "sevenZipPath=C:\App\7-Zip\7z.exe" + +:: Define the base directory +set "baseDir=%cd%\export" +set "archiveDir=%cd%\archive" + +:: Create the archive directory if it does not exist +if not exist "%archiveDir%" ( + mkdir "%archiveDir%" +) + +:: Loop through each subdirectory in the base directory +for /d %%D in ("%baseDir%\*") do ( + :: Get the name of the current subdirectory + set "dirName=%%~nxD" + setlocal enabledelayedexpansion + + :: Define archive names based on the directory and set the path for archives + set "tarArchive=%archiveDir%\!dirName!.tar" + set "xzArchive=%archiveDir%\!dirName!.tar.xz" + + :: Create the tar archive of the contents of the subdirectory + echo Creating TAR archive for !dirName!... + "%sevenZipPath%" a -ttar "!tarArchive!" "%%D\*" + + :: Check if the TAR archive was created successfully + if errorlevel 1 ( + echo Failed to create TAR archive for !dirName!. + endlocal + continue + ) + + :: Compress the TAR archive into a XZ archive with maximum compression level + echo Compressing TAR to XZ with maximum compression for !dirName!... + "%sevenZipPath%" a -txz -mx=9 "!xzArchive!" "!tarArchive!" + + :: Check if the XZ archive was created successfully + if errorlevel 1 ( + echo Failed to create XZ archive for !dirName!. + endlocal + continue + ) + + :: Clean up the intermediate TAR file + del "!tarArchive!" + + echo Archive "!xzArchive!" created successfully for !dirName!. + endlocal +) + +endlocal +pause + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4cf0cf0 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module switcher + +go 1.22.5 diff --git a/main.go b/main.go new file mode 100644 index 0000000..1cfe7a7 --- /dev/null +++ b/main.go @@ -0,0 +1,74 @@ +package main + +import ( + "io" + "log" + "os" + "path/filepath" + "runtime" +) + +func main(){ + //Make directory + errFolder := os.Mkdir("New User", 0777) + if errFolder != nil { + log.Println(errFolder) + } + errSwitcher := copy("Source/switch.exe", "New User/switch.exe") + if errSwitcher != nil { + log.Println(errSwitcher) + } + + //Copy prefs into new folder + err := copy(getPrefsPath(), "New User/prefs") + if err != nil { + log.Panic(err) + } +} + +// getPrefsPath returns the path to the prefs file based on the OS. +func getPrefsPath() string { + var prefsPath string + + switch runtime.GOOS { + case "windows": + // On Windows, use the USERPROFILE environment variable and the typical Spotify path + userProfile := os.Getenv("USERPROFILE") + if userProfile != "" { + prefsPath = filepath.Join(userProfile, "AppData", "Roaming", "Spotify", "prefs") + } + case "linux": + // On Linux, use the typical Spotify config directory + prefsPath = filepath.Join(os.Getenv("HOME"), ".config", "spotify", "prefs") + case "darwin": + // On macOS, use the typical Spotify Application Support directory + prefsPath = filepath.Join(os.Getenv("HOME"), "Library", "Application Support", "Spotify", "prefs") + default: + return "Unsupported operating system." + } + + return prefsPath +} + +func copy(srcpath, dstpath string) (err error) { + r, err := os.Open(srcpath) + if err != nil { + return err + } + defer r.Close() + + w, err := os.Create(dstpath) + if err != nil { + return nil + } + + defer func() { + if c := w.Close(); err == nil { + err = c + } + }() + + _, err = io.Copy(w, r) + return err + +}