mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat(agent/agentcontainers): add feature options as envs (#18576)
This commit is contained in:
@@ -6,7 +6,10 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
@@ -26,12 +29,55 @@ type DevcontainerConfig struct {
|
||||
|
||||
type DevcontainerMergedConfiguration struct {
|
||||
Customizations DevcontainerMergedCustomizations `json:"customizations,omitempty"`
|
||||
Features DevcontainerFeatures `json:"features,omitempty"`
|
||||
}
|
||||
|
||||
type DevcontainerMergedCustomizations struct {
|
||||
Coder []CoderCustomization `json:"coder,omitempty"`
|
||||
}
|
||||
|
||||
type DevcontainerFeatures map[string]any
|
||||
|
||||
// OptionsAsEnvs converts the DevcontainerFeatures into a list of
|
||||
// environment variables that can be used to set feature options.
|
||||
// The format is FEATURE_<FEATURE_NAME>_OPTION_<OPTION_NAME>=<value>.
|
||||
// For example, if the feature is:
|
||||
//
|
||||
// "ghcr.io/coder/devcontainer-features/code-server:1": {
|
||||
// "port": 9090,
|
||||
// }
|
||||
//
|
||||
// It will produce:
|
||||
//
|
||||
// FEATURE_CODE_SERVER_OPTION_PORT=9090
|
||||
//
|
||||
// Note that the feature name is derived from the last part of the key,
|
||||
// so "ghcr.io/coder/devcontainer-features/code-server:1" becomes
|
||||
// "CODE_SERVER". The version part (e.g. ":1") is removed, and dashes in
|
||||
// the feature and option names are replaced with underscores.
|
||||
func (f DevcontainerFeatures) OptionsAsEnvs() []string {
|
||||
var env []string
|
||||
for k, v := range f {
|
||||
vv, ok := v.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
// Take the last part of the key as the feature name/path.
|
||||
k = k[strings.LastIndex(k, "/")+1:]
|
||||
// Remove ":" and anything following it.
|
||||
if idx := strings.Index(k, ":"); idx != -1 {
|
||||
k = k[:idx]
|
||||
}
|
||||
k = strings.ReplaceAll(k, "-", "_")
|
||||
for k2, v2 := range vv {
|
||||
k2 = strings.ReplaceAll(k2, "-", "_")
|
||||
env = append(env, fmt.Sprintf("FEATURE_%s_OPTION_%s=%s", strings.ToUpper(k), strings.ToUpper(k2), fmt.Sprintf("%v", v2)))
|
||||
}
|
||||
}
|
||||
slices.Sort(env)
|
||||
return env
|
||||
}
|
||||
|
||||
type DevcontainerConfiguration struct {
|
||||
Customizations DevcontainerCustomizations `json:"customizations,omitempty"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user