@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
64 lines • 2.87 kB
JavaScript
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
// 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.
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
import { IOResult } from "../../../io/types.js";
import { command } from "../../config.js";
import { specOf } from "../../spec/builtins.js";
import { FlagView } from "../../spec/flag_view.js";
import { SYNOPSES } from "../../spec/synopsis.js";
import { usageExitCode, usageHint } from "../../spec/usage.js";
import { HOSTNAME } from "../constants.js";
const ENC = new TextEncoder();
// What each display option prints, as a Debian host with no DNS domain whose
// /etc/hosts maps its name to 127.0.1.1; null is the unset NIS domain.
const DISPLAY = {
short: `${HOSTNAME}\n`,
fqdn: `${HOSTNAME}\n`,
long: `${HOSTNAME}\n`,
domain: '',
alias: '\n',
all_fqdns: '\n',
ip_address: '127.0.1.1\n',
all_ip_addresses: '\n',
yp: null,
nis: null,
};
const NOT_ROOT = 'hostname: you must be root to change the host name\n';
const NO_NIS_DOMAIN = 'hostname: Local domain name not set\n';
const USAGE = `Usage: ${SYNOPSES.hostname ?? ''}\n${usageHint('hostname')}\n`;
// hostname 3.25 (Debian): the name uname -n prints, never the host's. The last
// display option wins, and setting a name is the refusal an unprivileged user
// gets. Misfit operands print the usage line and the --help hint, exit 255,
// where hostname dumps its whole help.
function hostname(_accessor, _paths, texts, ctx) {
const fl = new FlagView(ctx.flags, specOf('hostname'));
const typed = fl.typedOrder(...Object.keys(DISPLAY));
if (texts.length > 1 || (texts.length > 0 && typed.length > 0)) {
return [null, new IOResult({ exitCode: usageExitCode('hostname'), stderr: ENC.encode(USAGE) })];
}
if (texts.length > 0 || fl.asStr('file') !== undefined) {
return [null, new IOResult({ exitCode: 1, stderr: ENC.encode(NOT_ROOT) })];
}
const shown = DISPLAY[typed.at(-1) ?? 'short'];
if (shown === null || shown === undefined) {
return [ENC.encode(NO_NIS_DOMAIN), new IOResult({ exitCode: 1 })];
}
return [ENC.encode(shown), new IOResult()];
}
export const GENERAL_HOSTNAME = command({
name: 'hostname',
vfs: null,
spec: specOf('hostname'),
fn: hostname,
});
//# sourceMappingURL=hostname.js.map