Skip to content

Commit

Permalink
Fixed Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
UnaTried committed Feb 2, 2025
1 parent ccc208a commit fefb851
Showing 1 changed file with 172 additions and 0 deletions.
172 changes: 172 additions & 0 deletions src/utils/path-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,175 @@ func GetExtensionPath(name string) (string, error) {

return "", errors.New("extension not found")
}
package utils

Check failure on line 173 in src/utils/path-utils.go

View workflow job for this annotation

GitHub Actions / Build

syntax error: non-declaration statement outside function body

import (

Check failure on line 175 in src/utils/path-utils.go

View workflow job for this annotation

GitHub Actions / Build

syntax error: imports must appear before other declarations
"errors"
"os"
"path/filepath"
"runtime"
"strings"
)

func MigrateConfigFolder() {
if runtime.GOOS == "windows" {
source := filepath.Join(os.Getenv("USERPROFILE"), ".spicetify")
if _, err := os.Stat(source); err == nil {
PrintBold("Migrating spicetify config folder")
destination := GetSpicetifyFolder()
err := Copy(source, destination, true, nil)
if err != nil {
Fatal(err)
}
os.RemoveAll(source)
PrintGreen("OK")
}
}
}

func ReplaceEnvVarsInString(input string) string {
var replacements []string
for _, v := range os.Environ() {
pair := strings.SplitN(v, "=", 2)
replacements = append(replacements, "$"+pair[0], pair[1])
}
replacer := strings.NewReplacer(replacements...)
return replacer.Replace(input)
}

func GetSpicetifyFolder() string {
result, isAvailable := os.LookupEnv("SPICETIFY_CONFIG")
defer func() { CheckExistAndCreate(result) }()

if isAvailable && len(result) > 0 {
return result
}

if runtime.GOOS == "windows" {
parent := os.Getenv("APPDATA")

result = filepath.Join(parent, "spicetify")
} else if runtime.GOOS == "linux" {
parent, isAvailable := os.LookupEnv("XDG_CONFIG_HOME")

if !isAvailable || len(parent) == 0 {
parent = filepath.Join(os.Getenv("HOME"), ".config")
CheckExistAndCreate(parent)
}

result = filepath.Join(parent, "spicetify")
} else if runtime.GOOS == "darwin" {
parent := filepath.Join(os.Getenv("HOME"), ".config")
CheckExistAndCreate(parent)

result = filepath.Join(parent, "spicetify")
}

return result
}

func GetStateFolder(name string) string {
result, isAvailable := os.LookupEnv("SPICETIFY_STATE")
defer func() { CheckExistAndCreate(result) }()

if isAvailable && len(result) > 0 {
return result
}

if runtime.GOOS == "windows" {
parent := os.Getenv("APPDATA")

result = filepath.Join(parent, "spicetify")
} else if runtime.GOOS == "linux" {
parent, isAvailable := os.LookupEnv("XDG_STATE_HOME")

if !isAvailable || len(parent) == 0 {
parent = filepath.Join(os.Getenv("HOME"), ".local", "state")
CheckExistAndCreate(parent)
}

result = filepath.Join(parent, "spicetify")
} else if runtime.GOOS == "darwin" {
parent := filepath.Join(os.Getenv("HOME"), ".local", "state")
CheckExistAndCreate(parent)

result = filepath.Join(parent, "spicetify")
}
return result
}

// getUserFolder checks if folder `name` is available in spicetifyFolder,
// else creates then returns the path.
func GetUserFolder(name string) string {
dir := filepath.Join(GetSpicetifyFolder(), name)
CheckExistAndCreate(dir)

return dir
}

var userAppsFolder = GetUserFolder("CustomApps")
var userExtensionsFolder = GetUserFolder("Extensions")

func GetCustomAppSubfolderPath(folderPath string) string {
entries, err := os.ReadDir(folderPath)
if err != nil {
return ""
}

for _, entry := range entries {
if entry.IsDir() {
subfolderPath := filepath.Join(folderPath, entry.Name())
indexPath := filepath.Join(subfolderPath, "index.js")

if _, err := os.Stat(indexPath); err == nil {
return subfolderPath
}

if subfolderPath := GetCustomAppSubfolderPath(subfolderPath); subfolderPath != "" {
return subfolderPath
}
}
}

return ""
}

func GetCustomAppPath(name string) (string, error) {
customAppFolderPath := filepath.Join(userAppsFolder, name)

if _, err := os.Stat(customAppFolderPath); err == nil {
customAppActualFolderPath := GetCustomAppSubfolderPath(customAppFolderPath)
if customAppActualFolderPath != "" {
return customAppActualFolderPath, nil
}
return customAppFolderPath, nil
}

customAppFolderPath = filepath.Join(GetExecutableDir(), "CustomApps", name)

if _, err := os.Stat(customAppFolderPath); err == nil {
customAppActualFolderPath := GetCustomAppSubfolderPath(customAppFolderPath)
if customAppActualFolderPath != "" {
return customAppActualFolderPath, nil
}
return customAppFolderPath, nil
}

return "", errors.New("custom app not found")
}

func GetExtensionPath(name string) (string, error) {
extFilePath := filepath.Join(userExtensionsFolder, name)

if _, err := os.Stat(extFilePath); err == nil {
return extFilePath, nil
}

extFilePath = filepath.Join(GetExecutableDir(), "Extensions", name)

if _, err := os.Stat(extFilePath); err == nil {
return extFilePath, nil
}

return "", errors.New("extension not found")
}

0 comments on commit fefb851

Please sign in to comment.