-
Notifications
You must be signed in to change notification settings - Fork 311
/
config.go
143 lines (120 loc) · 4.11 KB
/
config.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2023 The Okteto Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v2
import (
"os"
"strconv"
oktetoLog "github.com/okteto/okteto/pkg/log"
"github.com/okteto/okteto/pkg/okteto"
"github.com/spf13/afero"
)
const (
// OktetoEnableSmartBuildEnvVar represents whether the feature flag to enable smart builds is enabled or not
OktetoEnableSmartBuildEnvVar = "OKTETO_SMART_BUILDS_ENABLED"
)
type configRepositoryInterface interface {
GetSHA() (string, error)
IsClean() (bool, error)
GetAnonymizedRepo() string
GetLatestDirSHA(string) (string, error)
}
type configRegistryInterface interface {
HasGlobalPushAccess() (bool, error)
}
type oktetoBuilderConfig struct {
repository configRepositoryInterface
fs afero.Fs
hasGlobalAccess bool
isCleanProject bool
isOkteto bool
isSmartBuildsEnable bool
}
type loggerInfo interface {
Infof(format string, args ...interface{})
}
func getConfig(registry configRegistryInterface, gitRepo configRepositoryInterface, l loggerInfo) oktetoBuilderConfig {
hasAccess, err := registry.HasGlobalPushAccess()
if err != nil {
l.Infof("error trying to access globalPushAccess: %s", err)
}
isClean, err := gitRepo.IsClean()
if err != nil {
l.Infof("error trying to get directory: %s", err)
}
return oktetoBuilderConfig{
repository: gitRepo,
hasGlobalAccess: hasAccess,
isCleanProject: isClean,
fs: afero.NewOsFs(),
isOkteto: okteto.IsOkteto(),
isSmartBuildsEnable: getIsSmartBuildEnabled(),
}
}
func getConfigStateless(registry configRegistryInterface, gitRepo configRepositoryInterface, l loggerInfo, isOkteto bool) oktetoBuilderConfig {
hasAccess, err := registry.HasGlobalPushAccess()
if err != nil {
l.Infof("error trying to access globalPushAccess: %s", err)
}
isClean, err := gitRepo.IsClean()
if err != nil {
l.Infof("error trying to get directory: %s", err)
}
return oktetoBuilderConfig{
repository: gitRepo,
hasGlobalAccess: hasAccess,
isCleanProject: isClean,
fs: afero.NewOsFs(),
isOkteto: isOkteto,
isSmartBuildsEnable: getIsSmartBuildEnabled(),
}
}
func getIsSmartBuildEnabled() bool {
enableSmartBuilds := true
enableSmartBuildsStr := os.Getenv(OktetoEnableSmartBuildEnvVar)
if enableSmartBuildsStr != "" {
smartBuildEnabledBool, err := strconv.ParseBool(enableSmartBuildsStr)
if err != nil {
oktetoLog.Warning("feature flag %s received an invalid value; expected boolean. Smart builds will remain enabled by default", OktetoEnableSmartBuildEnvVar)
} else {
enableSmartBuilds = smartBuildEnabledBool
}
}
return enableSmartBuilds
}
// IsOkteto checks if the context is an okteto managed context
func (oc oktetoBuilderConfig) IsOkteto() bool {
return oc.isOkteto
}
// HasGlobalAccess checks if the user has access to global registry
func (oc oktetoBuilderConfig) HasGlobalAccess() bool {
return oc.hasGlobalAccess
}
// IsCleanProject checks if the repository is clean(no changes over the last commit)
func (oc oktetoBuilderConfig) IsCleanProject() bool {
return oc.isCleanProject
}
// GetGitCommit returns the commit sha of the repository
func (oc oktetoBuilderConfig) GetGitCommit() string {
commitSHA, err := oc.repository.GetSHA()
if err != nil {
oktetoLog.Infof("could not get repository sha: %s", err)
}
return commitSHA
}
// GetAnonymizedRepo returns the repository url without credentials
func (oc oktetoBuilderConfig) GetAnonymizedRepo() string {
return oc.repository.GetAnonymizedRepo()
}
func (oc oktetoBuilderConfig) IsSmartBuildsEnabled() bool {
return oc.isSmartBuildsEnable
}