Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ module github.com/ovh/go-ovh

go 1.18

require gopkg.in/ini.v1 v1.67.0
require (
github.com/jarcoal/httpmock v1.3.0
github.com/maxatome/go-testdeep v1.12.0
gopkg.in/ini.v1 v1.67.0
)

require github.com/stretchr/testify v1.8.2 // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/stretchr/testify v1.8.2 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jarcoal/httpmock v1.3.0 h1:2RJ8GP0IIaWwcC9Fp2BmVi8Kog3v2Hn7VXM3fTd+nuc=
github.com/jarcoal/httpmock v1.3.0/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g=
github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
69 changes: 48 additions & 21 deletions ovh/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import (
"fmt"
"os"
"os/user"
"path/filepath"
"strings"

"gopkg.in/ini.v1"
)

// Use variables for easier test overload
var (
systemConfigPath = "/etc/ovh.conf"
userConfigPath = "/.ovh.conf" // prefixed with homeDir
localConfigPath = "./ovh.conf"
)
var configPaths = []string{
// System wide configuration
"/etc/ovh.com",
// Configuration in user's home
"~/.ovh.conf",
// Configuration in local folder
"./ovh.conf",
}

// currentUserHome attempts to get current user's home directory
// currentUserHome attempts to get current user's home directory.
func currentUserHome() (string, error) {
usr, err := user.Current()
if err != nil {
Expand All @@ -31,14 +32,43 @@ func currentUserHome() (string, error) {
return usr.HomeDir, nil
}

// appendConfigurationFile only if it exists. We need to do this because
// ini package will fail to load configuration at all if a configuration
// file is missing. This is racy, but better than always failing.
func appendConfigurationFile(cfg *ini.File, path string) {
if file, err := os.Open(path); err == nil {
defer file.Close()
_ = cfg.Append(path)
// configPaths returns configPaths, with ~/ prefix expanded.
func expandConfigPaths() []interface{} {
paths := []interface{}{}

// Will be initialized on first use
var home string
var homeErr error

for _, path := range configPaths {
if strings.HasPrefix(path, "~/") {
// Find home if needed
if home == "" && homeErr == nil {
home, homeErr = currentUserHome()
}
// Ignore file in HOME if we cannot find it
if homeErr != nil {
continue
}

path = home + path[1:]
}

paths = append(paths, path)
}

return paths
}

// loadINI builds a ini.File from the configuration paths provided in configPaths.
// It's a helper for loadConfig.
func loadINI() (*ini.File, error) {
paths := expandConfigPaths()
if len(paths) == 0 {
return ini.Empty(), nil
}

return ini.LooseLoad(paths[0], paths[1:]...)
}

// loadConfig loads client configuration from params, environments or configuration
Expand All @@ -58,13 +88,10 @@ func appendConfigurationFile(cfg *ini.File, path string) {
func (c *Client) loadConfig(endpointName string) error {
// Load configuration files by order of increasing priority. All configuration
// files are optional. Only load file from user home if home could be resolve
cfg := ini.Empty()
appendConfigurationFile(cfg, systemConfigPath)
if home, err := currentUserHome(); err == nil {
userConfigFullPath := filepath.Join(home, userConfigPath)
appendConfigurationFile(cfg, userConfigFullPath)
cfg, err := loadINI()
if err != nil {
return fmt.Errorf("cannot load configuration: %w", err)
}
appendConfigurationFile(cfg, localConfigPath)

// Canonicalize configuration
if endpointName == "" {
Expand Down
Loading