Skip to content

Commit

Permalink
Merge pull request #171 from sol-eng/add-print-to-writestrings
Browse files Browse the repository at this point in the history
Add the print and log directly to WriteStrings
  • Loading branch information
tnederlof authored May 19, 2023
2 parents 7207f17 + d9367f6 commit b8e1906
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
5 changes: 3 additions & 2 deletions internal/ssl/trust.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"

"github.com/sol-eng/wbi/internal/config"
"github.com/sol-eng/wbi/internal/system"
)
Expand All @@ -14,7 +15,7 @@ func TrustRootCertificate(rootCA *x509.Certificate, osType config.OperatingSyste
pemCert = append(pemCert, string(pem.EncodeToMemory(&block)))
switch osType {
case config.Ubuntu18, config.Ubuntu20, config.Ubuntu22:
err := system.WriteStrings(pemCert, "/usr/local/share/ca-certificates/workbenchCA.crt", 0755)
err := system.WriteStrings(pemCert, "/usr/local/share/ca-certificates/workbenchCA.crt", 0755, true)
if err != nil {
return fmt.Errorf("writing certificate to disk failed: %w", err)
}
Expand All @@ -23,7 +24,7 @@ func TrustRootCertificate(rootCA *x509.Certificate, osType config.OperatingSyste
return fmt.Errorf("running command to trust root certificate: %w", err)
}
case config.Redhat7, config.Redhat8, config.Redhat9:
err := system.WriteStrings(pemCert, "/etc/pki/ca-trust/source/anchors/workbenchCA.crt", 0755)
err := system.WriteStrings(pemCert, "/etc/pki/ca-trust/source/anchors/workbenchCA.crt", 0755, true)
if err != nil {
return fmt.Errorf("writing CA certificate to disk failed: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/system/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "fmt"
// AddToPATH adds a path to the PATH environment variable in a profile.d script
func AddToPATH(path string, filename string) error {
fullFileName := "/etc/profile.d/wbi_" + filename + ".sh"
err := WriteStrings([]string{"PATH=" + path + ":$PATH"}, fullFileName, 0644)
err := WriteStrings([]string{"PATH=" + path + ":$PATH"}, fullFileName, 0644, true)
if err != nil {
return fmt.Errorf("failed to add to PATH: %w", err)
}
Expand Down
6 changes: 5 additions & 1 deletion internal/system/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import (
)

// WriteStrings appends a slice of strings to a file and creates the file if it doesn't exist
func WriteStrings(lines []string, filepath string, perm fs.FileMode) error {
func WriteStrings(lines []string, filepath string, perm fs.FileMode, print bool) error {
if print {
PrintAndLogInfo("\n=== Writing to the file " + filepath + " ===")
}

file, err := os.OpenFile(filepath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, perm)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
Expand Down
15 changes: 5 additions & 10 deletions internal/workbench/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ func WriteRepoConfig(url string, source string) error {
"CRAN=" + url,
}

system.PrintAndLogInfo("\n=== Writing to the file " + filepath + ":")
err := system.WriteStrings(writeLines, filepath, 0644)
err := system.WriteStrings(writeLines, filepath, 0644, true)
if err != nil {
return fmt.Errorf("failed to write config: %w", err)
}
Expand All @@ -43,8 +42,7 @@ func WriteRepoConfig(url string, source string) error {
"index-url=" + url,
}

system.PrintAndLogInfo("\n=== Writing to the file " + filepath + ":")
err := system.WriteStrings(writeLines, filepath, 0644)
err := system.WriteStrings(writeLines, filepath, 0644, true)
if err != nil {
return fmt.Errorf("failed to write config: %w", err)
}
Expand Down Expand Up @@ -109,8 +107,7 @@ func WriteSSLConfig(certPath string, keyPath string, serverURL string) error {
"ssl-certificate-key=" + keyPath,
}

system.PrintAndLogInfo("\n=== Writing to the file " + filepath + ":")
err = system.WriteStrings(writeLines, filepath, 0644)
err = system.WriteStrings(writeLines, filepath, 0644, true)
if err != nil {
return fmt.Errorf("failed to write config: %w", err)
}
Expand All @@ -135,8 +132,7 @@ func WriteConnectURLConfig(url string) error {
"default-rsconnect-server=" + url,
}

system.PrintAndLogInfo("\n=== Writing to the file " + filepath + ":")
err := system.WriteStrings(writeLines, filepath, 0644)
err := system.WriteStrings(writeLines, filepath, 0644, true)
if err != nil {
return fmt.Errorf("failed to write config: %w", err)
}
Expand All @@ -154,8 +150,7 @@ func WriteJupyterConfig(jupyterPath string) error {
}
filepath := "/etc/rstudio/jupyter.conf"

system.PrintAndLogInfo("\n=== Writing to the file " + filepath + ":")
err := system.WriteStrings(writeLines, filepath, 0644)
err := system.WriteStrings(writeLines, filepath, 0644, true)
if err != nil {
return fmt.Errorf("failed to write config: %w", err)
}
Expand Down

0 comments on commit b8e1906

Please sign in to comment.