aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/admin/jvm/properties.go
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2023-02-03 15:20:23 +0100
committerMartin Polden <mpolden@mpolden.no>2023-02-03 15:35:25 +0100
commite1e94812425a487069bf33f781bec987e9e49874 (patch)
tree4a892c3b5c0a7dee2cb76f9971e538cb4aba8a16 /client/go/internal/admin/jvm/properties.go
parenta08ae588d6035b69f0961dff596fc871fd1c4e58 (diff)
Re-organize Go code
Diffstat (limited to 'client/go/internal/admin/jvm/properties.go')
-rw-r--r--client/go/internal/admin/jvm/properties.go106
1 files changed, 106 insertions, 0 deletions
diff --git a/client/go/internal/admin/jvm/properties.go b/client/go/internal/admin/jvm/properties.go
new file mode 100644
index 00000000000..9aae550b10a
--- /dev/null
+++ b/client/go/internal/admin/jvm/properties.go
@@ -0,0 +1,106 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+// Author: arnej
+
+package jvm
+
+import (
+ "bytes"
+ "fmt"
+ "os"
+ "sort"
+ "strings"
+
+ "github.com/vespa-engine/vespa/client/go/internal/admin/trace"
+ "github.com/vespa-engine/vespa/client/go/internal/util"
+)
+
+// quote as specified in JDK source file java.base/share/classes/java/util/Properties.java
+func propQuote(s string, buf *bytes.Buffer) {
+ inKey := true
+ inVal := false
+ for _, ch := range s {
+ needQ := false
+ switch {
+ case ch == ' ':
+ needQ = !inVal
+ case ch == ':':
+ needQ = inKey
+ case ch < ' ':
+ needQ = true
+ case ch > '~':
+ needQ = true
+ case ch == '\\':
+ buf.WriteString("\\")
+ case ch == '=':
+ inKey = false
+ default:
+ inVal = !inKey
+ }
+ if needQ {
+ fmt.Fprintf(buf, "\\u%04X", ch)
+ } else {
+ buf.WriteRune(ch)
+ }
+ }
+}
+
+func envAsProperties(envv []string) []byte {
+ suppress := map[string]bool{
+ "_": true,
+ "HISTCONTROL": true,
+ "HISTSIZE": true,
+ "IFS": true,
+ "LESSOPEN": true,
+ "LOADEDMODULES": true,
+ "LS_COLORS": true,
+ "MAIL": true,
+ "MODULEPATH": true,
+ "MODULEPATH_modshare": true,
+ "MODULESHOME": true,
+ "MODULES_CMD": true,
+ "MODULES_RUN_QUARANTINE": true,
+ "OLDPWD": true,
+ "PCP_DIR": true,
+ "PS1": true,
+ "PWD": true,
+ "SHLVL": true,
+ "SSH_AUTH_SOCK": true,
+ "SSH_CLIENT": true,
+ "SSH_CONNECTION": true,
+ "SSH_TTY": true,
+ "S_COLORS": true,
+ "which_declare": true,
+ "": true,
+ }
+ var buf bytes.Buffer
+ buf.WriteString("# properties converted from environment variables\n")
+ sort.Strings(envv)
+ for _, env := range envv {
+ parts := strings.Split(env, "=")
+ if len(parts) >= 2 {
+ varName := parts[0]
+ if suppress[varName] || strings.Contains(varName, "%%") {
+ continue
+ }
+ if strings.Contains(env, "\n") {
+ continue
+ }
+ propQuote(env, &buf)
+ buf.WriteRune('\n')
+ } else {
+ trace.Warning("environment value without '=':", env)
+ }
+ }
+ return buf.Bytes()
+}
+
+func writeEnvAsProperties(envv []string, propsFile string) {
+ if propsFile == "" {
+ panic("missing propsFile")
+ }
+ trace.Trace("write props file:", propsFile)
+ err := os.WriteFile(propsFile, envAsProperties(envv), 0600)
+ if err != nil {
+ util.JustExitWith(err)
+ }
+}