2
0
mirror of https://github.com/dadevel/wg-netns synced 2024-10-30 21:20:12 +00:00

retrieve current username correctly

`os.getlogin()` does not work in some cases, however `getpass.getuser()` does.
Closes #17.
This commit is contained in:
Rafael Ristovski 2023-05-25 21:24:15 +02:00 committed by dadevel
parent a4c991a499
commit b0e7fcdda8
No known key found for this signature in database
GPG Key ID: 1A8A9735430193D5

View File

@ -4,6 +4,7 @@ from argparse import ArgumentParser, RawDescriptionHelpFormatter
from pathlib import Path
from typing import Any, Optional
import dataclasses
import getpass
import json
import os
import subprocess
@ -92,7 +93,7 @@ def cli(args):
data = json.loads(output)
print('\n'.join(item['name'] for item in data))
elif opts.action == 'switch':
os.execvp('sudo', ['ip', 'ip', 'netns', 'exec', opts.netns, 'sudo', '-u', os.getlogin(), '-D', Path.cwd().as_posix(), os.environ['SHELL'], '-i'])
os.execvp('sudo', ['ip', 'ip', 'netns', 'exec', opts.netns, 'sudo', '-u', getpass.getuser(), '-D', Path.cwd().as_posix(), os.environ['SHELL'], '-i'])
else:
raise RuntimeError('congratulations, you reached unreachable code')