Skip to content

Commit

Permalink
rework with golang
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexDeFoc committed Aug 4, 2024
0 parents commit 2476a13
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
archive/
62 changes: 62 additions & 0 deletions Source/switch.go
Original file line number Diff line number Diff line change
@@ -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
}
44 changes: 44 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -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.

57 changes: 57 additions & 0 deletions compress.bat
Original file line number Diff line number Diff line change
@@ -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

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module switcher

go 1.22.5
74 changes: 74 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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

}

0 comments on commit 2476a13

Please sign in to comment.