Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
moved download to its own cmd package
Browse files Browse the repository at this point in the history
  • Loading branch information
blankdots committed Nov 2, 2023
1 parent dbf1918 commit 0917cd5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ENV CGO_ENABLED=0

COPY . .

RUN go build -buildvcs=false -o ./sda-download ./cmd
RUN set -ex; for p in cmd/*; do test -d "$p" && go build -buildvcs=false -o "sda-${p#cmd/}" "./$p"; done
RUN echo "nobody:x:65534:65534:nobody:/:/sbin/nologin" > passwd

FROM scratch
Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go → cmd/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func init() {
log.Info("(1/5) Loading configuration")

// Load configuration
conf, err := config.NewConfig()
conf, err := config.NewConfig("download")
if err != nil {
log.Panicf("configuration loading failed, reason: %v", err)
}
Expand Down
80 changes: 47 additions & 33 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ type DatabaseConfig struct {
}

// NewConfig populates ConfigMap with data
func NewConfig() (*Map, error) {
func NewConfig(app string) (*Map, error) {
var requiredConfVars []string

viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.AutomaticEnv()
Expand All @@ -162,21 +164,6 @@ func NewConfig() (*Map, error) {
return nil, err
}
}
requiredConfVars := []string{
"db.host", "db.user", "db.password", "db.database", "c4gh.filepath", "c4gh.passphrase", "oidc.configuration.url",
}

if viper.GetString("archive.type") == S3 {
requiredConfVars = append(requiredConfVars, []string{"archive.url", "archive.accesskey", "archive.secretkey", "archive.bucket"}...)
} else if viper.GetString("archive.type") == POSIX {
requiredConfVars = append(requiredConfVars, []string{"archive.location"}...)
}

for _, s := range requiredConfVars {
if !viper.IsSet(s) || viper.GetString(s) == "" {
return nil, fmt.Errorf("%s not set", s)
}
}

if viper.IsSet("log.format") {
if viper.GetString("log.format") == "json" {
Expand All @@ -196,22 +183,47 @@ func NewConfig() (*Map, error) {
log.Printf("Setting log level to '%s'", stringLevel)
}

c := &Map{}
c.applyDefaults()
c.sessionConfig()
c.configArchive()
err := c.configureOIDC()
if err != nil {
return nil, err
switch app {
case "download":
requiredConfVars = []string{
"db.host", "db.user", "db.password", "db.database", "oidc.configuration.url",
}
default:
requiredConfVars = []string{
"db.host", "db.user", "db.password", "db.database", "c4gh.filepath", "c4gh.passphrase", "oidc.configuration.url",
}
}
err = c.appConfig()
if err != nil {
return nil, err

if viper.GetString("archive.type") == S3 {
requiredConfVars = append(requiredConfVars, []string{"archive.url", "archive.accesskey", "archive.secretkey", "archive.bucket"}...)
} else if viper.GetString("archive.type") == POSIX {
requiredConfVars = append(requiredConfVars, []string{"archive.location"}...)
}

err = c.configDatabase()
if err != nil {
return nil, err
for _, s := range requiredConfVars {
if !viper.IsSet(s) || viper.GetString(s) == "" {
return nil, fmt.Errorf("%s not set", s)
}
}
c := &Map{}
c.applyDefaults()
switch app {
case "download":
c.sessionConfig()
c.configArchive()
err := c.configureOIDC()
if err != nil {
return nil, err
}
err = c.appConfig()
if err != nil {
return nil, err
}

err = c.configDatabase()
if err != nil {
return nil, err
}
}

return c, nil
Expand Down Expand Up @@ -310,10 +322,12 @@ func (c *Map) appConfig() error {
c.App.Port = 443
}

var err error
c.App.Crypt4GHKey, err = GetC4GHKey()
if err != nil {
return err
if viper.IsSet("c4gh.filepath") && viper.IsSet("c4gh.passphrase") {
var err error
c.App.Crypt4GHKey, err = GetC4GHKey()
if err != nil {
return err
}
}

if !slices.Contains(availableMiddlewares, c.App.Middleware) {
Expand Down
4 changes: 2 additions & 2 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestConfigTestSuite(t *testing.T) {

func (suite *TestSuite) TestConfigFile() {
viper.Set("configFile", "test")
config, err := NewConfig()
config, err := NewConfig("download")
assert.Nil(suite.T(), config)
assert.Error(suite.T(), err)
assert.Equal(suite.T(), "test", viper.ConfigFileUsed())
Expand All @@ -52,7 +52,7 @@ func (suite *TestSuite) TestMissingRequiredConfVar() {
requiredConfVarValue := viper.Get(requiredConfVar)
viper.Set(requiredConfVar, nil)
expectedError := fmt.Errorf("%s not set", requiredConfVar)
config, err := NewConfig()
config, err := NewConfig("download")
assert.Nil(suite.T(), config)
if assert.Error(suite.T(), err) {
assert.Equal(suite.T(), expectedError, err)
Expand Down

0 comments on commit 0917cd5

Please sign in to comment.