mirror of
				https://github.com/ClusterCockpit/cc-backend
				synced 2025-11-04 09:35:07 +01:00 
			
		
		
		
	@@ -25,6 +25,7 @@ import (
 | 
			
		||||
	"github.com/ClusterCockpit/cc-backend/pkg/runtimeEnv"
 | 
			
		||||
	"github.com/ClusterCockpit/cc-backend/pkg/schema"
 | 
			
		||||
	"github.com/google/gops/agent"
 | 
			
		||||
	"github.com/joho/godotenv"
 | 
			
		||||
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	_ "github.com/mattn/go-sqlite3"
 | 
			
		||||
@@ -76,7 +77,8 @@ func main() {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := runtimeEnv.LoadEnv("./.env"); err != nil && !os.IsNotExist(err) {
 | 
			
		||||
	err := godotenv.Load()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Abortf("Could not parse existing .env file at location './.env'. Application startup failed, exited.\nError: %s\n", err.Error())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							@@ -58,6 +58,7 @@ require (
 | 
			
		||||
	github.com/hashicorp/go-multierror v1.1.1 // indirect
 | 
			
		||||
	github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
 | 
			
		||||
	github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf // indirect
 | 
			
		||||
	github.com/joho/godotenv v1.5.1 // indirect
 | 
			
		||||
	github.com/jonboulle/clockwork v0.5.0 // indirect
 | 
			
		||||
	github.com/josharian/intern v1.0.0 // indirect
 | 
			
		||||
	github.com/jpillora/backoff v1.0.0 // indirect
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
									
									
									
									
								
							@@ -137,6 +137,8 @@ github.com/jcmturner/rpc/v2 v2.0.3 h1:7FXXj8Ti1IaVFpSAziCZWNzbNuZmnvw/i6CqLNdWfZ
 | 
			
		||||
github.com/jcmturner/rpc/v2 v2.0.3/go.mod h1:VUJYCIDm3PVOEHw8sgt091/20OJjskO/YJki3ELg/Hc=
 | 
			
		||||
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
 | 
			
		||||
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
 | 
			
		||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
 | 
			
		||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
 | 
			
		||||
github.com/jonboulle/clockwork v0.5.0 h1:Hyh9A8u51kptdkR+cqRpT1EebBwTn1oK9YfGYbdFz6I=
 | 
			
		||||
github.com/jonboulle/clockwork v0.5.0/go.mod h1:3mZlmanh0g2NDKO5TWZVJAfofYk64M7XN3SzBPjZF60=
 | 
			
		||||
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
 | 
			
		||||
 
 | 
			
		||||
@@ -5,85 +5,16 @@
 | 
			
		||||
package runtimeEnv
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bufio"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"os/user"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"syscall"
 | 
			
		||||
 | 
			
		||||
	"github.com/ClusterCockpit/cc-backend/pkg/log"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Very simple and limited .env file reader.
 | 
			
		||||
// All variable definitions found are directly
 | 
			
		||||
// added to the processes environment.
 | 
			
		||||
func LoadEnv(file string) error {
 | 
			
		||||
	f, err := os.Open(file)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("Error while opening .env file")
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	defer f.Close()
 | 
			
		||||
	s := bufio.NewScanner(bufio.NewReader(f))
 | 
			
		||||
	for s.Scan() {
 | 
			
		||||
		line := s.Text()
 | 
			
		||||
		if strings.HasPrefix(line, "#") || len(line) == 0 {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if strings.Contains(line, "#") {
 | 
			
		||||
			return errors.New("'#' are only supported at the start of a line")
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		line = strings.TrimPrefix(line, "export ")
 | 
			
		||||
		parts := strings.SplitN(line, "=", 2)
 | 
			
		||||
		if len(parts) != 2 {
 | 
			
		||||
			return fmt.Errorf("RUNTIME/SETUP > unsupported line: %#v", line)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		key := strings.TrimSpace(parts[0])
 | 
			
		||||
		val := strings.TrimSpace(parts[1])
 | 
			
		||||
		if strings.HasPrefix(val, "\"") {
 | 
			
		||||
			if !strings.HasSuffix(val, "\"") {
 | 
			
		||||
				return fmt.Errorf("RUNTIME/SETUP > unsupported line: %#v", line)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			runes := []rune(val[1 : len(val)-1])
 | 
			
		||||
			sb := strings.Builder{}
 | 
			
		||||
			for i := 0; i < len(runes); i++ {
 | 
			
		||||
				if runes[i] == '\\' {
 | 
			
		||||
					i++
 | 
			
		||||
					switch runes[i] {
 | 
			
		||||
					case 'n':
 | 
			
		||||
						sb.WriteRune('\n')
 | 
			
		||||
					case 'r':
 | 
			
		||||
						sb.WriteRune('\r')
 | 
			
		||||
					case 't':
 | 
			
		||||
						sb.WriteRune('\t')
 | 
			
		||||
					case '"':
 | 
			
		||||
						sb.WriteRune('"')
 | 
			
		||||
					default:
 | 
			
		||||
						return fmt.Errorf("RUNTIME/SETUP > unsupported escape sequence in quoted string: backslash %#v", runes[i])
 | 
			
		||||
					}
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
				sb.WriteRune(runes[i])
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			val = sb.String()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		os.Setenv(key, val)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return s.Err()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Changes the processes user and group to that
 | 
			
		||||
// specified in the config.json. The go runtime
 | 
			
		||||
// takes care of all threads (and not only the calling one)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user