From 28998dd0ccfffaf79418053e17ec25646f7c15de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Matczuk?= Date: Tue, 9 Jul 2024 11:29:06 +0200 Subject: [PATCH] bind: remove newOSFileFlag Reimplement file binding with plain anyflag.NewValueWithRedact[*os.File]() --- bind/file.go | 33 --------------------------------- bind/flag.go | 4 ++-- bind/redact.go | 8 ++++++++ 3 files changed, 10 insertions(+), 35 deletions(-) delete mode 100644 bind/file.go diff --git a/bind/file.go b/bind/file.go deleted file mode 100644 index ff5362ba..00000000 --- a/bind/file.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2022-2024 Sauce Labs Inc., all rights reserved. -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at https://mozilla.org/MPL/2.0/. - -package bind - -import ( - "os" - - "github.com/spf13/pflag" -) - -// osFileFlag allows to print the file name instead of the file descriptor. -type osFileFlag struct { - pflag.Value - f **os.File -} - -func (f *osFileFlag) String() string { - if *f.f == nil { - return "" - } - return (*f.f).Name() -} - -func newOSFileFlag(v pflag.Value, f **os.File) pflag.Value { - if f == nil { - panic("nil pointer") - } - return &osFileFlag{v, f} -} diff --git a/bind/flag.go b/bind/flag.go index 4a51816b..a3ebbc25 100644 --- a/bind/flag.go +++ b/bind/flag.go @@ -382,8 +382,8 @@ func TLSServerConfig(fs *pflag.FlagSet, cfg *forwarder.TLSServerConfig, namePref } func LogConfig(fs *pflag.FlagSet, cfg *log.Config) { - fs.VarP(newOSFileFlag(anyflag.NewValue[*os.File](nil, &cfg.File, - forwarder.OpenFileParser(log.DefaultFileFlags, log.DefaultFileMode, log.DefaultDirMode)), &cfg.File), + fs.VarP(anyflag.NewValueWithRedact[*os.File](cfg.File, &cfg.File, + forwarder.OpenFileParser(log.DefaultFileFlags, log.DefaultFileMode, log.DefaultDirMode), DisplayFileName), "log-file", "", ""+ "Path to the log file, if empty, logs to stdout. "+ "The file is reopened on SIGHUP to allow log rotation using external tools. ") diff --git a/bind/redact.go b/bind/redact.go index e34d11e7..6e8f0275 100644 --- a/bind/redact.go +++ b/bind/redact.go @@ -9,6 +9,7 @@ package bind import ( "fmt" "net/url" + "os" "strings" "github.com/saucelabs/forwarder/header" @@ -39,3 +40,10 @@ func RedactBase64(s string) string { return s } + +func DisplayFileName(f *os.File) string { + if f == nil { + return "" + } + return f.Name() +}