Skip to content

Commit

Permalink
feat/implement cli (#205)
Browse files Browse the repository at this point in the history
* feat: implement provider contract

* feat: implement event parameters

* feat: implement cli domain

* feat: implement cli components and add export

* feat: add cli commands in parameters

* feat: move cli to dedicated package
  • Loading branch information
LeadcodeDev authored Oct 13, 2024
1 parent 74d548f commit 9828093
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 70 deletions.
1 change: 1 addition & 0 deletions lib/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export 'package:mineral/src/domains/events/contracts/server/server_roles_update_
export 'package:mineral/src/domains/events/contracts/server/server_stickers_update_event.dart';
export 'package:mineral/src/domains/events/contracts/server/server_text_select_event.dart';
export 'package:mineral/src/domains/events/contracts/server/server_update_event.dart';
export 'package:mineral/src/domains/events/event.dart';
1 change: 1 addition & 0 deletions lib/services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export 'package:mineral/src/infrastructure/services/http/http_interceptor.dart';
export 'package:mineral/src/infrastructure/services/http/request.dart';
export 'package:mineral/src/infrastructure/services/http/response.dart';
export 'package:mineral/src/infrastructure/services/http/type/response_code.dart';
export 'package:mineral/src/infrastructure/services/logger/log_level.dart';
export 'package:mineral/src/infrastructure/services/logger/logger.dart';
export 'package:mineral/src/infrastructure/services/placeholder/env_placeholder.dart';
export 'package:mineral/src/infrastructure/services/placeholder/placeholder.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ abstract class PrivateUserSelectEvent implements ListenableEvent {
@override
String? customId;

FutureOr<void> handle(PrivateSelectContext ctx, List<User> roles);
FutureOr<void> handle(PrivateSelectContext ctx, List<User> users);
}
89 changes: 46 additions & 43 deletions lib/src/domains/events/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,53 +45,56 @@ import 'package:mineral/src/domains/events/contracts/server/server_update_event.
interface class EventType {}

enum Event implements EnhancedEnum, EventType {
ready(ReadyEvent),
serverCreate(ServerCreateEvent),
serverUpdate(ServerUpdateEvent),
serverDelete(ServerDeleteEvent),
serverMessageCreate(ServerMessageCreateEvent),
serverChannelCreate(ServerChannelCreateEvent),
serverChannelUpdate(ServerChannelUpdateEvent),
serverChannelDelete(ServerChannelDeleteEvent),
serverChannelPinsUpdate(ServerChannelPinsUpdateEvent),
privateChannelPinsUpdate(PrivateChannelPinsUpdateEvent),
serverMemberAdd(ServerMemberAddEvent),
serverMemberRemove(ServerMemberRemoveEvent),
serverBanAdd(ServerBanAddEvent),
serverBanRemove(ServerBanRemoveEvent),
serverMemberUpdate(ServerMemberUpdateEvent),
serverPresenceUpdate(ServerPresenceUpdateEvent),
serverEmojisUpdate(ServerEmojisUpdateEvent),
serverStickersUpdate(ServerStickersUpdateEvent),
serverRoleCreate(ServerRolesCreateEvent),
serverRoleUpdate(ServerRolesUpdateEvent),
serverRoleDelete(ServerRolesDeleteEvent),
serverButtonClick(ServerButtonClickEvent),
serverDialogSubmit(ServerDialogSubmitEvent),
serverChannelSelect(ServerChannelSelectEvent),
serverRoleSelect(ServerRoleSelectEvent),
serverMemberSelect(ServerMemberSelectEvent),
serverTextSelect(ServerTextSelectEvent),
serverThreadCreate(ServerThreadCreateEvent),
serverThreadUpdate(ServerThreadUpdateEvent),
serverThreadDelete(ServerThreadDeleteEvent),
serverThreadMemberUpdate(ServerThreadMemberUpdateEvent),
serverThreadMemberAdd(ServerThreadMemberAddEvent),
serverThreadMemberRemove(ServerThreadMemberRemoveEvent),
serverThreadListSync(ServerThreadListSyncEvent),
ready(ReadyEvent, ['Bot bot']),
serverCreate(ServerCreateEvent, ['Server server']),
serverUpdate(ServerUpdateEvent, ['Server before', 'Server after']),
serverDelete(ServerDeleteEvent, ['Server? server']),
serverMessageCreate(ServerMessageCreateEvent, ['ServerMessage message']),
serverChannelCreate(ServerChannelCreateEvent, ['ServerChannel channel']),
serverChannelUpdate(ServerChannelUpdateEvent, ['ServerChannel before', 'ServerChannel after']),
serverChannelDelete(ServerChannelDeleteEvent, ['ServerChannel? channel']),
serverChannelPinsUpdate(ServerChannelPinsUpdateEvent, ['ServerChannel channel']),
privateChannelPinsUpdate(PrivateChannelPinsUpdateEvent, ['PrivateChannel channel']),
serverMemberAdd(ServerMemberAddEvent, ['Member member', 'Server server']),
serverMemberRemove(ServerMemberRemoveEvent, ['Member member']),
serverBanAdd(ServerBanAddEvent, ['ServerBan ban']),
serverBanRemove(ServerBanRemoveEvent, ['ServerBan ban']),
serverMemberUpdate(
ServerMemberUpdateEvent, ['ServerMember? before', 'Member after', 'Server server']),
serverPresenceUpdate(ServerPresenceUpdateEvent, ['Member member', 'Server server', 'Presence presence']),
serverEmojisUpdate(ServerEmojisUpdateEvent, ['EmojiManager emojisManager', 'Server server']),
serverStickersUpdate(ServerStickersUpdateEvent, ['StickerManager stickerManager', 'Server server']),
serverRoleCreate(ServerRolesCreateEvent, ['Role role', 'Server server']),
serverRoleUpdate(ServerRolesUpdateEvent, ['Role before', 'Role after', 'Server server']),
serverRoleDelete(ServerRolesDeleteEvent, ['Role? role', 'Server server']),
serverButtonClick(ServerButtonClickEvent, ['ServerButtonContext ctx']),
serverDialogSubmit(ServerDialogSubmitEvent, ['ServerDialogContext ctx']),
serverChannelSelect(ServerChannelSelectEvent, ['ServerSelectContext ctx', 'List<ServerChannel> channels']),
serverRoleSelect(ServerRoleSelectEvent, ['ServerSelectContext ctx', 'List<Role> roles']),
serverMemberSelect(ServerMemberSelectEvent, ['ServerSelectContext ctx', 'List<Member> members']),
serverTextSelect(ServerTextSelectEvent, ['ServerSelectContext ctx', 'List<String> values']),
serverThreadCreate(ServerThreadCreateEvent, ['ThreadChannel channel', 'Server server']),
serverThreadUpdate(ServerThreadUpdateEvent, ['ThreadChannel before', 'ThreadChannel after', 'Server server']),
serverThreadDelete(ServerThreadDeleteEvent, ['ThreadChannel thread', 'Server server']),
serverThreadMemberUpdate(ServerThreadMemberUpdateEvent, ['ThreadChannel thread', 'Server server', 'Member member']),
serverThreadMemberAdd(ServerThreadMemberAddEvent, ['ThreadChannel thread', 'Server server', 'Member member']),
serverThreadMemberRemove(ServerThreadMemberRemoveEvent, ['ThreadChannel thread', 'Server server', 'Member member']),
serverThreadListSync(ServerThreadListSyncEvent, ['List<ThreadChannel> threads', 'Server server']),

// private
privateMessageCreate(PrivateMessageCreateEvent),
privateChannelCreate(PrivateChannelCreateEvent),
privateChannelUpdate(PrivateChannelUpdateEvent),
privateChannelDelete(PrivateChannelDeleteEvent),
privateButtonClick(PrivateButtonClickEvent),
privateDialogSubmit(PrivateDialogSubmitEvent),
privateUserSelect(PrivateUserSelectEvent),
privateTextSelect(PrivateTextSelectEvent);
privateMessageCreate(PrivateMessageCreateEvent, ['PrivateMessage message']),
privateChannelCreate(PrivateChannelCreateEvent, ['PrivateChannel channel']),
privateChannelUpdate(PrivateChannelUpdateEvent, ['PrivateChannel before', 'PrivateChannel after']),
privateChannelDelete(PrivateChannelDeleteEvent, ['PrivateChannel? channel']),
privateButtonClick(PrivateButtonClickEvent, ['PrivateButtonContext ctx']),
privateDialogSubmit(PrivateDialogSubmitEvent, ['PrivateDialogContext ctx']),
privateUserSelect(PrivateUserSelectEvent, ['PrivateSelectContext ctx', 'List<User> users']),
privateTextSelect(PrivateTextSelectEvent, ['PrivateSelectContext ctx', 'List<String> values']),;

@override
final Type value;

const Event(this.value);
final List<String> parameters;

const Event(this.value, this.parameters);
}
10 changes: 9 additions & 1 deletion lib/src/domains/providers/provider.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import 'dart:async';

abstract class ProviderContract {
abstract interface class ProviderContract {
FutureOr<void> ready();
FutureOr<void> dispose();
}

abstract class Provider implements ProviderContract {
@override
FutureOr<void> ready() {}

@override
FutureOr<void> dispose() {}
}
19 changes: 19 additions & 0 deletions lib/src/infrastructure/commons/file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,22 @@ extension JsonFile on File {
return constructor != null ? constructor(map) : map;
}
}

extension YamlWriter<K, V> on Map<K, V> {
void writeAsYaml({required StringBuffer buffer, required List<MapEntry> payload, int spacing = 2}) {
for (final entry in payload) {
final spaces = ' ' * spacing;

if (entry.value is String) {
buffer.writeln('$spaces${entry.key}: ${entry.value}');
} else if (entry.value is Map) {
buffer.writeln('$spaces${entry.key}:');

writeAsYaml(
buffer: buffer,
payload: (entry.value as dynamic).entries.toList(),
spacing: spacing + 2);
}
}
}
}
17 changes: 17 additions & 0 deletions lib/src/infrastructure/services/logger/log_level.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:logging/logging.dart' as logging;
import 'package:mineral/api.dart';

enum LogLevel implements EnhancedEnum<logging.Level> {
trace('TRACE', logging.Level.FINEST),
fatal('FATAL', logging.Level.SHOUT),
error('ERROR', logging.Level.SEVERE),
warn('WARN', logging.Level.WARNING),
info('INFO', logging.Level.INFO);

final String name;

@override
final logging.Level value;

const LogLevel(this.name, this.value);
}
39 changes: 14 additions & 25 deletions lib/src/infrastructure/services/logger/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:logging/logging.dart' as logging;
import 'package:mansion/mansion.dart';
import 'package:mineral/src/infrastructure/internals/environment/app_env.dart';
import 'package:mineral/src/infrastructure/internals/environment/environment.dart';
import 'package:mineral/src/infrastructure/services/logger/log_level.dart';

abstract interface class LoggerContract {
void trace(Object message);
Expand All @@ -20,6 +21,7 @@ abstract interface class LoggerContract {

final class Logger implements LoggerContract {
static Color get primaryColor => Color.fromRGB(140, 169, 238);

static Color get mutedColor => Color.brightBlack;

final EnvContract _env;
Expand All @@ -29,26 +31,18 @@ final class Logger implements LoggerContract {
final level = _env.get(AppEnv.logLevel);
final dartEnv = _env.get(AppEnv.dartEnv);

const logLevels = {
'TRACE': logging.Level.FINEST,
'FATAL': logging.Level.SHOUT,
'ERROR': logging.Level.SEVERE,
'WARN': logging.Level.WARNING,
'INFO': logging.Level.INFO,
};

final bool logLevel = logLevels.keys.contains(level.toUpperCase());
final bool logLevel = LogLevel.values.map((level) => level.name).contains(level.toUpperCase());
if (!logLevel) {
throw Exception(
'Invalid LOG_LEVEL environment variable, please include in ${logLevels.keys.map((e) => e.toLowerCase())}');
'Invalid LOG_LEVEL environment variable, please include in ${LogLevel.values.map((level) => level.name.toLowerCase())}');
}

logging.Logger.root.level = logLevels[level.toUpperCase()];
logging.Logger.root.level =
LogLevel.values.firstWhere((element) => element.name == level.toUpperCase()).value;
logging.Logger.root.onRecord.listen((record) {
final time = '[${DateFormat.Hms().format(record.time)}]';

List<Sequence> makeMessage(
String messageType, Color messageColor, List<Sequence> message) {
List<Sequence> makeMessage(String messageType, Color messageColor, List<Sequence> message) {
return [
SetStyles(Style.foreground(Color.brightBlack)),
Print(time),
Expand All @@ -63,18 +57,13 @@ final class Logger implements LoggerContract {
}

final message = switch (record.level) {
logging.Level.FINEST => makeMessage('trace', Color.white, [
SetStyles(Style.foreground(Color.brightBlack)),
Print(record.message)
]),
logging.Level.SHOUT =>
makeMessage('fatal', Color.brightRed, [Print(record.message)]),
logging.Level.SEVERE =>
makeMessage('error', Color.red, [Print(record.message)]),
logging.Level.WARNING =>
makeMessage('warn', Color.yellow, [Print(record.message)]),
logging.Level.INFO => makeMessage(
'info', Color.fromRGB(140, 169, 238), [Print(record.message)]),
logging.Level.FINEST => makeMessage('trace', Color.white,
[SetStyles(Style.foreground(Color.brightBlack)), Print(record.message)]),
logging.Level.SHOUT => makeMessage('fatal', Color.brightRed, [Print(record.message)]),
logging.Level.SEVERE => makeMessage('error', Color.red, [Print(record.message)]),
logging.Level.WARNING => makeMessage('warn', Color.yellow, [Print(record.message)]),
logging.Level.INFO =>
makeMessage('info', Color.fromRGB(140, 169, 238), [Print(record.message)]),
_ => makeMessage('unknown', Color.blue, [Print(record.message)]),
};

Expand Down

0 comments on commit 9828093

Please sign in to comment.