Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Refactor internal packages
Browse files Browse the repository at this point in the history
  • Loading branch information
achhabra2 committed Dec 15, 2021
1 parent 35e4917 commit 9b8e057
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 39 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ build/bin
# The black hole that is...
node_modules

.DS_Store
.DS_Store

# Log files

*.log
28 changes: 16 additions & 12 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
goruntime "runtime"
"time"

"riftshare/transport"
"riftshare/internal/settings"
"riftshare/internal/transport"
"riftshare/internal/update"

"github.com/gen2brain/beeep"
"github.com/psanford/wormhole-william/wormhole"
Expand All @@ -40,13 +42,13 @@ func NewApp() *App {
func (b *App) startup(ctx context.Context) {
// Perform your setup here
b.ctx = ctx
settings, err := GetUserSettings()
setting, err := settings.GetUserSettings()
if err != nil {
log.Println(err)
}
b.c.Notifications = settings.Notifications
b.c.OverwriteExisting = settings.Overwrite
b.c.DownloadPath = settings.DownloadsDirectory
b.c.Notifications = setting.Notifications
b.c.OverwriteExisting = setting.Overwrite
b.c.DownloadPath = setting.DownloadsDirectory
}

// domReady is called after the front-end dom has been loaded
Expand All @@ -58,12 +60,12 @@ func (b *App) domReady(ctx context.Context) {
// shutdown is called at application termination
func (b *App) shutdown(ctx context.Context) {
// Perform your teardown here
settings := UserSettings{
setting := settings.UserSettings{
Notifications: b.c.Notifications,
Overwrite: b.c.OverwriteExisting,
DownloadsDirectory: b.c.DownloadPath,
}
err := SaveUserSettings(settings)
err := settings.SaveUserSettings(setting)
if err != nil {
log.Println("Could not persist user settings")
}
Expand All @@ -74,8 +76,9 @@ func (b *App) OpenDirectoryDialog() ([]string, error) {
opts := runtime.OpenDialogOptions{Title: "Select Directory", DefaultDirectory: b.GetDownloadsFolder(), AllowDirectories: true}
selection, err := runtime.OpenDirectoryDialog(b.ctx, opts)
if err != nil {
runtime.LogInfo(b.ctx, "Error opening dialog")
runtime.LogError(b.ctx, "Error opening dialog")
b.ShowErrorDialog(err.Error())
return b.selectedFiles, errors.New("system error opening dialog")
}
runtime.LogInfo(b.ctx, "File Selected:"+selection)
if selection == "" {
Expand All @@ -90,8 +93,9 @@ func (b *App) OpenFilesDialog() ([]string, error) {
opts := runtime.OpenDialogOptions{Title: "Select File", AllowFiles: true, DefaultDirectory: b.GetDownloadsFolder()}
selection, err := runtime.OpenMultipleFilesDialog(b.ctx, opts)
if err != nil {
runtime.LogInfo(b.ctx, "Error opening dialog")
runtime.LogError(b.ctx, "Error opening dialog")
b.ShowErrorDialog(err.Error())
return b.selectedFiles, errors.New("system error opening dialog")
}
runtime.LogInfo(b.ctx, "File Selected:")
log.Println(selection)
Expand Down Expand Up @@ -308,7 +312,7 @@ func (b *App) CancelWormholeRequest() {
}

func (b *App) UpdateCheckUI() {
shouldUpdate, latestVersion := checkForUpdate()
shouldUpdate, latestVersion := update.CheckForUpdate()
if shouldUpdate {
updateMessage := fmt.Sprintf("New Version Available, would you like to update to v%s", latestVersion)
buttons := []string{"Yes", "No"}
Expand All @@ -320,7 +324,7 @@ func (b *App) UpdateCheckUI() {
runtime.LogInfo(b.ctx, action)
if action == "Yes" {
log.Println("Update clicked")
updated := doSelfUpdate()
updated := update.DoSelfUpdate()
if updated {
buttons = []string{"Ok"}
dialogOpts = runtime.MessageDialogOptions{Title: "Update Succeeded", Message: "Update Successfull. Please restart. ", Type: runtime.InfoDialog, Buttons: buttons, DefaultButton: "Ok"}
Expand All @@ -339,7 +343,7 @@ func (b *App) GetDownloadsFolder() string {
}

func (b *App) GetCurrentVersion() string {
return version
return update.Version
}

func (b *App) SetDownloadsFolder() string {
Expand Down
2 changes: 1 addition & 1 deletion build/windows/RiftShare.exe.manifest
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
<assemblyIdentity type="win32" name="RiftShare" version="0.0.0.1" processorArchitecture="amd64"/>
<assemblyIdentity type="win32" name="RiftShare" version="0.0.6.0" processorArchitecture="amd64"/>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
Expand Down
2 changes: 1 addition & 1 deletion frontend/dist/bundle.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions user_settings.go → internal/settings/user_settings.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main
package settings

import (
"errors"
"log"
"os"
"path/filepath"
"riftshare/transport"
"riftshare/internal/transport"
"runtime"

"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -55,7 +55,7 @@ func GetUserSettings() (UserSettings, error) {
return settings, nil
}

func getSettingsDirectory() (string, error) {
func GetSettingsDirectory() (string, error) {
var err error
homeDir, err := os.UserHomeDir()
prefDir := ""
Expand All @@ -71,7 +71,7 @@ func getSettingsDirectory() (string, error) {
}

func openPrefFile() (*os.File, error) {
dir, err := getSettingsDirectory()
dir, err := GetSettingsDirectory()
if err != nil {
log.Println(err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main
package settings

import "testing"

var settings UserSettings = UserSettings{Notifications: true, Overwrite: true, DownloadsDirectory: "test"}

func TestGetSettingsDirectory(t *testing.T) {
dir, err := getSettingsDirectory()
dir, err := GetSettingsDirectory()
if err != nil {
t.Error(err)
t.Fail()
Expand Down
2 changes: 1 addition & 1 deletion transport/receiver.go → internal/transport/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"path/filepath"
"strings"

"riftshare/transport/zip"
"riftshare/internal/transport/zip"

"github.com/psanford/wormhole-william/wormhole"
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 7 additions & 7 deletions selfupdate.go → internal/update/selfupdate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package update

import (
"log"
Expand All @@ -7,18 +7,18 @@ import (
"github.com/rhysd/go-github-selfupdate/selfupdate"
)

const version = "0.0.6"
const Version = "0.0.6"

func doSelfUpdate() bool {
v := semver.MustParse(version)
func DoSelfUpdate() bool {
v := semver.MustParse(Version)
latest, err := selfupdate.UpdateSelf(v, "achhabra2/riftshare")
if err != nil {
log.Println("Binary update failed:", err)
return false
}
if latest.Version.Equals(v) {
// latest version is the same as current version. It means current binary is up to date.
log.Println("Current binary is the latest version", version)
log.Println("Current binary is the latest version", Version)
return true
} else {
log.Println("Successfully updated to version", latest.Version)
Expand All @@ -27,14 +27,14 @@ func doSelfUpdate() bool {
}
}

func checkForUpdate() (bool, string) {
func CheckForUpdate() (bool, string) {
latest, found, err := selfupdate.DetectLatest("achhabra2/riftshare")
if err != nil {
log.Println("Error occurred while detecting version:", err)
return false, ""
}

v := semver.MustParse(version)
v := semver.MustParse(Version)
if !found || latest.Version.LTE(v) {
log.Println("Current version is the latest")
return false, ""
Expand Down
25 changes: 15 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"path/filepath"
goruntime "runtime"

"github.com/wailsapp/wails/v2/pkg/options/mac"
Expand All @@ -13,6 +14,9 @@ import (
"github.com/wailsapp/wails/v2/pkg/logger"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/windows"

"riftshare/internal/settings"
"riftshare/internal/update"
)

//go:embed frontend/dist
Expand All @@ -25,6 +29,15 @@ func main() {
// Create an instance of the app structure
app := NewApp()

settingsDir, ferr := settings.GetSettingsDirectory()
if ferr != nil {
log.Fatal("Could not open settings directory")
}

loggerPath := filepath.Join(settingsDir, "riftshare-output.log")
fileLogger := logger.NewFileLogger(loggerPath)
defer os.Remove(loggerPath)

width, height := GetAppDefaultDimensions()
// Create application with options
err := wails.Run(&options.App{
Expand All @@ -43,6 +56,7 @@ func main() {
RGBA: &options.RGBA{R: 33, G: 37, B: 43, A: 255},
Assets: assets,
LogLevel: logger.DEBUG,
Logger: fileLogger,
OnStartup: app.startup,
OnDomReady: app.domReady,
OnShutdown: app.shutdown,
Expand All @@ -61,7 +75,7 @@ func main() {
WebviewIsTransparent: false,
WindowIsTranslucent: false,
About: &mac.AboutInfo{
Title: fmt.Sprintf("RiftShare %v", version),
Title: fmt.Sprintf("RiftShare %v", update.Version),
Message: "Easy, Secure, Free file sharing",
Icon: icon,
},
Expand All @@ -83,12 +97,3 @@ func GetAppDefaultDimensions() (int, int) {
return 480, 400
}
}

func setupLogs() {
f, err := os.OpenFile("./riftshare-output.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
fmt.Printf("error opening file: %v", err)
}

log.SetOutput(f)
}
Binary file removed transport/.DS_Store
Binary file not shown.

0 comments on commit 9b8e057

Please sign in to comment.