Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/local-app/pkg/config/context.go
2500 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package config
6
7
import (
8
"fmt"
9
"net/url"
10
11
"github.com/gitpod-io/local-app/pkg/prettyprint"
12
"gopkg.in/yaml.v3"
13
)
14
15
func (c *Config) GetActiveContext() (*ConnectionContext, error) {
16
if c == nil {
17
return nil, ErrNoContext
18
}
19
if c.ActiveContext == "" {
20
return nil, ErrNoContext
21
}
22
res := c.Contexts[c.ActiveContext]
23
if res == nil {
24
return nil, ErrNoContext
25
}
26
return res, nil
27
}
28
29
var ErrNoContext = prettyprint.AddResolution(fmt.Errorf("no active context"),
30
"sign in using `{gitpod} login`",
31
"select an existing context using `{gitpod} config use-context`",
32
"create a new context using `{gitpod} config add-context`",
33
)
34
35
type ConnectionContext struct {
36
Host *YamlURL `yaml:"host"`
37
OrganizationID string `yaml:"organizationID,omitempty"`
38
Token string `yaml:"token,omitempty"`
39
}
40
41
type YamlURL struct {
42
*url.URL
43
}
44
45
// UnmarshalYAML implements yaml.Unmarshaler
46
func (u *YamlURL) UnmarshalYAML(value *yaml.Node) error {
47
var s string
48
err := value.Decode(&s)
49
if err != nil {
50
return err
51
}
52
53
res, err := url.Parse(s)
54
if err != nil {
55
return err
56
}
57
58
*u = YamlURL{URL: res}
59
return nil
60
}
61
62
// MarshalYAML implements yaml.Marshaler
63
func (u *YamlURL) MarshalYAML() (interface{}, error) {
64
return u.String(), nil
65
}
66
67