| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package bootstrap | 5 package bootstrap |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "errors" | 8 "errors" |
| 9 "fmt" | 9 "fmt" |
| 10 | 10 |
| 11 "github.com/luci/luci-go/client/environ" | 11 "github.com/luci/luci-go/client/environ" |
| 12 "github.com/luci/luci-go/client/logdog/butlerlib/streamclient" | 12 "github.com/luci/luci-go/client/logdog/butlerlib/streamclient" |
| 13 "github.com/luci/luci-go/common/config" |
| 13 "github.com/luci/luci-go/common/logdog/types" | 14 "github.com/luci/luci-go/common/logdog/types" |
| 14 ) | 15 ) |
| 15 | 16 |
| 16 // ErrNotBootstrapped is returned by Get when the current process is not | 17 // ErrNotBootstrapped is returned by Get when the current process is not |
| 17 // bootstrapped. | 18 // bootstrapped. |
| 18 var ErrNotBootstrapped = errors.New("not bootstrapped") | 19 var ErrNotBootstrapped = errors.New("not bootstrapped") |
| 19 | 20 |
| 20 // Bootstrap contains information about the | 21 // Bootstrap contains information about the |
| 21 type Bootstrap struct { | 22 type Bootstrap struct { |
| 23 // Project is the Butler instance project name. |
| 24 Project config.ProjectName |
| 22 // Prefix is the Butler instance prefix. | 25 // Prefix is the Butler instance prefix. |
| 23 Prefix types.StreamName | 26 Prefix types.StreamName |
| 24 | 27 |
| 25 // Client is the streamclient for this instance, or nil if the Butler ha
s no | 28 // Client is the streamclient for this instance, or nil if the Butler ha
s no |
| 26 // streamserver. | 29 // streamserver. |
| 27 Client streamclient.Client | 30 Client streamclient.Client |
| 28 } | 31 } |
| 29 | 32 |
| 30 func getFromEnv(env environ.Environment, reg *streamclient.Registry) (*Bootstrap
, error) { | 33 func getFromEnv(env environ.Environment, reg *streamclient.Registry) (*Bootstrap
, error) { |
| 31 // Detect Butler by looking for EnvStreamPrefix in the envrironent. | 34 // Detect Butler by looking for EnvStreamPrefix in the envrironent. |
| 32 prefix, ok := env[EnvStreamPrefix] | 35 prefix, ok := env[EnvStreamPrefix] |
| 33 if !ok { | 36 if !ok { |
| 34 return nil, ErrNotBootstrapped | 37 return nil, ErrNotBootstrapped |
| 35 } | 38 } |
| 36 | 39 |
| 37 bs := &Bootstrap{ | 40 bs := &Bootstrap{ |
| 38 » » Prefix: types.StreamName(prefix), | 41 » » Prefix: types.StreamName(prefix), |
| 42 » » Project: config.ProjectName(env[EnvStreamProject]), |
| 39 } | 43 } |
| 40 if err := bs.Prefix.Validate(); err != nil { | 44 if err := bs.Prefix.Validate(); err != nil { |
| 41 » » return nil, fmt.Errorf("bootstrap: failed to validate prefix [%s
]: %s", prefix, err) | 45 » » return nil, fmt.Errorf("bootstrap: failed to validate prefix %q:
%s", prefix, err) |
| 46 » } |
| 47 » if err := bs.Project.Validate(); err != nil { |
| 48 » » return nil, fmt.Errorf("bootstrap: failed to validate project %q
: %s", bs.Project, err) |
| 42 } | 49 } |
| 43 | 50 |
| 44 // If we have a stream server attached; instantiate a stream Client. | 51 // If we have a stream server attached; instantiate a stream Client. |
| 45 if p, ok := env[EnvStreamServerPath]; ok { | 52 if p, ok := env[EnvStreamServerPath]; ok { |
| 46 c, err := reg.NewClient(p) | 53 c, err := reg.NewClient(p) |
| 47 if err != nil { | 54 if err != nil { |
| 48 return nil, fmt.Errorf("bootstrap: failed to create stre
am client [%s]: %s", p, err) | 55 return nil, fmt.Errorf("bootstrap: failed to create stre
am client [%s]: %s", p, err) |
| 49 } | 56 } |
| 50 bs.Client = c | 57 bs.Client = c |
| 51 } | 58 } |
| 52 | 59 |
| 53 return bs, nil | 60 return bs, nil |
| 54 } | 61 } |
| 55 | 62 |
| 56 // Get loads a Bootstrap instance from the environment. It will return an error | 63 // Get loads a Bootstrap instance from the environment. It will return an error |
| 57 // if the bootstrap data is invalid, and will return ErrNotBootstrapped if the | 64 // if the bootstrap data is invalid, and will return ErrNotBootstrapped if the |
| 58 // current process is not bootstrapped. | 65 // current process is not bootstrapped. |
| 59 func Get() (*Bootstrap, error) { | 66 func Get() (*Bootstrap, error) { |
| 60 return getFromEnv(environ.Get(), streamclient.DefaultRegistry) | 67 return getFromEnv(environ.Get(), streamclient.DefaultRegistry) |
| 61 } | 68 } |
| OLD | NEW |