github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/core/chaincode/platforms/golang/env.go (about)

     1  /*
     2  Copyright 2017 - Greg Haskins <gregory.haskins@gmail.com>
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package golang
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  	"time"
    24  )
    25  
    26  type Env map[string]string
    27  
    28  func getEnv() Env {
    29  	env := make(Env)
    30  	for _, entry := range os.Environ() {
    31  		tokens := strings.SplitN(entry, "=", 2)
    32  		if len(tokens) > 1 {
    33  			env[tokens[0]] = tokens[1]
    34  		}
    35  	}
    36  
    37  	return env
    38  }
    39  
    40  func getGoEnv() (Env, error) {
    41  	env := getEnv()
    42  
    43  	goenvbytes, err := runProgram(env, 10*time.Second, "go", "env")
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	goenv := make(Env)
    49  
    50  	envout := strings.Split(string(goenvbytes), "\n")
    51  	for _, entry := range envout {
    52  		tokens := strings.SplitN(entry, "=", 2)
    53  		if len(tokens) > 1 {
    54  			goenv[tokens[0]] = strings.Trim(tokens[1], "\"")
    55  		}
    56  	}
    57  
    58  	return goenv, nil
    59  }
    60  
    61  func flattenEnv(env Env) []string {
    62  	result := make([]string, 0)
    63  	for k, v := range env {
    64  		result = append(result, k+"="+v)
    65  	}
    66  
    67  	return result
    68  }
    69  
    70  type Paths map[string]bool
    71  
    72  func splitEnvPaths(value string) Paths {
    73  	_paths := filepath.SplitList(value)
    74  	paths := make(Paths)
    75  	for _, path := range _paths {
    76  		paths[path] = true
    77  	}
    78  	return paths
    79  }
    80  
    81  func flattenEnvPaths(paths Paths) string {
    82  
    83  	_paths := make([]string, 0)
    84  	for path, _ := range paths {
    85  		_paths = append(_paths, path)
    86  	}
    87  
    88  	return strings.Join(_paths, string(os.PathListSeparator))
    89  }