Initial commit
This commit is contained in:
		
							
								
								
									
										12
									
								
								utils/env.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								utils/env.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
package utils
 | 
			
		||||
import (
 | 
			
		||||
    "bytes"
 | 
			
		||||
    "os"
 | 
			
		||||
    "path/filepath"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var WORKDIR string = TryGetEnv( "GOLIFEHK_WORKDIR", filepath.Join( os.TempDir(), "golifehk" ) )
 | 
			
		||||
 | 
			
		||||
var BOM string = bytes.NewBuffer([]byte{ 0xEF, 0xBB, 0xBF }).String()
 | 
			
		||||
 | 
			
		||||
const ROUTE_CHARS string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
 | 
			
		||||
							
								
								
									
										45
									
								
								utils/shortcuts.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								utils/shortcuts.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
			
		||||
package utils
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
    "os"
 | 
			
		||||
    "strconv"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TryGetEnv[T any]( name string, fallback T ) T {
 | 
			
		||||
    v := os.Getenv( name )
 | 
			
		||||
 | 
			
		||||
    if v != "" {
 | 
			
		||||
        switch any( fallback ).(type) {
 | 
			
		||||
        case uint64:
 | 
			
		||||
            p, err := strconv.ParseUint( v, 10, 64 )
 | 
			
		||||
            if err == nil {
 | 
			
		||||
                return any( uint64( p ) ).(T)
 | 
			
		||||
            }
 | 
			
		||||
        case uint32:
 | 
			
		||||
            p, err := strconv.ParseUint( v, 10, 32 )
 | 
			
		||||
            if err == nil {
 | 
			
		||||
                return any( uint32( p ) ).(T)
 | 
			
		||||
            }
 | 
			
		||||
        case int:
 | 
			
		||||
            p, err := strconv.ParseInt( v, 10, 32 )
 | 
			
		||||
            if err == nil {
 | 
			
		||||
                return any( int( p ) ).(T)
 | 
			
		||||
            }
 | 
			
		||||
        case float64:
 | 
			
		||||
            p, err := strconv.ParseFloat( v, 64 )
 | 
			
		||||
            if err == nil {
 | 
			
		||||
                return any( float64( p ) ).(T)
 | 
			
		||||
            }
 | 
			
		||||
        case float32:
 | 
			
		||||
            p, err := strconv.ParseFloat( v, 32 )
 | 
			
		||||
            if err == nil {
 | 
			
		||||
                return any( float32( p ) ).(T)
 | 
			
		||||
            }
 | 
			
		||||
        default:
 | 
			
		||||
            return any( v ).(T)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return fallback
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										29
									
								
								utils/shortcuts_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								utils/shortcuts_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
package utils
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
    "os"
 | 
			
		||||
    "reflect"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func testType[T any]( t *testing.T, name string, fallback T ) T {
 | 
			
		||||
    got := TryGetEnv( "ABC", fallback )
 | 
			
		||||
    a := reflect.TypeOf( got ).Kind()
 | 
			
		||||
    b := reflect.TypeOf( fallback ).Kind()
 | 
			
		||||
    if a != b {
 | 
			
		||||
        t.Errorf( "%s is not of type %s", any( got ), b )
 | 
			
		||||
    }
 | 
			
		||||
    return got
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestAll(t *testing.T) {
 | 
			
		||||
    testType( t, "ABC", "Test" )
 | 
			
		||||
    testType( t, "ABC", 11 )
 | 
			
		||||
 | 
			
		||||
    os.Setenv( "ABC", "22" )
 | 
			
		||||
    got := testType( t, "ABC", 11 )
 | 
			
		||||
    if got != 22 {
 | 
			
		||||
        t.Errorf( "Expected 22, got %d", got )
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user