Lines 19–34 at 268 (were 19–25 at 267)
1919import { CLIENT_DIR } from "./client-version.ts"; 2020import { type Env, type Output, plural } from "./command-env.ts"; 2121import { leftOutText, syncFolder, uploadBlobs } from "./commands.ts"; 22−import { findRoot, installKey, readConfig, type Settings, tokenFor, writeConfig } from "./config.ts";
2332import { elsewhereText } from "./doctor.ts"; 2433import { fromEnv } from "./env-names.ts"; 2534import { clearKnown, loadIgnore, readFolder, writeKnown } from "./folder.ts"; Lines 68–109 at 268 (were 59–64 at 267)
5968 return ['The project is empty. Put its files in this folder, then abso push -m "why" publishes them.']; 71+/** A credential other than the box's, and how init and join name it when they say what they sign in with. */ 72+export interface Credential { 73+ readonly token: string | undefined; 74+ readonly words: string; 78+ * What init and join sign in with when it isn't the box's sign-in. The machine's token file only with --token-file, and 79+ * never into a folder that already has a key; else a token the environment names (ABSO_TOKEN, or a pipeline run's 80+ * ABSO_KEY_FILE); else none. The token file is never read unasked, so a project is never made or joined with a shared 81+ * credential nobody chose. 83+export function tokenCredential( 88+ file: () => string | undefined = tokenFile, 89+): Credential | undefined { 90+ if (bool(args, "token-file")) { 91+ if (existsSync(join(stateDir(root), "key"))) 92+ throw new UsageError(`${root} already has a key, so it signs in with that: leave off --token-file`); 93+ const token = trustedToken(server, env, file()); 94+ if (!token) throw new UsageError("--token-file reads ~/.abso/token, and this machine has none"); 95+ return { token, words: "the token file, ~/.abso/token, as --token-file asks" }; 97+ const named = fromEnv(env, "TOKEN")?.trim(); 98+ if (named) return { token: trustedToken(server, env, named), words: "ABSO_TOKEN" }; 99+ const run = runKey(env); 100+ if (run) return { token: trustedToken(server, env, run), words: "a pipeline run's key (ABSO_KEY_FILE)" }; 104+/** Why a token file is no fallback: said when one is here and nothing else signs in. */ 105+const TOKEN_FILE_ASKED = "~/.abso/token is used only with --token-file."; 62107export async function runInit(args: Parsed, env: Env): Promise<Output> { 63108 if (args.positionals.length > 1) throw new UsageError("init takes one folder"); 64109 const root = resolve(env.cwd, args.positionals[0] ?? "."); Lines 112–132 at 268 (were 67–78 at 267)
67112 const project = str(args, "project"); 68113 if (!project) throw new UsageError("give the project a name with --project <name>"); 69114 if (bool(args, "public") && bool(args, "private")) throw new UsageError("--public or --private, not both"); 70− // A box signed in here, and no token for this server: the project is made as the person who approved the box,
71− // the way the website makes one (abso 663).
115+ // A box signed in here, and no other credential asked for: the project is made as the person who approved the box, 116+ // the way the website makes one (abso 663). The token file only with --token-file, never by itself. 72117 const server = serverFrom(args, env); 73118 const box = readBox(server); 74− if (box && !tokenFor(server, env.env) && !fromEnv(env.env, "KEY_FILE"))
119+ const credential = tokenCredential(args, env.env, server, root); 120+ const say = env.writeErr ?? env.write; 121+ if (box && credential === undefined) { 122+ say(`Signing in with this box's sign-in, ${box.id}. 75124 return initFromBox(args, env, root, project, server, box); 126+ if (credential === undefined && tokenFile() !== undefined) 127+ throw new UsageError( 128+ `abso init signs in with this box's sign-in, and this box isn't signed in to ${server}: abso login signs it in. ${TOKEN_FILE_ASKED}`, 76130 const org = str(args, "org"); 77131 if (org !== undefined) return initInOrg(org, server); 78132 const config = makeSettings(args, env, project); Lines 137–145 at 268 (were 83–89 at 267)
83137 // What the server runs on each new version (#39), guessed from package.json; recorded with the rest. 84138 const wroteProject = asIs ? "exists" : writeStartingProjectFile(root); 85139 const { files, blobs, modes, leftOut } = readFolder(root, loadIgnore(root)); 86− const token = tokenFor(config.server, env.env);
140+ const token = credential?.token; 141+ say(`Signing in with ${credential?.words ?? `no credential: nothing here signs in to ${server}`}. 87143 const api = new Api({ ...config, ...(token ? { token } : {}) }); 88144 await uploadBlobs(api, blobs); 89145 const answer = await api.init( Lines 365–380 at 268 (were 309–319 at 267)
309365 const server = serverFrom(args, env); 310366 const box = readBox(server); 311367 const hasKey = existsSync(join(stateDir(root), "key")); 368+ const credential = tokenCredential(args, env.env, server, root); 369+ const tokenFileAsked = bool(args, "token-file"); 312370 let agent: string; 313371 let minted: { key: string; id: string } | undefined; 314372 if (str(args, "agent") !== undefined) { 315373 agent = agentFrom(args); 316− minted = box && !hasKey && !fromEnv(env.env, "KEY_FILE") ? await mintKey(box, project, agent) : undefined;
375+ box && !hasKey && !fromEnv(env.env, "KEY_FILE") && !tokenFileAsked 376+ ? await mintKey(box, project, agent) 317378 } else if (fromEnv(env.env, "KEY_FILE")) { 318379 // A pipeline run's key (ABSO_KEY_FILE) belongs to one agent, which the runner names in ABSO_AGENT. 319380 const named = fromEnv(env.env, "AGENT"); Lines 398–410 at 268 (were 337–343 at 267)
337398 writeConfig(root, config); 339400 if (minted) installKey(root, JSON.stringify(minted)); 340− const settings = readConfig(root, env.env);
401+ // The token file only with --token-file: readConfig reads it otherwise. 402+ const read = readConfig(root, env.env, false); 403+ const settings = tokenFileAsked ? { ...read, token: credential?.token } : read; 404+ (env.writeErr ?? env.write)( 405+ `Signing in with ${minted && box ? `a new key from this box's sign-in, ${box.id}` : hasKey ? "this folder's key" : (credential?.words ?? `no credential: nothing here signs in to ${server}`)}. 341408 const api = new Api(settings); 342409 let synced: Awaited<ReturnType<typeof syncFolder>>; Lines 415–422 at 268 (were 348–354 at 267)
349416 if (!(error instanceof ApiError && error.status === 401 && !box && !settings.token)) throw error; 350417 rmSync(stateDir(root), { recursive: true, force: true }); 351− throw new UsageError(await signedOut(config.server, project));
418+ const why = await signedOut(config.server, project); 419+ throw new UsageError(!tokenFileAsked && tokenFile() !== undefined ? `${why} ${TOKEN_FILE_ASKED}` : why);