diff --git a/index.js b/index.js index 718905f..74b7d48 100644 --- a/index.js +++ b/index.js @@ -24,6 +24,8 @@ const getUsernameFromOsUserInfo = () => { const cleanWindowsCommand = string => string.replace(/^.*\\/, ''); +const makeUsernameFromId = userId => `no-username-${userId}`; + module.exports = mem(async () => { const envVariable = getEnvironmentVariable(); if (envVariable) { @@ -35,12 +37,20 @@ module.exports = mem(async () => { return userInfoUsername; } + /** + First we try to get the ID of the user and then the actual username. We do this because in `docker run --user :` context, we don't have "username" available. Therefore, we have a fallback to `makeUsernameFromId` for such scenario. Applies also to the `sync()` method below. + */ try { if (process.platform === 'win32') { return cleanWindowsCommand(await execa.stdout('whoami')); } - return await execa.stdout('id', ['-un']); + const userId = await execa.stdout('id', ['-u']); + try { + return await execa.stdout('id', ['-un', userId]); + } catch (_) {} + + return makeUsernameFromId(userId); } catch (_) {} }); @@ -60,6 +70,11 @@ module.exports.sync = mem(() => { return cleanWindowsCommand(execa.sync('whoami').stdout); } - return execa.sync('id', ['-un']).stdout; + const userId = execa.sync('id', ['-u']).stdout; + try { + return execa.sync('id', ['-un', userId]).stdout; + } catch (_) {} + + return makeUsernameFromId(userId); } catch (_) {} });