From 18cdea3f881712883382acbe8be68202a94828b8 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 3 Jan 2023 15:52:01 +1100 Subject: [PATCH] Build constraint not working for non linux (#1481) * Build constraint not working for non linux * Go Build Constraint: Fix Non-Linux Filename (#1494) Correct the filename pkg/yqlib/chown_not_linux.go to escape the default OS detection in filename format `*_GOOS[_test].go`. Add an extra word after `linux` to resolve the issue. pkg/yqlib/chown_not_linux_os.go Signed-off-by: Bhargav Ravuri Signed-off-by: Bhargav Ravuri Signed-off-by: Bhargav Ravuri Co-authored-by: Bhargav Ravuri --- pkg/yqlib/chown_linux.go | 18 ++++++++++++++++++ pkg/yqlib/chown_not_linux_os.go | 12 ++++++++++++ pkg/yqlib/write_in_place_handler.go | 4 ++++ 3 files changed, 34 insertions(+) create mode 100644 pkg/yqlib/chown_linux.go create mode 100644 pkg/yqlib/chown_not_linux_os.go diff --git a/pkg/yqlib/chown_linux.go b/pkg/yqlib/chown_linux.go new file mode 100644 index 00000000..3913662b --- /dev/null +++ b/pkg/yqlib/chown_linux.go @@ -0,0 +1,18 @@ +//go:build linux + +package yqlib + +import ( + "io/fs" + "os" + "syscall" +) + +func changeOwner(info fs.FileInfo, file *os.File) error { + if stat, ok := info.Sys().(*syscall.Stat_t); ok { + uid := int(stat.Uid) + gid := int(stat.Gid) + return os.Chown(file.Name(), uid, gid) + } + return nil +} diff --git a/pkg/yqlib/chown_not_linux_os.go b/pkg/yqlib/chown_not_linux_os.go new file mode 100644 index 00000000..3675791a --- /dev/null +++ b/pkg/yqlib/chown_not_linux_os.go @@ -0,0 +1,12 @@ +//go:build !linux + +package yqlib + +import ( + "io/fs" + "os" +) + +func changeOwner(info fs.FileInfo, file *os.File) error { + return nil +} diff --git a/pkg/yqlib/write_in_place_handler.go b/pkg/yqlib/write_in_place_handler.go index d3bbb487..d49b2963 100644 --- a/pkg/yqlib/write_in_place_handler.go +++ b/pkg/yqlib/write_in_place_handler.go @@ -34,6 +34,10 @@ func (w *writeInPlaceHandlerImpl) CreateTempFile() (*os.File, error) { if err != nil { return nil, err } + + if err = changeOwner(info, file); err != nil { + return nil, err + } log.Debug("WriteInPlaceHandler: writing to tempfile: %v", file.Name()) w.tempFile = file return file, err