Skip to content

Commit

Permalink
refactor: extract config checks
Browse files Browse the repository at this point in the history
  • Loading branch information
c100k committed Mar 21, 2024
1 parent 61e9ca6 commit 31ad501
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions impl/http-server-go/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,39 @@ func getConfig() *Config {
sysCmdStop: getEnvOr("SYS_CMD_STOP", "shutdown"),
}

assertServiceImpl(config)
assertServiceImplFileJson(config)
assertSysCmdPkg(config)

return &config
}

func assertServiceImpl(config Config) {
if config.serviceImpl != "fileJson" && config.serviceImpl != "noop" && config.serviceImpl != "self" {
panic(fmt.Sprintf("Valid values for serviceImpl are : 'fileJson' and 'noop' and 'self'. Got '%s'", config.serviceImpl))
}
}

func assertServiceImplFileJson(config Config) {
if config.serviceImpl != "fileJson" {
return
}

if config.serviceFileJsonFilePath == nil {
panic(fmt.Sprintf("You must provide a json file path when serviceImpl is 'fileJson'"))
}

if config.serviceImpl == "fileJson" {
if config.serviceFileJsonFilePath == nil {
panic(fmt.Sprintf("You must provide a json file path when serviceImpl is 'fileJson'"))
} else {
path := *config.serviceFileJsonFilePath
_, err := os.Stat(path)
if err != nil {
panic(fmt.Sprintf("The file %s does not exist", path))
}
}
path := *config.serviceFileJsonFilePath
_, err := os.Stat(path)
if err != nil {
panic(fmt.Sprintf("The file %s does not exist", path))
}
}

func assertSysCmdPkg(config Config) {
if config.sysCmdPkg != "exec" && config.sysCmdPkg != "syscall" {
panic(fmt.Sprintf("Valid values for sysCmdPkg are : 'exec' and 'syscall'. Got '%s'", config.sysCmdPkg))
}

return &config
}

func envName(key string) string {
Expand Down

0 comments on commit 31ad501

Please sign in to comment.