Skip to content

Commit

Permalink
Added SystemEnvironmentPropertySource to get the values of the enviro…
Browse files Browse the repository at this point in the history
…nment properties.
  • Loading branch information
burakkoken committed Jan 8, 2021
1 parent 8be6bfc commit 4afc203
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 16 deletions.
16 changes: 1 addition & 15 deletions cmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type CommandLinePropertySource interface {
}

type SimpleCommandLinePropertySource struct {
name string
source CommandLineArgs
}

Expand All @@ -23,26 +22,13 @@ func NewSimpleCommandLinePropertySource(args []string) SimpleCommandLineProperty
panic(err)
}
cmdlinePropertySource := SimpleCommandLinePropertySource{
name: ProcyonApplicationCommandLinePropertySource,
source: cmdLineArgs,
}
return cmdlinePropertySource
}

func SimpleCommandLinePropertySourceWithName(name string, args []string) SimpleCommandLinePropertySource {
cmdLineArgs, err := NewCommandLineArgsParser().Parse(args)
if err != nil {
panic(err)
}
cmdlinePropertySource := SimpleCommandLinePropertySource{
name: name,
source: cmdLineArgs,
}
return cmdlinePropertySource
}

func (cmdLineSource SimpleCommandLinePropertySource) GetName() string {
return cmdLineSource.name
return ProcyonApplicationCommandLinePropertySource
}

func (cmdLineSource SimpleCommandLinePropertySource) GetSource() interface{} {
Expand Down
89 changes: 88 additions & 1 deletion environment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package core

import "os"
import (
"os"
"strings"
)

type Environment interface {
PropertyResolver
Expand Down Expand Up @@ -47,3 +50,87 @@ func (env StandardEnvironment) GetProperty(name string, defaultValue string) int
func (env StandardEnvironment) GetTypeConverterService() TypeConverterService {
return env.converterService
}

const ProcyonSystemEnvironmentPropertySource = "ProcyonSystemEnvironmentPropertySource"

type SystemEnvironmentPropertySource struct {
environmentProperties map[string]string
}

func NewSystemEnvironmentPropertySource() SystemEnvironmentPropertySource {
propertySource := SystemEnvironmentPropertySource{
environmentProperties: make(map[string]string, 0),
}

environmentProperties := os.Environ()
for _, property := range environmentProperties {
index := strings.Index(property, "=")
if index != -1 {
propertySource.environmentProperties[property[:index]] = property[index+1:]
}
}
return propertySource
}

func (propertySource SystemEnvironmentPropertySource) GetName() string {
return ProcyonSystemEnvironmentPropertySource
}

func (propertySource SystemEnvironmentPropertySource) GetSource() interface{} {
return propertySource.environmentProperties
}

func (propertySource SystemEnvironmentPropertySource) GetProperty(name string) interface{} {
actualPropertyName := propertySource.checkPropertyName(strings.ToLower(name))
if actualPropertyName != nil {
return propertySource.environmentProperties[actualPropertyName.(string)]
}

actualPropertyName = propertySource.checkPropertyName(strings.ToUpper(name))
if actualPropertyName != nil {
return propertySource.environmentProperties[actualPropertyName.(string)]
}
return nil
}

func (propertySource SystemEnvironmentPropertySource) ContainsProperty(name string) bool {
return propertySource.checkPropertyName(strings.ToUpper(name)) != nil || propertySource.checkPropertyName(strings.ToLower(name)) != nil
}

func (propertySource SystemEnvironmentPropertySource) GetPropertyNames() []string {
keys := make([]string, 0, len(propertySource.environmentProperties))
for key, _ := range propertySource.environmentProperties {
keys = append(keys, key)
}
return keys
}

func (propertySource SystemEnvironmentPropertySource) checkIfPresent(propertyName string) bool {
if _, ok := propertySource.environmentProperties[propertyName]; ok {
return true
}
return false
}

func (propertySource SystemEnvironmentPropertySource) checkPropertyName(propertyName string) interface{} {
if propertySource.checkIfPresent(propertyName) {
return propertyName
}

noHyphenPropertyName := strings.ReplaceAll(propertyName, "-", "_")
if propertyName != noHyphenPropertyName && propertySource.checkIfPresent(noHyphenPropertyName) {
return noHyphenPropertyName
}

noDotPropertyName := strings.ReplaceAll(propertyName, ".", "_")
if propertyName != noDotPropertyName && propertySource.checkIfPresent(noDotPropertyName) {
return noDotPropertyName
}

noHyphenAndNoDotName := strings.ReplaceAll(noDotPropertyName, "-", "_")
if noDotPropertyName != noHyphenAndNoDotName && propertySource.checkIfPresent(noHyphenAndNoDotName) {
return noHyphenAndNoDotName
}

return nil
}

0 comments on commit 4afc203

Please sign in to comment.