MCPcopy Index your code
hub / github.com/coder/coder / updateCommandEnv

Method updateCommandEnv

agent/agent.go:1539–1630  ·  view source on GitHub ↗

updateCommandEnv updates the provided command environment with the following set of environment variables: - Predefined workspace environment variables - Environment variables currently set (overriding predefined) - Environment variables passed via the agent manifest (overriding predefined and curre

(current []string)

Source from the content-addressed store, hash-verified

1537// - User secret variables passed via the agent manifest (overriding predefined, current, and manifest env vars)
1538// - Agent-level environment variables (overriding all)
1539func (a *agent) updateCommandEnv(current []string) (updated []string, err error) {
1540 manifest := a.manifest.Load()
1541 if manifest == nil {
1542 return nil, xerrors.Errorf("no manifest")
1543 }
1544
1545 executablePath, err := os.Executable()
1546 if err != nil {
1547 return nil, xerrors.Errorf("getting os executable: %w", err)
1548 }
1549 unixExecutablePath := strings.ReplaceAll(executablePath, "\\", "/")
1550
1551 // Define environment variables that should be set for all commands,
1552 // and then merge them with the current environment.
1553 envs := map[string]string{
1554 // Set env vars indicating we're inside a Coder workspace.
1555 "CODER": "true",
1556 "CODER_WORKSPACE_NAME": manifest.WorkspaceName,
1557 "CODER_WORKSPACE_AGENT_NAME": manifest.AgentName,
1558 "CODER_WORKSPACE_OWNER_NAME": manifest.OwnerName,
1559 "CODER_WORKSPACE_ID": manifest.WorkspaceID.String(),
1560
1561 // Specific Coder subcommands require the agent token exposed!
1562 "CODER_AGENT_TOKEN": a.client.GetSessionToken(),
1563
1564 // Git on Windows resolves with UNIX-style paths.
1565 // If using backslashes, it's unable to find the executable.
1566 "GIT_SSH_COMMAND": fmt.Sprintf("%s gitssh --", unixExecutablePath),
1567 // Hide Coder message on code-server's "Getting Started" page
1568 "CS_DISABLE_GETTING_STARTED_OVERRIDE": "true",
1569 }
1570
1571 // This adds the ports dialog to code-server that enables
1572 // proxying a port dynamically.
1573 // If this is empty string, do not set anything. Code-server auto defaults
1574 // using its basepath to construct a path based port proxy.
1575 if manifest.VSCodePortProxyURI != "" {
1576 envs["VSCODE_PROXY_URI"] = manifest.VSCodePortProxyURI
1577 }
1578
1579 // Allow any of the current env to override what we defined above.
1580 for _, env := range current {
1581 parts := strings.SplitN(env, "=", 2)
1582 if len(parts) != 2 {
1583 continue
1584 }
1585 if _, ok := envs[parts[0]]; !ok {
1586 envs[parts[0]] = parts[1]
1587 }
1588 }
1589
1590 // Load environment variables passed via the agent manifest.
1591 // These override all variables we manually specify.
1592 for k, v := range manifest.EnvironmentVariables {
1593 // Expanding environment variables allows for customization
1594 // of the $PATH, among other variables. Customers can prepend
1595 // or append to the $PATH, so allowing expand is required!
1596 envs[k] = os.ExpandEnv(v)

Callers

nothing calls this directly

Calls 5

ScriptBinDirMethod · 0.80
GetSessionTokenMethod · 0.65
LoadMethod · 0.45
ErrorfMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected