Skip to content

Commit

Permalink
move client_configuration_finder to client_configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Oct 19, 2023
1 parent 6409206 commit 6326fa9
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 236 deletions.
63 changes: 63 additions & 0 deletions client_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"os"
"path"
"strings"
)

Expand All @@ -20,6 +21,68 @@ const (
levelTrace string = "TRACE" // trace log level
)

const (
defaultConfigName = "sf_client_config.json"
clientConfEnvName = "SF_CLIENT_CONFIG_FILE"
)

func getClientConfig(filePathFromConnectionString string) (*ClientConfig, error) {
configPredefinedFilePaths, err := clientConfigPredefinedDirs()

Check failure on line 30 in client_configuration.go

View workflow job for this annotation

GitHub Actions / AWS Go 1.19 on Mac

this value of err is never used (SA4006)

Check failure on line 30 in client_configuration.go

View workflow job for this annotation

GitHub Actions / GCP Go 1.19 on Ubuntu

this value of err is never used (SA4006)

Check failure on line 30 in client_configuration.go

View workflow job for this annotation

GitHub Actions / AZURE Go 1.19 on Mac

this value of err is never used (SA4006)

Check failure on line 30 in client_configuration.go

View workflow job for this annotation

GitHub Actions / AZURE Go 1.19 on Ubuntu

this value of err is never used (SA4006)

Check failure on line 30 in client_configuration.go

View workflow job for this annotation

GitHub Actions / GCP Go 1.19 on Mac

this value of err is never used (SA4006)

Check failure on line 30 in client_configuration.go

View workflow job for this annotation

GitHub Actions / AWS Go 1.19 on Ubuntu

this value of err is never used (SA4006)

Check failure on line 30 in client_configuration.go

View workflow job for this annotation

GitHub Actions / GCP Go 1.20 on Ubuntu

this value of err is never used (SA4006)

Check failure on line 30 in client_configuration.go

View workflow job for this annotation

GitHub Actions / AZURE Go 1.20 on Ubuntu

this value of err is never used (SA4006)

Check failure on line 30 in client_configuration.go

View workflow job for this annotation

GitHub Actions / AWS Go 1.20 on Ubuntu

this value of err is never used (SA4006)

Check failure on line 30 in client_configuration.go

View workflow job for this annotation

GitHub Actions / AZURE Go 1.20 on Mac

this value of err is never used (SA4006)

Check failure on line 30 in client_configuration.go

View workflow job for this annotation

GitHub Actions / GCP Go 1.20 on Mac

this value of err is never used (SA4006)

Check failure on line 30 in client_configuration.go

View workflow job for this annotation

GitHub Actions / AWS Go 1.20 on Mac

this value of err is never used (SA4006)
var filePath string
filePath, err = findClientConfigFilePath(filePathFromConnectionString, configPredefinedFilePaths)
if err != nil {
return nil, err
}

Check warning on line 35 in client_configuration.go

View check run for this annotation

Codecov / codecov/patch

client_configuration.go#L34-L35

Added lines #L34 - L35 were not covered by tests
if filePath == "" { // we did not find a config file
return nil, nil
}
return parseClientConfiguration(filePath)
}

func findClientConfigFilePath(filePathFromConnectionString string, configPredefinedDirs []string) (string, error) {
if filePathFromConnectionString != "" {
return filePathFromConnectionString, nil
}
envConfigFilePath := os.Getenv(clientConfEnvName)
if envConfigFilePath != "" {
return envConfigFilePath, nil
}
return searchForConfigFile(configPredefinedDirs)
}

func searchForConfigFile(directories []string) (string, error) {
for _, dir := range directories {
filePath := path.Join(dir, defaultConfigName)
exists, err := existsFile(filePath)
if err != nil {
return "", err
}

Check warning on line 59 in client_configuration.go

View check run for this annotation

Codecov / codecov/patch

client_configuration.go#L58-L59

Added lines #L58 - L59 were not covered by tests
if exists {
return filePath, nil
}
}
return "", nil
}

func existsFile(filePath string) (bool, error) {
_, err := os.Stat(filePath)
if err == nil {
return true, nil
}
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, err

Check warning on line 75 in client_configuration.go

View check run for this annotation

Codecov / codecov/patch

client_configuration.go#L75

Added line #L75 was not covered by tests
}

func clientConfigPredefinedDirs() ([]string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}

Check warning on line 82 in client_configuration.go

View check run for this annotation

Codecov / codecov/patch

client_configuration.go#L81-L82

Added lines #L81 - L82 were not covered by tests
return []string{".", homeDir, os.TempDir()}, nil
}

// ClientConfig config root
type ClientConfig struct {
Common *ClientConfigCommonProps `json:"common"`
Expand Down
71 changes: 0 additions & 71 deletions client_configuration_finder.go

This file was deleted.

165 changes: 0 additions & 165 deletions client_configuration_finder_test.go

This file was deleted.

Loading

0 comments on commit 6326fa9

Please sign in to comment.