aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/yandex/tvm/tvmtool/any.go
blob: 5c394af771fd61c3ebc804ba7d2f59b654cabdcf (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
36
37
package tvmtool

import (
	"os"

	"a.yandex-team.ru/library/go/core/xerrors"
)

const (
	LocalEndpointEnvKey = "TVMTOOL_URL"
	LocalTokenEnvKey    = "TVMTOOL_LOCAL_AUTHTOKEN"
)

var ErrUnknownTvmtoolEnvironment = xerrors.NewSentinel("unknown tvmtool environment")

// NewAnyClient method creates a new tvmtool client with environment auto-detection.
// You must reuse it to prevent connection/goroutines leakage.
func NewAnyClient(opts ...Option) (*Client, error) {
	switch {
	case os.Getenv(QloudEndpointEnvKey) != "":
		// it's Qloud
		return NewQloudClient(opts...)
	case os.Getenv(DeployEndpointEnvKey) != "":
		// it's Y.Deploy
		return NewDeployClient(opts...)
	case os.Getenv(LocalEndpointEnvKey) != "":
		passedOpts := append(
			[]Option{
				WithAuthToken(os.Getenv(LocalTokenEnvKey)),
			},
			opts...,
		)
		return NewClient(os.Getenv(LocalEndpointEnvKey), passedOpts...)
	default:
		return nil, ErrUnknownTvmtoolEnvironment.WithFrame()
	}
}