exportifyNushell formats vars as nushell environment variable assignments. Each line is of the form `$env.KEY = "value"` with special characters escaped.
(w io.Writer, vars map[string]string)
| 108 | // exportifyNushell formats vars as nushell environment variable assignments. |
| 109 | // Each line is of the form `$env.KEY = "value"` with special characters escaped. |
| 110 | func exportifyNushell(w io.Writer, vars map[string]string) string { |
| 111 | // Nushell protected environment variables that cannot be set manually |
| 112 | // See: https://www.nushell.sh/book/environment.html#automatic-environment-variables |
| 113 | protectedVars := map[string]bool{ |
| 114 | "CURRENT_FILE": true, |
| 115 | "FILE_PWD": true, |
| 116 | "LAST_EXIT_CODE": true, |
| 117 | "CMD_DURATION_MS": true, |
| 118 | "NU_VERSION": true, |
| 119 | "PWD": true, // Nushell manages this automatically |
| 120 | } |
| 121 | |
| 122 | keys := make([]string, len(vars)) |
| 123 | i := 0 |
| 124 | for k := range vars { |
| 125 | keys[i] = k |
| 126 | i++ |
| 127 | } |
| 128 | slices.Sort(keys) // for reproducibility |
| 129 | |
| 130 | var invalidNames []string |
| 131 | strb := strings.Builder{} |
| 132 | for _, key := range keys { |
| 133 | // Skip bash functions for nushell |
| 134 | if strings.HasPrefix(key, "BASH_FUNC_") && strings.HasSuffix(key, "%%") { |
| 135 | continue |
| 136 | } |
| 137 | |
| 138 | // Skip nushell protected environment variables |
| 139 | if protectedVars[key] { |
| 140 | continue |
| 141 | } |
| 142 | |
| 143 | // Skip names that aren't valid environment variable identifiers. |
| 144 | if !isValidEnvName(key) { |
| 145 | invalidNames = append(invalidNames, key) |
| 146 | continue |
| 147 | } |
| 148 | |
| 149 | // Nushell environment variable syntax: $env.KEY = "value" |
| 150 | strb.WriteString("$env.") |
| 151 | strb.WriteString(key) |
| 152 | strb.WriteString(` = "`) |
| 153 | for _, r := range vars[key] { |
| 154 | switch r { |
| 155 | // Escape special characters for nushell double-quoted strings |
| 156 | case '"', '\\': |
| 157 | strb.WriteRune('\\') |
| 158 | } |
| 159 | strb.WriteRune(r) |
| 160 | } |
| 161 | strb.WriteString("\"\n") |
| 162 | } |
| 163 | warnInvalidEnvNames(w, invalidNames) |
| 164 | return strings.TrimSpace(strb.String()) |
| 165 | } |
| 166 | |
| 167 | // addEnvIfNotPreviouslySetByDevbox adds the key-value pairs from new to existing, |