Skip to content

Commit

Permalink
perf: #5 refactor config props (#15)
Browse files Browse the repository at this point in the history
* refactor config props

* cleanup

* simplify names

* format

* Add TODO comment

* remove unused late init vars

* cleanup environment.dart
  • Loading branch information
miquelbeltran authored Jan 27, 2025
1 parent bad5e2d commit b1e0acd
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 108 deletions.
87 changes: 39 additions & 48 deletions lib/config_props.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,51 @@ import 'dart:io';
import 'package:args/args.dart';
import 'package:raygun_cli/environment.dart';

/// Configuration properties for the Raygun CLI
class ConfigProps {
/// Raygun's application ID
final String appId;

/// Raygun's access token
final String token;

ConfigProps._({
required this.appId,
required this.token,
/// A Config property is a value
/// that can be set via argument
/// or environment variable.
/// TODO: #5 add support for config files (.raygun.yaml or similar)
class ConfigProp {
static const appId = ConfigProp(
name: 'app-id',
envKey: Environment.raygunAppIdKey,
);

static const token = ConfigProp(
name: 'token',
envKey: Environment.raygunTokenKey,
);

static const apiKey = ConfigProp(
name: 'api-key',
envKey: Environment.raygunApiKeyKey,
);

/// The name of the property
final String name;

/// The environment variable key
final String envKey;

const ConfigProp({
required this.name,
required this.envKey,
});

/// Load configuration properties from arguments or environment variables
/// and return a new instance of [ConfigProps] or exit with code 2.
factory ConfigProps.load(ArgResults arguments, {bool verbose = false}) {
String? appId;
String? token;

// Providing app-id and token via argument takes priority
if (arguments.wasParsed('app-id')) {
appId = arguments['app-id'];
} else {
appId = Environment.instance.raygunAppId;
}

if (appId == null) {
print('Error: Missing "app-id"');
print(
' Please provide "app-id" via argument or environment variable "RAYGUN_APP_ID"');
exit(2);
}

if (arguments.wasParsed('token')) {
token = arguments['token'];
/// Load the value of the property from arguments or environment variables
String load(ArgResults arguments) {
String? value;
if (arguments.wasParsed(name)) {
value = arguments[name];
} else {
token = Environment.instance.raygunToken;
value = Environment.instance[envKey];
}

if (token == null) {
print('Error: Missing "token"');
if (value == null) {
print('Error: Missing "$name"');
print(
' Please provide "token" via argument or environment variable "RAYGUN_TOKEN"');
' Please provide "$name" via argument or environment variable "$envKey"');
exit(2);
}

if (verbose) {
print('App ID: $appId');
print('Token: $token');
}

return ConfigProps._(
appId: appId,
token: token,
);
return value;
}
}
17 changes: 3 additions & 14 deletions lib/deployments/deployments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,29 @@ import '../config_props.dart';
class Deployments {
final ArgResults command;
final bool verbose;
late final String appId;
late final String token;

Deployments({
required this.command,
required this.verbose,
required ConfigProps config,
}) {
appId = config.appId;
token = config.token;
}
});

Future<void> notify() async {
if (!command.wasParsed('api-key')) {
print('Error: Missing "--api-key"');
print(' Please provide "--api-key" via argument');
exit(2);
}
if (!command.wasParsed('version')) {
print('Error: Missing "--version"');
print(' Please provide "--version" via argument');
exit(2);
}

final apiKey = command.option('api-key') as String;
final version = command.option('version') as String;
final ownerName = command.option('owner-name');
final emailAddress = command.option('email-address');
final comment = command.option('comment');
final scmIdentifier = command.option('scm-identifier');
final scmType = command.option('scm-type');
final apiKey = ConfigProp.apiKey.load(command);
final token = ConfigProp.token.load(command);

if (verbose) {
print('app-id: $appId');
print('token: $token');
print('api-key: $apiKey');
print('version: $version');
Expand Down
9 changes: 0 additions & 9 deletions lib/deployments/deployments_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import 'dart:io';
import 'package:args/args.dart';
import 'package:raygun_cli/deployments/deployments.dart';

import '../config_props.dart';

const kDeploymentsCommand = 'deployments';

ArgParser buildParserDeployments() {
Expand All @@ -15,10 +13,6 @@ ArgParser buildParserDeployments() {
negatable: false,
help: 'Print deployments usage information',
)
..addOption(
'app-id',
help: 'Raygun application ID',
)
..addOption(
'token',
help: 'Raygun access token',
Expand Down Expand Up @@ -70,11 +64,8 @@ void parseDeploymentsCommand(ArgResults command, bool verbose) {
exit(0);
}

final configProps = ConfigProps.load(command, verbose: verbose);

Deployments(
command: command,
verbose: verbose,
config: configProps,
).notify();
}
22 changes: 20 additions & 2 deletions lib/environment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import 'dart:io';
/// Wraps access to Environment variables
/// Allows faking for testing
class Environment {
static String raygunAppIdKey = 'RAYGUN_APP_ID';
static String raygunTokenKey = 'RAYGUN_TOKEN';
static const String raygunAppIdKey = 'RAYGUN_APP_ID';
static const String raygunTokenKey = 'RAYGUN_TOKEN';
static const String raygunApiKeyKey = 'RAYGUN_API_KEY';

final String? raygunAppId;
final String? raygunToken;
final String? raygunApiKey;

static Environment? _instance;

Expand All @@ -27,14 +29,30 @@ class Environment {
Environment({
required this.raygunAppId,
required this.raygunToken,
required this.raygunApiKey,
});

String? operator [](String key) {
switch (key) {
case raygunAppIdKey:
return raygunAppId;
case raygunTokenKey:
return raygunToken;
case raygunApiKeyKey:
return raygunApiKey;
default:
throw ArgumentError('Unknown environment variable: $key');
}
}

factory Environment._init() {
final raygunAppId = Platform.environment[raygunAppIdKey];
final raygunToken = Platform.environment[raygunTokenKey];
final raygunApiKey = Platform.environment[raygunApiKeyKey];
return Environment(
raygunAppId: raygunAppId,
raygunToken: raygunToken,
raygunApiKey: raygunApiKey,
);
}
}
4 changes: 3 additions & 1 deletion lib/sourcemap/flutter/sourcemap_flutter.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'dart:io';

import 'package:raygun_cli/config_props.dart';
import 'package:raygun_cli/sourcemap/sourcemap_api.dart';
import 'package:raygun_cli/sourcemap/sourcemap_base.dart';

class SourcemapFlutter extends SourcemapBase {
SourcemapFlutter({
required super.command,
required super.verbose,
required super.config,
});

@override
Expand All @@ -19,6 +19,8 @@ class SourcemapFlutter extends SourcemapBase {
final uri =
command.option('uri') ?? '${command.option('base-uri')}main.dart.js';
final path = command.option('input-map') ?? 'build/web/main.dart.js.map';
final appId = ConfigProp.appId.load(command);
final token = ConfigProp.token.load(command);
if (verbose) {
print('app-id: $appId');
print('token: $token');
Expand Down
1 change: 0 additions & 1 deletion lib/sourcemap/node/sourcemap_node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class SourcemapNode extends SourcemapBase {
SourcemapNode({
required super.command,
required super.verbose,
required super.config,
});

@override
Expand Down
10 changes: 1 addition & 9 deletions lib/sourcemap/sourcemap_base.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import 'package:args/args.dart';

import '../config_props.dart';

abstract class SourcemapBase {
SourcemapBase({
required this.command,
required this.verbose,
required ConfigProps config,
}) {
appId = config.appId;
token = config.token;
}
});

final ArgResults command;
final bool verbose;
late final String appId;
late final String token;

Future<void> upload();
}
6 changes: 0 additions & 6 deletions lib/sourcemap/sourcemap_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import 'package:raygun_cli/sourcemap/flutter/sourcemap_flutter.dart';
import 'package:raygun_cli/sourcemap/node/sourcemap_node.dart';
import 'package:raygun_cli/sourcemap/sourcemap_single_file.dart';

import '../config_props.dart';

const kSourcemapCommand = 'sourcemap';

ArgParser buildParserSourcemap() {
Expand Down Expand Up @@ -56,26 +54,22 @@ void parseSourcemapCommand(ArgResults command, bool verbose) {
print(buildParserSourcemap().usage);
exit(0);
}
final configProps = ConfigProps.load(command, verbose: verbose);

switch (command.option('platform')) {
case null:
SourcemapSingleFile(
command: command,
verbose: verbose,
config: configProps,
).upload();
case 'flutter':
SourcemapFlutter(
command: command,
verbose: verbose,
config: configProps,
).upload();
case 'node':
SourcemapNode(
command: command,
verbose: verbose,
config: configProps,
).upload();
default:
print('Unsupported platform');
Expand Down
5 changes: 4 additions & 1 deletion lib/sourcemap/sourcemap_single_file.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'dart:io';

import 'package:raygun_cli/config_props.dart';
import 'package:raygun_cli/sourcemap/sourcemap_api.dart';
import 'package:raygun_cli/sourcemap/sourcemap_base.dart';

class SourcemapSingleFile extends SourcemapBase {
SourcemapSingleFile({
required super.command,
required super.verbose,
required super.config,
});

@override
Expand All @@ -24,6 +24,9 @@ class SourcemapSingleFile extends SourcemapBase {
}
final path = command.option('input-map')!;

final appId = ConfigProp.appId.load(command);
final token = ConfigProp.token.load(command);

if (verbose) {
print('app-id: $appId');
print('token: $token');
Expand Down
5 changes: 2 additions & 3 deletions lib/symbols/flutter_symbols.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ void parseSymbolsCommand(ArgResults command, bool verbose) {
print(buildParserSymbols().usage);
exit(0);
}
final configProps = ConfigProps.load(command, verbose: verbose);
_run(
command: command,
appId: configProps.appId,
token: configProps.token,
appId: ConfigProp.appId.load(command),
token: ConfigProp.token.load(command),
).then((result) {
if (result) {
exit(0);
Expand Down
Loading

0 comments on commit b1e0acd

Please sign in to comment.