Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/umask command #17

Merged
merged 10 commits into from
Nov 5, 2024
Merged
18 changes: 18 additions & 0 deletions composables/umask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createGlobalState } from '@vueuse/core';

export const useUmaskStore = createGlobalState(() => {
const umask = ref(window?.localStorage?.getItem('umask') || '000000000010');
function changeUmask(newUmask: string & { length: 12 } & { [index: number]: '0' | '1' }) {
umask.value = newUmask;
localStorage.setItem('umask', newUmask);
}
function clearUmask() {
umask.value = '000000000010';
localStorage.setItem('umask', umask.value);
}
return {
umask,
changeUmask,
clearUmask,
};
});
2 changes: 2 additions & 0 deletions composables/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createGlobalState } from '@vueuse/core';

export const useUserStore = createGlobalState(() => {
const { clearUmask } = useUmaskStore();
const username = ref(window?.localStorage?.getItem('username') || 'guest');
onMounted(async () => {
if (username.value === 'guest') {
Expand All @@ -19,6 +20,7 @@ export const useUserStore = createGlobalState(() => {
function switchUser(name: string) {
username.value = name;
localStorage.setItem('username', name);
clearUmask();
}
watch(username, async () => {
const meta = (await useFetch('/api/users', {
Expand Down
6 changes: 6 additions & 0 deletions lib/command/impls/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ const commandDescriptions: Record<Command, CommandDescription> = {
{ args: ['-u <username>', '-p <password>'] },
],
},
[Command.UMASK]: {
description: 'Set file mode creation mask',
usages: [
{ args: ['(<oct><oct><oct>)?'] },
],
},
};

function getDescription(commandName: string): string[] {
Expand Down
12 changes: 12 additions & 0 deletions lib/command/impls/mkdir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { formatArg } from '../utils';
import type { CommandFunc } from './types';

export const mkdir: CommandFunc = async function(...args) {
// discard `mkdir`
args.shift();
// discard first space
args.shift();

return [
];
};
12 changes: 12 additions & 0 deletions lib/command/impls/touch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { formatArg } from '../utils';
import type { CommandFunc } from './types';

export const touch: CommandFunc = async function(...args) {
// discard `touch`
args.shift();
// discard first space
args.shift();

return [
];
};
3 changes: 3 additions & 0 deletions lib/command/impls/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ export enum Command {
SU = 'su',
LS = 'ls',
USERADD = 'useradd',
TOUCH = 'touch',
MKDIR = 'mkdir',
UMASK = 'umask',
}
58 changes: 58 additions & 0 deletions lib/command/impls/umask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { formatArg } from '../utils';
import type { CommandFunc } from './types';

export const umask: CommandFunc = async function(...args) {
// discard `umask`
args.shift();
// discard first space
args.shift();

if (args.length === 0) {
const { umask } = useUmaskStore();
return [umaskToOct(umask.value)];
}

if (args.length > 1) {
return [
'Invalid use of umask. Run \'help umask\'',
];
}

const umask = formatArg(args[0]);
if (umask.length !== 3 || !isOctDigit(umask[0]) || !isOctDigit(umask[1]) || !isOctDigit(umask[2])) {
return ['Invalid umask'];
}
const { changeUmask } = useUmaskStore();
changeUmask(umaskFromOct(umask as any));
return [
'Change umask successfully',
];
};

function isOctDigit(c: string): boolean {
const n = Number.parseInt(c);
return c.length === 1 && 0 <= n && n <= 7;
}

function umaskToOct(umask: string): string {
const ownerRead = Number.parseInt(umask[3]);
const ownerWrite = Number.parseInt(umask[4]);
const ownerExecute = Number.parseInt(umask[5]);
const ownerOct = ownerRead * 4 + ownerWrite * 2 + ownerExecute;
const groupRead = Number.parseInt(umask[6]);
const groupWrite = Number.parseInt(umask[7]);
const groupExecute = Number.parseInt(umask[8]);
const groupOct = groupRead * 4 + groupWrite * 2 + groupExecute;
const otherRead = Number.parseInt(umask[9]);
const otherWrite = Number.parseInt(umask[10]);
const otherExecute = Number.parseInt(umask[11]);
const otherOct = otherRead * 4 + otherWrite * 2 + otherExecute;
return `${ownerOct}${groupOct}${otherOct}`;
}

function umaskFromOct(octs: string): string {
const ownerOct = octs[0];
const groupOct = octs[1];
const otherOct = octs[2];
return `000${ownerOct.toString(2).padStart(3, '0')}${groupOct.toString(2).padStart(3, '0')}${otherOct.toString(2).padStart(3, '0')}`;
}
12 changes: 12 additions & 0 deletions lib/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { cd } from './impls/cd';
import { su } from './impls/su';
import { ls } from './impls/ls';
import { useradd } from './impls/useradd';
import { touch } from './impls/touch';
import { mkdir } from './impls/mkdir';
import { umask } from './impls/umask';

export async function execute(command: string): Promise<ColoredContent> {
const args = parse(command);
Expand Down Expand Up @@ -37,6 +40,15 @@ export async function execute(command: string): Promise<ColoredContent> {
case Command.USERADD:
res = await useradd(...args as any);
break;
case Command.TOUCH:
res = await touch(...args as any);
break;
case Command.MKDIR:
res = await mkdir(...args as any);
break;
case Command.UMASK:
res = await umask(...args as any);
break;
default:
res = echo('echo', ' ', `Unknown command:\\u001b[31m ${args[0]}`);
break;
Expand Down
2 changes: 2 additions & 0 deletions services/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export const fileService = {
},
async createFile(filename: string): Promise<Result<null, Diagnostic>> {
},
async createFolder(filename: string): Promise<Result<null, Diagnostic>> {
},
async changeDirectory(filename: string): Promise<Result<null, Diagnostic>> {
try {
const { cwd, switchCwd } = useCwdStore();
Expand Down
Loading