Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pactus gui lock support #947

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cmd/gtk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"

flock "github.com/gofrs/flock"
"github.com/gotk3/gotk3/gdk"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
Expand Down Expand Up @@ -56,6 +57,22 @@ func main() {
}
}

// Define the lock file path
lockFilePath := filepath.Join(workingDir, ".pactus.lock")
fileLock := flock.New(lockFilePath)

locked, err := fileLock.TryLock()
if err != nil {
// handle unable to attempt to acquire lock
fatalErrorCheck(err)
}

if !locked {
fmt.Printf("Could not lock '%s', another instance is running?", lockFilePath)

return
}

// Create a new app.
// When using GtkApplication, it is not necessary to call gtk_init() manually.
app, err := gtk.ApplicationNew(appID, glib.APPLICATION_NON_UNIQUE)
Expand Down Expand Up @@ -100,6 +117,7 @@ func main() {

// Connect function to application shutdown event, this is not required.
app.Connect("shutdown", func() {
_ = fileLock.Unlock()
log.Println("application shutdown")
})

Expand Down
Loading