[cli] Add tooling for cli
This commit is contained in:
		
							
								
								
									
										11
									
								
								cli/cli.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								cli/cli.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
package cli
 | 
			
		||||
 | 
			
		||||
import "os"
 | 
			
		||||
 | 
			
		||||
func SetDefaultValueFromEnv(value *string, key string, defaultValue string) {
 | 
			
		||||
	if os.Getenv(key) != "" {
 | 
			
		||||
		*value = os.Getenv(key)
 | 
			
		||||
	} else {
 | 
			
		||||
		*value = defaultValue
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								cli/cli_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								cli/cli_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
package cli
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"os"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestSetDefaultValueFromEnv(t *testing.T) {
 | 
			
		||||
	err := os.Setenv("KEY1", "value1")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Errorf("unable to set env value: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	cases := []struct{
 | 
			
		||||
		key string
 | 
			
		||||
		defValue string
 | 
			
		||||
		expected string
 | 
			
		||||
	}{
 | 
			
		||||
		{"MISSING_KEY", "default", "default"},
 | 
			
		||||
		{"KEY1", "bad value", "value1"},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, c := range cases {
 | 
			
		||||
		var value = ""
 | 
			
		||||
		SetDefaultValueFromEnv(&value, c.key, c.defValue)
 | 
			
		||||
		if c.expected != value {
 | 
			
		||||
			t.Errorf("SetDefaultValueFromEnv(*value, %v, %v): %v, wants %v", c.key, c.defValue, value, c.expected)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user