aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ui/filesave.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-05-04 22:40:19 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2019-05-05 13:34:54 +0200
commitab56e18f1bf729223566160a9a5d0794e2fc96e1 (patch)
tree72a2ac5a937643459bc4c07a5b657557225ca177 /ui/filesave.go
parentui: use more windows icons (diff)
downloadwireguard-windows-ab56e18f1bf729223566160a9a5d0794e2fc96e1.tar.xz
wireguard-windows-ab56e18f1bf729223566160a9a5d0794e2fc96e1.zip
ui: syntax: implement trafic blocking semantics
This is our "auto kill switch".
Diffstat (limited to '')
-rw-r--r--ui/filesave.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/ui/filesave.go b/ui/filesave.go
new file mode 100644
index 00000000..b17f106c
--- /dev/null
+++ b/ui/filesave.go
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
+ */
+
+package ui
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/lxn/walk"
+)
+
+func writeFileWithOverwriteHandling(owner walk.Form, filePath string, write func(file *os.File) error) bool {
+ showError := func(err error) bool {
+ if err == nil {
+ return false
+ }
+
+ walk.MsgBox(owner, "Writing file failed", err.Error(), walk.MsgBoxIconError)
+
+ return true
+ }
+
+ file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0600)
+ if err != nil {
+ if os.IsExist(err) {
+ if walk.DlgCmdNo == walk.MsgBox(owner, "Writing file failed", fmt.Sprintf(`File "%s" already exists.
+
+Do you want to overwrite it?`, filePath), walk.MsgBoxYesNo|walk.MsgBoxDefButton2|walk.MsgBoxIconWarning) {
+ return false
+ }
+
+ if file, err = os.Create(filePath); err != nil {
+ return !showError(err)
+ }
+ } else {
+ return !showError(err)
+ }
+ }
+ defer file.Close()
+
+ return !showError(write(file))
+}