constructor.go 890 B

12345678910111213141516171819202122232425262728293031323334
  1. package sdk
  2. import (
  3. "fmt"
  4. "os"
  5. "notabug.org/Umnik/GoAndroidSDK/v2/components/misc"
  6. )
  7. // NewSDKFromPath creates SDK from path to dir
  8. func NewSDKFromPath(dirPath string) (*SDK, error) {
  9. if err := misc.TestDir(dirPath); err != nil {
  10. return nil, err
  11. } else {
  12. return &SDK{dirPath: dirPath}, nil
  13. }
  14. }
  15. // NewSDKFromEnv creates SDK from environment variable. For example: ${ANDROID_SDK_ROOT}
  16. func NewSDKFromEnv(env string) (*SDK, error) {
  17. return NewSDKFromPath(os.ExpandEnv(env))
  18. }
  19. // NewSDK creates SDK from ANDROID_SDK_ROOT and ANDROID_HOME environment variables if it exists
  20. func NewSDK() (*SDK, error) {
  21. environments := []string{"ANDROID_HOME", "ANDROID_SDK_ROOT"}
  22. for _, val := range environments {
  23. if key, ok := os.LookupEnv(val); ok {
  24. return NewSDKFromEnv(key)
  25. }
  26. }
  27. return nil, fmt.Errorf("following environments was checked: %q. They are not set", environments)
  28. }