-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Load user regions * cleanup * lint * pluralize * add explicit None region metadata * Apply suggestions from code review Co-authored-by: Alex Plischke <alex.plischke@saucelabs.com> * break if lookup is successful * Log error if regions file could not be parsed --------- Co-authored-by: Alex Plischke <alex.plischke@saucelabs.com>
- Loading branch information
1 parent
f6b25b7
commit 0a19867
Showing
1 changed file
with
83 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,136 @@ | ||
package region | ||
|
||
// Region represents the sauce labs region. | ||
type Region uint | ||
|
||
const ( | ||
// None is an undefined sauce labs region. | ||
None Region = iota | ||
// USWest1 is a sauce labs region in western US, aka us-west-1. | ||
USWest1 | ||
// USEast4 is a sauce labs region in eastern US, aka us-east-4. | ||
USEast4 | ||
// EUCentral1 is a sauce labs region in the EU, aka eu-central-1. | ||
EUCentral1 | ||
// Staging is a sauce labs internal pre-production environment. | ||
Staging | ||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/rs/zerolog/log" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
var meta = []struct { | ||
Name string | ||
APIBaseURL string | ||
AppBaseURL string | ||
WebDriverBaseURL string | ||
}{ | ||
// None | ||
func init() { | ||
homeDir, _ := os.UserHomeDir() | ||
path := filepath.Join(homeDir, ".sauce", "regions.yml") | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return | ||
} | ||
defer file.Close() | ||
|
||
if err = yaml.NewDecoder(file).Decode(&userRegionMetas); err != nil { | ||
log.Error().Msgf("failed to parse regions file (%s): %v", path, err) | ||
return | ||
} | ||
} | ||
|
||
// Region represents the sauce labs region. | ||
type Region string | ||
|
||
type regionMeta struct { | ||
Name string `yaml:"name"` | ||
APIBaseURL string `yaml:"apiBaseURL"` | ||
AppBaseURL string `yaml:"appBaseURL"` | ||
WebdriverBaseURL string `yaml:"webdriverBaseURL"` | ||
} | ||
|
||
// None is an undefined sauce labs region. | ||
const None Region = "" | ||
|
||
// USWest1 is a sauce labs region in western US, aka us-west-1. | ||
const USWest1 Region = "us-west-1" | ||
|
||
// USEast4 is a sauce labs region in eastern US, aka us-east-4. | ||
const USEast4 Region = "us-east-4" | ||
|
||
// EUCentral1 is a sauce labs region in the EU, aka eu-central-1. | ||
const EUCentral1 Region = "eu-central-1" | ||
|
||
// Staging is a sauce labs internal pre-production environment. | ||
const Staging Region = "staging" | ||
|
||
var sauceRegionMetas = []regionMeta{ | ||
{ | ||
"", | ||
None.String(), | ||
"", | ||
"", | ||
"", | ||
}, | ||
// USWest1 | ||
{ | ||
"us-west-1", | ||
USWest1.String(), | ||
"https://api.us-west-1.saucelabs.com", | ||
"https://app.saucelabs.com", | ||
"https://ondemand.us-west-1.saucelabs.com", | ||
}, | ||
// USEast4 | ||
{ | ||
"us-east-4", | ||
USEast4.String(), | ||
"https://api.us-east-4.saucelabs.com", | ||
"https://app.us-east-4.saucelabs.com", | ||
"https://ondemand.us-east-4.saucelabs.com", | ||
}, | ||
// EUCentral1 | ||
{ | ||
"eu-central-1", | ||
EUCentral1.String(), | ||
"https://api.eu-central-1.saucelabs.com", | ||
"https://app.eu-central-1.saucelabs.com", | ||
"https://ondemand.eu-central-1.saucelabs.com", | ||
}, | ||
// Staging | ||
{ | ||
"staging", | ||
Staging.String(), | ||
"https://api.staging.saucelabs.net", | ||
"https://app.staging.saucelabs.net", | ||
"https://ondemand.staging.saucelabs.net", | ||
}, | ||
} | ||
|
||
// userRegionMetas is a list of user defined regions that is loaded | ||
// from the user's ~/.sauce directory. | ||
var userRegionMetas []regionMeta | ||
|
||
// allRegionMetas concats the list of known Sauce region metadata and the user's | ||
// list of region metadata. | ||
func allRegionMetas() []regionMeta { | ||
return append(sauceRegionMetas, userRegionMetas...) | ||
} | ||
|
||
func (r Region) String() string { | ||
return meta[r].Name | ||
return string(r) | ||
} | ||
|
||
// FromString converts the given string to the corresponding Region. | ||
// Returns None if the string did not match any Region. | ||
func FromString(s string) Region { | ||
for i, m := range meta { | ||
if m.Name == s { | ||
return Region(i) | ||
for _, m := range allRegionMetas() { | ||
if s == m.Name { | ||
return Region(m.Name) | ||
} | ||
} | ||
|
||
return None | ||
} | ||
|
||
func lookupMeta(r Region) regionMeta { | ||
var found regionMeta | ||
for _, m := range allRegionMetas() { | ||
if m.Name == string(r) { | ||
found = m | ||
break | ||
} | ||
} | ||
return found | ||
} | ||
|
||
// APIBaseURL returns the API base URL for the region. | ||
func (r Region) APIBaseURL() string { | ||
return meta[r].APIBaseURL | ||
meta := lookupMeta(r) | ||
return meta.APIBaseURL | ||
} | ||
|
||
// AppBaseURL returns the Aapp base URL for the region. | ||
func (r Region) AppBaseURL() string { | ||
return meta[r].AppBaseURL | ||
meta := lookupMeta(r) | ||
return meta.AppBaseURL | ||
} | ||
|
||
// WebDriverBaseURL returns the webdriver base URL for the region. | ||
func (r Region) WebDriverBaseURL() string { | ||
return meta[r].WebDriverBaseURL | ||
meta := lookupMeta(r) | ||
return meta.WebdriverBaseURL | ||
} |