Authentication-Results: mail-b.sr.ht; dkim=pass header.d=secluded.site header.i=@secluded.site Received: from mx.nixnet.email (mx.nixnet.email [94.16.121.167]) by mail-b.sr.ht (Postfix) with ESMTPS id 479C911F08C for <~whereswaldon/arbor-dev@lists.sr.ht>; Thu, 21 Jul 2022 03:10:47 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by mx.nixnet.email (Postfix) with ESMTPSA id E30A320286E; Wed, 20 Jul 2022 23:10:36 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=secluded.site; s=202002021149; t=1658373037; h=from:from:reply-to:subject:subject:to:to:cc:cc; bh=HYBHYssDpvZ7etk61+oOrFo/GefPRkxZvJKh+iFM8o4=; b=rmedmg6M8hnYwNQsn5wvTSZYi5A+GOawXB4MhZRSDZJPbVLOf94A7dulGUPKVfOr2sttcK hT+Mq3lecT/figZTwnhS07DDAdvwdklrww+0hb6plpL/9VCddjMCe++iFE6Or9Frpigd+E yB0vKYWQRP9yWRaNojUlLjD3qTkH/Mc= From: Amolith To: ~whereswaldon/arbor-dev@lists.sr.ht Cc: Amolith Subject: [PATCH 08/12] Add copy function for writing from embedded fs Date: Wed, 20 Jul 2022 23:09:57 -0400 Message-Id: <20220721031001.97027-9-amolith@secluded.site> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220721031001.97027-1-amolith@secluded.site> References: <20220721031001.97027-1-amolith@secluded.site> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Signed-off-by: Amolith --- setup.go | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/setup.go b/setup.go index ca0a3b4..f99f401 100644 --- a/setup.go +++ b/setup.go @@ -61,14 +61,13 @@ func Users() error { } func Sudoers() error { - fmt.Println("Running target sudoers") - return sh.Run("cp", "files/sudoers", "/etc/sudoers") + log.Println("Running target sudoers") + return copy("files/sudoers", "/etc/sudoers") } func SshConfig() error { - fmt.Println("Running target ssh_config") - err := sh.Run("cp", "files/sshd_config", "/etc/ssh/sshd_config") - + log.Println("Running target ssh_config") + err := copy("files/sshd_config", "/etc/sshd/sshd_config") if err != nil { return err } @@ -192,6 +191,31 @@ func Caddy() error { return sh.Run("systemctl", "restart", "caddy") } +// copy() copies the source rfile from the embedded filesystem to the +// destination wfile on the host filesystem. +func copy(rfile string, wfile string) error { + log.Println("Copying", rfile, "to", wfile) + + // Open rfile (read file) and stores it in sfile (source file) + sfile, err := files.Open(rfile) + if err != nil { + return err + } + + // Open wfile (write file) stores it in dfile (destination file) + dfile, err := os.OpenFile(wfile, os.O_CREATE|os.O_RDWR, 0660) + if err != nil { + return err + } + + // Write sfile to filesystem and discard number of bytes written + _, err = io.Copy(dfile, sfile) + if err != nil { + return err + } + return nil +} + // config() takes source template and destination file arguments and executes // the template replacing `hostname` with the system's hostname. func config(rfile string, wfile string) error { -- 2.37.1