github.com/webx-top/com@v1.2.12/platform.go (about)

     1  package com
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  	"runtime"
     7  	"strings"
     8  	"time"
     9  )
    10  
    11  const (
    12  	IsWindows = runtime.GOOS == "windows"
    13  	IsLinux   = runtime.GOOS == "linux"
    14  	IsMac     = runtime.GOOS == "darwin"
    15  	Is32Bit   = runtime.GOARCH == "386"
    16  	Is64Bit   = runtime.GOARCH == "amd64"
    17  )
    18  
    19  // ExitOnSuccess 成功时退出程序
    20  func ExitOnSuccess(msg string) {
    21  	os.Stdout.WriteString(msg)
    22  	if !strings.HasSuffix(msg, StrLF) {
    23  		os.Stdout.WriteString(StrLF)
    24  	}
    25  	os.Exit(0)
    26  }
    27  
    28  // ExitOnFailure 失败时退出程序
    29  func ExitOnFailure(msg string, errCodes ...int) {
    30  	errCode := 1
    31  	if len(errCodes) > 0 {
    32  		errCode = errCodes[0]
    33  	}
    34  	os.Stderr.WriteString(msg)
    35  	if !strings.HasSuffix(msg, StrLF) {
    36  		os.Stdout.WriteString(StrLF)
    37  	}
    38  	os.Exit(errCode)
    39  }
    40  
    41  func GetenvOr(key string) string {
    42  	parts := strings.SplitN(key, `:`, 2)
    43  	v := os.Getenv(parts[0])
    44  	if len(v) == 0 && len(parts) == 2 {
    45  		v = parts[1]
    46  	}
    47  	return v
    48  }
    49  
    50  func Getenv(key string, defaults ...string) string {
    51  	v := os.Getenv(key)
    52  	if len(v) == 0 && len(defaults) > 0 {
    53  		return defaults[0]
    54  	}
    55  	return v
    56  }
    57  
    58  func GetenvInt(key string, defaults ...int) int {
    59  	v := os.Getenv(key)
    60  	if len(v) == 0 && len(defaults) > 0 {
    61  		return defaults[0]
    62  	}
    63  	return Int(v)
    64  }
    65  
    66  func GetenvUint(key string, defaults ...uint) uint {
    67  	v := os.Getenv(key)
    68  	if len(v) == 0 && len(defaults) > 0 {
    69  		return defaults[0]
    70  	}
    71  	return Uint(v)
    72  }
    73  
    74  func GetenvInt64(key string, defaults ...int64) int64 {
    75  	v := os.Getenv(key)
    76  	if len(v) == 0 && len(defaults) > 0 {
    77  		return defaults[0]
    78  	}
    79  	return Int64(v)
    80  }
    81  
    82  func GetenvUint64(key string, defaults ...uint64) uint64 {
    83  	v := os.Getenv(key)
    84  	if len(v) == 0 && len(defaults) > 0 {
    85  		return defaults[0]
    86  	}
    87  	return Uint64(v)
    88  }
    89  
    90  func GetenvInt32(key string, defaults ...int32) int32 {
    91  	v := os.Getenv(key)
    92  	if len(v) == 0 && len(defaults) > 0 {
    93  		return defaults[0]
    94  	}
    95  	return Int32(v)
    96  }
    97  
    98  func GetenvUint32(key string, defaults ...uint32) uint32 {
    99  	v := os.Getenv(key)
   100  	if len(v) == 0 && len(defaults) > 0 {
   101  		return defaults[0]
   102  	}
   103  	return Uint32(v)
   104  }
   105  
   106  func GetenvFloat32(key string, defaults ...float32) float32 {
   107  	v := os.Getenv(key)
   108  	if len(v) == 0 && len(defaults) > 0 {
   109  		return defaults[0]
   110  	}
   111  	return Float32(v)
   112  }
   113  
   114  func GetenvFloat64(key string, defaults ...float64) float64 {
   115  	v := os.Getenv(key)
   116  	if len(v) == 0 && len(defaults) > 0 {
   117  		return defaults[0]
   118  	}
   119  	return Float64(v)
   120  }
   121  
   122  func GetenvBool(key string, defaults ...bool) bool {
   123  	v := os.Getenv(key)
   124  	if len(v) == 0 && len(defaults) > 0 {
   125  		return defaults[0]
   126  	}
   127  	return Bool(v)
   128  }
   129  
   130  func GetenvDuration(key string, defaults ...time.Duration) time.Duration {
   131  	v := os.Getenv(key)
   132  	if len(v) > 0 {
   133  		t, err := time.ParseDuration(v)
   134  		if err == nil {
   135  			return t
   136  		}
   137  		log.Printf("GetenvDuration: %v: %v\n", v, err)
   138  	}
   139  	if len(defaults) > 0 {
   140  		return defaults[0]
   141  	}
   142  	return 0
   143  }