From 0af6ae62f08ab9a1a53f6b49a2562efcb0f27a52 Mon Sep 17 00:00:00 2001 From: Gerry Demaret Date: Fri, 20 Oct 2023 16:34:47 +0200 Subject: [PATCH] status: support output to a file Use renameio to get automagic atomic writes --- go.mod | 6 ++---- go.sum | 2 -- status.go | 10 +++++++++- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 7758ad7..e500b5a 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,8 @@ go 1.19 require ( github.com/BertoldVdb/ms-tools v0.0.0-20231004114646-22ddf782f595 github.com/alecthomas/kong v0.8.0 + github.com/google/renameio v1.0.1 github.com/sstallion/go-hid v0.14.1 ) -require ( - github.com/google/renameio v1.0.1 // indirect - golang.org/x/sys v0.8.0 // indirect -) +require golang.org/x/sys v0.8.0 // indirect diff --git a/go.sum b/go.sum index cf19b3e..78f0d9a 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/BertoldVdb/ms-tools v0.0.0-20221129120332-dd3c7641485a h1:HiNl4aayDj6YwzDHtbcRBX79r8BzCY6gBmGkalPkVuQ= -github.com/BertoldVdb/ms-tools v0.0.0-20221129120332-dd3c7641485a/go.mod h1:jmbR3cRIFTGz2Licvnbx5fv/SG6aLRJHpOILmEKTy5M= github.com/BertoldVdb/ms-tools v0.0.0-20231004114646-22ddf782f595 h1:dUuC+xTDdNG2kl6WF0O4iJKAzabaKhNfedXrdaz6pQs= github.com/BertoldVdb/ms-tools v0.0.0-20231004114646-22ddf782f595/go.mod h1:jmbR3cRIFTGz2Licvnbx5fv/SG6aLRJHpOILmEKTy5M= github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0= diff --git a/status.go b/status.go index d5b3430..546d040 100644 --- a/status.go +++ b/status.go @@ -2,9 +2,13 @@ package main import ( "fmt" + "os" "time" + "encoding/binary" "encoding/json" + + "github.com/google/renameio" ) type Region struct { @@ -56,7 +60,11 @@ func (s *StatusCmd) Run(c *Context) error { p = fmt.Sprintf("width: %d\nheight: %d\nsignal: %s\n", output.Width, output.Height, output.Signal) } - fmt.Print(p) + if (s.Filename == "") { + fmt.Print(p) + } else { + renameio.WriteFile(s.Filename, []byte(p), os.FileMode(int(0644))) + } if (s.Loop == 0) { next = false