From e872bcccaebad340760ca931fcec0f960a89b19a Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Sat, 13 Jul 2024 11:13:40 +1000 Subject: [PATCH] Switched to OS version of write file --- services.go | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/services.go b/services.go index e1d5d8a..d02f4fa 100644 --- a/services.go +++ b/services.go @@ -24,18 +24,14 @@ func (s *Services) AddPrivateKey() error { return err } + if err := writeFile(`${HOME}/.ssh/id_rsa`, s.cfg.PrivateKey, 0400); err != nil { + return err + } + if err := runCmd(`chmod 700 $HOME/.ssh`); err != nil { return err } - if err := writeFile(`${HOME}/.ssh/id_rsa`, s.cfg.PrivateKey); err != nil { - return err - } - - if err := runCmd(`chmod -R 400 $HOME/.ssh/.`); err != nil { - return err - } - return nil } @@ -53,11 +49,7 @@ func (s *Services) ConfigureSSH() error { return err } - if err := writeFile(`${HOME}/.ssh/config`, bfr.String()); err != nil { - return err - } - - if err := runCmd(`chmod 400 $HOME/.ssh/config`); err != nil { + if err := writeFile(`${HOME}/.ssh/config`, bfr.String(), 0400); err != nil { return err } @@ -91,12 +83,11 @@ func runCmd(cmd string) error { return script.Exec(fullCmd).Error() } -func writeFile(path string, content string) error { +func writeFile(path string, content string, mode os.FileMode) error { fullPath := os.ExpandEnv(path) log.Printf(" .. [file] %v", fullPath) - _, err := script.Echo(content).WriteFile(fullPath) - return err + return os.WriteFile(fullPath, []byte(content), mode) } var (