Skip to content

Commit

Permalink
Fix panic on windows due to uber-go/zap#621
Browse files Browse the repository at this point in the history
  • Loading branch information
giwty authored and giwty committed Jul 29, 2020
1 parent 0ea3fcb commit 90be744
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
24 changes: 18 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"github.com/giwty/switch-library-manager/settings"
"github.com/giwty/switch-library-manager/ui"
"go.uber.org/zap"
"net/url"
"os"
"path"
"path/filepath"
"runtime"
)

func main() {
Expand All @@ -23,10 +24,10 @@ func main() {
fmt.Println("failed to get working directory, please ensure app has sufficient permissions. aborting")
}

webResourcesPath := path.Join(workingFolder, "web")
webResourcesPath := filepath.Join(workingFolder, "web")
if _, err := os.Stat(webResourcesPath); err != nil {
workingFolder = filepath.Dir(exePath)
webResourcesPath = path.Join(workingFolder, "web")
webResourcesPath = filepath.Join(workingFolder, "web")
if _, err := os.Stat(webResourcesPath); err != nil {
fmt.Println("Missing web folder, please re-download latest release, and extract all files. aborting", err)
return
Expand Down Expand Up @@ -60,14 +61,25 @@ func createLogger(workingFolder string, debug bool) *zap.Logger {
config = zap.NewDevelopmentConfig()
config.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
}
logPath := path.Join(workingFolder, "slm.log")

logPath := filepath.Join(workingFolder, "slm.log")
// delete old file
os.Remove(logPath)

if runtime.GOOS == "windows" {
zap.RegisterSink("winfile", func(u *url.URL) (zap.Sink, error) {
// Remove leading slash left by url.Parse()
return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
})
logPath = "winfile:///" + logPath
}

config.OutputPaths = []string{logPath}
config.ErrorOutputPaths = []string{logPath}
logger, _ := config.Build()
logger, err := config.Build()
if err != nil {
fmt.Printf("failed to create logger - %v", err)
panic(1)
}
zap.ReplaceGlobals(logger)
return logger
}
4 changes: 2 additions & 2 deletions settings/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package settings
import (
"errors"
"github.com/magiconair/properties"
"path"
"path/filepath"
)

var (
Expand All @@ -25,7 +25,7 @@ func SwitchKeys() (*switchKeys, error) {
func InitSwitchKeys(baseFolder string) (*switchKeys, error) {

// init from a file
p, err := properties.LoadFile(path.Join(baseFolder, "prod.keys"), properties.UTF8)
p, err := properties.LoadFile(filepath.Join(baseFolder, "prod.keys"), properties.UTF8)
if err != nil {
p, err = properties.LoadFile("${HOME}/.switch/prod.keys", properties.UTF8)
}
Expand Down
14 changes: 7 additions & 7 deletions settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
)

var (
Expand Down Expand Up @@ -56,10 +56,10 @@ type AppSettings struct {
}

func ReadSettingsAsJSON(baseFolder string) string {
if _, err := os.Stat(path.Join(baseFolder, SETTINGS_FILENAME)); err != nil {
if _, err := os.Stat(filepath.Join(baseFolder, SETTINGS_FILENAME)); err != nil {
saveDefaultSettings(baseFolder)
}
file, _ := os.Open(path.Join(baseFolder, SETTINGS_FILENAME))
file, _ := os.Open(filepath.Join(baseFolder, SETTINGS_FILENAME))
bytes, _ := ioutil.ReadAll(file)
return string(bytes)
}
Expand All @@ -69,8 +69,8 @@ func ReadSettings(baseFolder string) *AppSettings {
return settingsInstance
}
settingsInstance = &AppSettings{Debug: false, GuiPagingSize: 100}
if _, err := os.Stat(path.Join(baseFolder, SETTINGS_FILENAME)); err == nil {
file, err := os.Open(path.Join(baseFolder, SETTINGS_FILENAME))
if _, err := os.Stat(filepath.Join(baseFolder, SETTINGS_FILENAME)); err == nil {
file, err := os.Open(filepath.Join(baseFolder, SETTINGS_FILENAME))
if err != nil {
zap.S().Warnf("Missing or corrupted config file, creating a new one")
return saveDefaultSettings(baseFolder)
Expand Down Expand Up @@ -109,13 +109,13 @@ func saveDefaultSettings(baseFolder string) *AppSettings {

func SaveSettings(settings *AppSettings, baseFolder string) *AppSettings {
file, _ := json.MarshalIndent(settings, "", " ")
_ = ioutil.WriteFile(path.Join(baseFolder, SETTINGS_FILENAME), file, 0644)
_ = ioutil.WriteFile(filepath.Join(baseFolder, SETTINGS_FILENAME), file, 0644)
settingsInstance = settings
return settings
}

func CheckForUpdates(workingFolder string) (bool, error) {
file, err := os.Open(path.Join(workingFolder, SLM_VERSION_FILE))
file, err := os.Open(filepath.Join(workingFolder, SLM_VERSION_FILE))
if err != nil {
return false, err
}
Expand Down
9 changes: 4 additions & 5 deletions ui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"go.uber.org/zap"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"sync"
Expand Down Expand Up @@ -57,7 +56,7 @@ func CreateGUI(baseFolder string, sugarLogger *zap.SugaredLogger) *GUI {
}
func (g *GUI) Start() {

webResourcesPath := path.Join(g.baseFolder, "web")
webResourcesPath := filepath.Join(g.baseFolder, "web")
if _, err := os.Stat(webResourcesPath); err != nil {
g.sugarLogger.Error("Missing web folder, please re-download latest release, and extract all files. aborting")
return
Expand Down Expand Up @@ -157,7 +156,7 @@ func (g *GUI) Start() {
Icon: title.Attributes.IconUrl,
Name: title.Attributes.Name,
TitleId: v.File.Metadata.TitleId,
Path: path.Join(v.File.BaseFolder, v.File.Info.Name()),
Path: filepath.Join(v.File.BaseFolder, v.File.Info.Name()),
})
} else {
response = append(response,
Expand Down Expand Up @@ -247,15 +246,15 @@ func (g *GUI) buildSwitchDb() (*db.SwitchTitlesDB, error) {
settingsObj := settings.ReadSettings(g.baseFolder)
//1. load the titles JSON object
g.UpdateProgress(1, 4, "Downloading titles.json")
filename := path.Join(g.baseFolder, settings.TITLE_JSON_FILENAME)
filename := filepath.Join(g.baseFolder, settings.TITLE_JSON_FILENAME)
titleFile, titlesEtag, err := db.LoadAndUpdateFile(settings.TITLES_JSON_URL, filename, settingsObj.TitlesEtag)
if err != nil {
return nil, err
}
settingsObj.TitlesEtag = titlesEtag

g.UpdateProgress(2, 4, "Downloading versions.json")
filename = path.Join(g.baseFolder, settings.VERSIONS_JSON_FILENAME)
filename = filepath.Join(g.baseFolder, settings.VERSIONS_JSON_FILENAME)
versionsFile, versionsEtag, err := db.LoadAndUpdateFile(settings.VERSIONS_JSON_URL, filename, settingsObj.VersionsEtag)
if err != nil {
return nil, err
Expand Down

0 comments on commit 90be744

Please sign in to comment.