-
Notifications
You must be signed in to change notification settings - Fork 543
Expand file tree
/
Copy pathconfig.go
More file actions
148 lines (130 loc) · 4.98 KB
/
config.go
File metadata and controls
148 lines (130 loc) · 4.98 KB
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
144
145
146
147
148
package devcontainer
import (
"os"
"path"
"path/filepath"
"github.com/loft-sh/devpod/pkg/devcontainer/config"
"github.com/loft-sh/devpod/pkg/devcontainer/crane"
"github.com/loft-sh/devpod/pkg/language"
provider2 "github.com/loft-sh/devpod/pkg/provider"
"github.com/pkg/errors"
)
func (r *runner) getRawConfig(options provider2.CLIOptions) (*config.DevContainerConfig, error) {
if r.WorkspaceConfig.Workspace.DevContainerConfig != nil {
rawParsedConfig := config.CloneDevContainerConfig(r.WorkspaceConfig.Workspace.DevContainerConfig)
if r.WorkspaceConfig.Workspace.DevContainerPath != "" {
rawParsedConfig.Origin = path.Join(filepath.ToSlash(r.LocalWorkspaceFolder), r.WorkspaceConfig.Workspace.DevContainerPath)
} else {
rawParsedConfig.Origin = path.Join(filepath.ToSlash(r.LocalWorkspaceFolder), ".devcontainer.devpod.json")
}
return rawParsedConfig, nil
} else if r.WorkspaceConfig.Workspace.Source.Container != "" {
return &config.DevContainerConfig{
DevContainerConfigBase: config.DevContainerConfigBase{
// Default workspace directory for containers
// Upon inspecting the container, this would be updated to the correct folder, if found set
WorkspaceFolder: "/",
},
RunningContainer: config.RunningContainer{
ContainerID: r.WorkspaceConfig.Workspace.Source.Container,
},
Origin: "",
}, nil
} else if crane.ShouldUse(&options) {
localWorkspaceFolder, err := crane.PullConfigFromSource(r.WorkspaceConfig, &options, r.Log)
if err != nil {
return nil, err
}
return config.ParseDevContainerJSON(
localWorkspaceFolder,
r.WorkspaceConfig.Workspace.DevContainerPath,
)
}
localWorkspaceFolder := r.LocalWorkspaceFolder
// if a subpath is specified, let's move to it
if r.WorkspaceConfig.Workspace.Source.GitSubPath != "" {
localWorkspaceFolder = filepath.Join(r.LocalWorkspaceFolder, r.WorkspaceConfig.Workspace.Source.GitSubPath)
}
// parse the devcontainer json
rawParsedConfig, err := config.ParseDevContainerJSON(
localWorkspaceFolder,
r.WorkspaceConfig.Workspace.DevContainerPath,
)
// We want to fail only in case of real errors, non-existing devcontainer.jon
// will be gracefully handled by the auto-detection mechanism
if err != nil && !os.IsNotExist(err) {
return nil, errors.Wrap(err, "parsing devcontainer.json")
} else if rawParsedConfig == nil {
r.Log.Infof("Couldn't find a devcontainer.json")
return r.getDefaultConfig(options)
}
return rawParsedConfig, nil
}
func (r *runner) getDefaultConfig(options provider2.CLIOptions) (*config.DevContainerConfig, error) {
defaultConfig := &config.DevContainerConfig{}
if options.FallbackImage != "" {
r.Log.Infof("Using fallback image %s", options.FallbackImage)
defaultConfig.ImageContainer = config.ImageContainer{
Image: options.FallbackImage,
}
} else {
r.Log.Infof("Try detecting project programming language...")
defaultConfig = language.DefaultConfig(r.LocalWorkspaceFolder, r.Log)
}
defaultConfig.Origin = path.Join(filepath.ToSlash(r.LocalWorkspaceFolder), ".devcontainer.json")
err := config.SaveDevContainerJSON(defaultConfig)
if err != nil {
return nil, errors.Wrap(err, "write default devcontainer.json")
}
return defaultConfig, nil
}
func (r *runner) getSubstitutedConfig(options provider2.CLIOptions) (*config.SubstitutedConfig, *config.SubstitutionContext, error) {
rawConfig, err := r.getRawConfig(options)
if err != nil {
return nil, nil, err
}
return r.substitute(options, rawConfig)
}
func (r *runner) substitute(
options provider2.CLIOptions,
rawParsedConfig *config.DevContainerConfig,
) (*config.SubstitutedConfig, *config.SubstitutionContext, error) {
configFile := rawParsedConfig.Origin
// get workspace folder within container
workspaceMount, containerWorkspaceFolder := getWorkspace(
r.LocalWorkspaceFolder,
r.WorkspaceConfig.Workspace.ID,
rawParsedConfig,
)
substitutionContext := &config.SubstitutionContext{
DevContainerID: r.ID,
LocalWorkspaceFolder: r.LocalWorkspaceFolder,
ContainerWorkspaceFolder: containerWorkspaceFolder,
Env: config.ListToObject(os.Environ()),
WorkspaceMount: workspaceMount,
}
// substitute & load
parsedConfig := &config.DevContainerConfig{}
err := config.Substitute(substitutionContext, rawParsedConfig, parsedConfig)
if err != nil {
return nil, nil, err
}
if parsedConfig.WorkspaceFolder != "" {
substitutionContext.ContainerWorkspaceFolder = parsedConfig.WorkspaceFolder
}
if parsedConfig.WorkspaceMount != "" {
substitutionContext.WorkspaceMount = parsedConfig.WorkspaceMount
}
if options.DevContainerImage != "" {
parsedConfig.Build = nil
parsedConfig.Dockerfile = ""
parsedConfig.DockerfileContainer = config.DockerfileContainer{}
parsedConfig.ImageContainer = config.ImageContainer{Image: options.DevContainerImage}
}
substitutionContext.DisableSELinuxFlag = options.DisableSELinuxFlag
parsedConfig.Origin = configFile
return &config.SubstitutedConfig{
Config: parsedConfig,
Raw: rawParsedConfig,
}, substitutionContext, nil
}