aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/core/Platform.scala
blob: aa7e7113289b5a9eddae2e96d31b501576ecdda0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package xyz.driver.core
import java.nio.file.{Files, Path, Paths}

import com.google.auth.oauth2.ServiceAccountCredentials

sealed trait Platform {
  def isKubernetes: Boolean
}

object Platform {
  case class GoogleCloud(keyfile: Path, namespace: String) extends Platform {
    def credentials: ServiceAccountCredentials = ServiceAccountCredentials.fromStream(
      Files.newInputStream(keyfile)
    )
    def project: String       = credentials.getProjectId
    override def isKubernetes = true
  }
  // case object AliCloud   extends Platform
  case object Dev extends Platform {
    override def isKubernetes: Boolean = false
  }

  lazy val fromEnv: Platform = {
    def isGoogle = sys.env.get("GOOGLE_APPLICATION_CREDENTIALS").map { value =>
      val keyfile = Paths.get(value)
      require(Files.isReadable(keyfile), s"Google credentials file $value is not readable.")
      val namespace = sys.env.getOrElse("SERVICE_NAMESPACE", sys.error("Namespace not set"))
      GoogleCloud(keyfile, namespace)
    }
    isGoogle.getOrElse(Dev)
  }

  def current: Platform = fromEnv

}