Skip to content

Commit

Permalink
Config Parse
Browse files Browse the repository at this point in the history
  • Loading branch information
atterpac committed Jan 19, 2024
1 parent 2436229 commit aea6028
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
4 changes: 3 additions & 1 deletion v3/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21.1
toolchain go1.21.4

require (
github.com/atterpac/refresh v0.4.32
github.com/atterpac/refresh v0.4.8
github.com/bep/debounce v1.2.1
github.com/ebitengine/purego v0.4.0-alpha.4
github.com/go-git/go-git/v5 v5.11.0
Expand Down Expand Up @@ -117,3 +117,5 @@ require (
modernc.org/token v1.0.1 // indirect
mvdan.cc/sh/v3 v3.7.0 // indirect
)

replace github.com/atterpac/refresh => ../../../../projects/refresh
2 changes: 2 additions & 0 deletions v3/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuW
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/atterpac/refresh v0.4.7 h1:nxJLnar1MoSnKgo4PJBo9wpCiDExF+DhvUd0B3kRjBA=
github.com/atterpac/refresh v0.4.7/go.mod h1:1mhPVWQ9R8Xlr/ouNrtAVEAsEVOt65J+6FFYWMMKRN0=
github.com/atterpac/refresh v0.4.32 h1:sy2T3rNtbGYffZQWGpqRafZ1QwQMGLkRafCTX52fv0w=
github.com/atterpac/refresh v0.4.32/go.mod h1:1mhPVWQ9R8Xlr/ouNrtAVEAsEVOt65J+6FFYWMMKRN0=
github.com/bep/debounce v1.2.1 h1:v67fRdBA9UQu2NhLFXrSg0Brw7CexQekrBwDMM8bzeY=
Expand Down
50 changes: 47 additions & 3 deletions v3/internal/commands/watcher.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
package commands

import (
"log/slog"
"os"
"runtime"
"strings"

"github.com/atterpac/refresh/engine"
"github.com/wailsapp/wails/v3/internal/signal"
"os"
)

type WatcherOptions struct {
Config string `description:"The config file including path" default:"."`
}

const (
windowsExtension = ".exe"
linuxExtension = ""
darwinExtension = ".app"

extensionVar = "<OS_EXTENSION>"
)


func Watcher(options *WatcherOptions) error {
stopChan := make(chan struct{})
watcherEngine := engine.NewEngineFromTOML(options.Config)
watcherEngine := engine.Engine{}
parsedConfig, err := parseConfig(options.Config)
if err != nil {
return err
}
err = watcherEngine.StringtoConfigYAML(parsedConfig)
if err != nil {
return err
}
signalHandler := signal.NewSignalHandler(func() {
watcherEngine.Stop()
stopChan <- struct{}{}
Expand All @@ -21,10 +42,33 @@ func Watcher(options *WatcherOptions) error {
return ""
}
signalHandler.Start()
err := watcherEngine.Start()
err = watcherEngine.Start()
if err != nil {
return err
}
<-stopChan
return nil
}

func parseConfig(config string) (string, error) {
_, err := os.Stat(config)
if err != nil {
return "", err
}
byteConfig, err := os.ReadFile(config)
if err != nil {
return "", err
}
var ext string
switch runtime.GOOS {
case "windows":
ext = windowsExtension
case "linux":
ext = linuxExtension
case "darwin":
ext = darwinExtension
}
stringConfig := strings.ReplaceAll(string(byteConfig), extensionVar, ext)
slog.Warn(stringConfig)
return stringConfig, nil
}

0 comments on commit aea6028

Please sign in to comment.