github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/util/env/env.go (about)

     1  // Copyright (c) 2018, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package env
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  	"strings"
    12  )
    13  
    14  // SetFromList sets environment variables from environ argument list
    15  func SetFromList(environ []string) error {
    16  	for _, env := range environ {
    17  		splitted := strings.SplitN(env, "=", 2)
    18  		if len(splitted) != 2 {
    19  			return fmt.Errorf("can't process environment variable %s", env)
    20  		}
    21  		if err := os.Setenv(splitted[0], splitted[1]); err != nil {
    22  			return err
    23  		}
    24  	}
    25  	return nil
    26  }