github.com/konsorten/ktn-build-info@v1.0.11/ver/env_read.go (about)

     1  package ver
     2  
     3  import (
     4  	"os"
     5  	"strconv"
     6  
     7  	log "github.com/sirupsen/logrus"
     8  )
     9  
    10  func TryReadFromEnvironmentVariables(varMap map[string]string) (*VersionInformation, error) {
    11  	vi := MakeVersionInformation()
    12  
    13  	// project name
    14  	if varMap["name"] != "" {
    15  		v := os.Getenv(varMap["name"])
    16  
    17  		if v != "" {
    18  			log.Debugf("Found project name in environment variable %v: %v", varMap["rev"], v)
    19  
    20  			vi.Name = v
    21  		}
    22  	}
    23  
    24  	// project description
    25  	if varMap["desc"] != "" {
    26  		v := os.Getenv(varMap["desc"])
    27  
    28  		if v != "" {
    29  			log.Debugf("Found project description in environment variable %v: %v", varMap["rev"], v)
    30  
    31  			vi.Description = v
    32  		}
    33  	}
    34  
    35  	// revision
    36  	if varMap["rev"] != "" {
    37  		v := os.Getenv(varMap["rev"])
    38  
    39  		if v != "" {
    40  			log.Debugf("Found revision information in environment variable %v: %v", varMap["rev"], v)
    41  
    42  			vi.Revision = v
    43  		}
    44  	}
    45  
    46  	// build id
    47  	if varMap["build"] != "" {
    48  		v := os.Getenv(varMap["build"])
    49  
    50  		if v != "" {
    51  			i, err := strconv.Atoi(v)
    52  
    53  			if err != nil {
    54  				log.Fatalf("Failed to parse build ID in environment variable %v: %v", varMap["rev"], v)
    55  				return nil, err
    56  			}
    57  
    58  			log.Debugf("Found build ID in environment variable %v: %v", varMap["rev"], v)
    59  
    60  			vi.Build = i
    61  		}
    62  	}
    63  
    64  	// build host
    65  	if varMap["buildHost"] != "" {
    66  		v := os.Getenv(varMap["buildHost"])
    67  
    68  		if v != "" {
    69  			log.Debugf("Found build host name in environment variable %v: %v", varMap["rev"], v)
    70  
    71  			vi.BuildHost = v
    72  		}
    73  	}
    74  
    75  	// done
    76  	return vi, nil
    77  }