diff options
author | Alexander Neumann <alexander.neumann@picos-software.com> | 2019-04-16 18:20:56 +0200 |
---|---|---|
committer | Alexander Neumann <alexander.neumann@picos-software.com> | 2019-04-23 11:05:00 +0200 |
commit | 78b6a8e2550e971ca0a39ec41b586528fdb36e86 (patch) | |
tree | ea99f52a3ca41aac2779f4b4e16e395dfeefecb1 /ui/util.go | |
parent | ui: refactor exclude private IPs code to be closer to existing ports and preserve user edits (diff) | |
download | wireguard-windows-78b6a8e2550e971ca0a39ec41b586528fdb36e86.tar.xz wireguard-windows-78b6a8e2550e971ca0a39ec41b586528fdb36e86.zip |
ui: implement log dialog; some refactoring in manage tunnels window to share some bits
Signed-off-by: Alexander Neumann <alexander.neumann@picos-software.com>
Diffstat (limited to 'ui/util.go')
-rw-r--r-- | ui/util.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/ui/util.go b/ui/util.go new file mode 100644 index 00000000..b17f106c --- /dev/null +++ b/ui/util.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)) +} |