From 6de057052066c9c137131351c3f39b41a2885fc9 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Tue, 16 Apr 2019 18:20:56 +0200 Subject: ui: implement log dialog; some refactoring in manage tunnels window to share some bits Signed-off-by: Alexander Neumann Signed-off-by: Jason A. Donenfeld --- ui/util.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 ui/util.go (limited to 'ui/util.go') 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)) +} -- cgit v1.2.3-59-g8ed1b