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: #6 support environment variables #12

Merged
merged 10 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
workflow_dispatch:

jobs:
ckeck:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -23,9 +23,8 @@ jobs:
- name: Analyze project source
run: dart analyze

# No tests to run yet
# - name: Run tests
# run: dart test
- name: Run tests
run: dart test

build-linux:
runs-on: ubuntu-latest
Expand Down
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,22 @@ Or use directly from sources:
dart bin/raygun_cli.dart <command> <arguments>
```

**Common mandatory arguments**
#### Configuration parameters

- `app-id` the Application ID in Raygun.com.
- `token` is an access token from https://app.raygun.com/user/tokens.
All `raygun-cli` commands share the same configuration parameters.

- App ID: The Application ID in Raygun.com.
- Token: An access token from https://app.raygun.com/user/tokens.

You can pass these parameters via arguments, e.g. `--app-id=<id>`
or you can set them as environment variables.

Parameters passed as arguments have priority over environment variables.

| Parameter | Argument | Environment Variable |
|-----------|----------|----------------------|
| App ID | `app-id` | `RAYGUN_APP_ID` |
| Token | `token` | `RAYGUN_TOKEN` |

#### Sourcemap Uploader

Expand Down
12 changes: 10 additions & 2 deletions bin/raygun_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:args/args.dart';
import 'package:raygun_cli/sourcemap/sourcemap_command.dart';
import 'package:raygun_cli/symbols/flutter_symbols.dart';

const String version = '0.0.1';
const String version = '0.0.2';

ArgParser buildParser() {
return ArgParser()
Expand Down Expand Up @@ -34,8 +34,16 @@ ArgParser buildParser() {
}

void printUsage(ArgParser argParser) {
print('Usage: raygun-cli <flags> [arguments]');
print('Raygun CLI: $version');
print('');
print('Usage: raygun-cli <command> <arguments>');
print(argParser.usage);
print('');
print('Commands:');
for (final command in argParser.commands.keys) {
print(' $command');
}
print('');
}

void main(List<String> arguments) {
Expand Down
62 changes: 62 additions & 0 deletions lib/config_props.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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,
});

/// 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'];
} else {
token = Environment.instance.raygunToken;
}

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

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

return ConfigProps._(
appId: appId,
token: token,
);
}
}
40 changes: 40 additions & 0 deletions lib/environment.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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';

final String? raygunAppId;
final String? raygunToken;

static Environment? _instance;

/// Singleton instance access
/// Will init if not already
static Environment get instance {
_instance ??= Environment._init();
return _instance!;
}

/// For testing purposes
static void setInstance(Environment instance) {
_instance = instance;
}

/// Create custom instance
Environment({
required this.raygunAppId,
required this.raygunToken,
});

factory Environment._init() {
final raygunAppId = Platform.environment[raygunAppIdKey];
final raygunToken = Platform.environment[raygunTokenKey];
return Environment(
raygunAppId: raygunAppId,
raygunToken: raygunToken,
);
}
}
1 change: 1 addition & 0 deletions lib/sourcemap/flutter/sourcemap_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class SourcemapFlutter extends SourcemapBase {
SourcemapFlutter({
required super.command,
required super.verbose,
required super.config,
});

@override
Expand Down
1 change: 1 addition & 0 deletions lib/sourcemap/node/sourcemap_node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class SourcemapNode extends SourcemapBase {
SourcemapNode({
required super.command,
required super.verbose,
required super.config,
});

@override
Expand Down
7 changes: 5 additions & 2 deletions lib/sourcemap/sourcemap_base.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'package:args/args.dart';

import '../config_props.dart';

abstract class SourcemapBase {
SourcemapBase({
required this.command,
required this.verbose,
required ConfigProps config,
}) {
appId = command.option('app-id')!;
token = command.option('token')!;
appId = config.appId;
token = config.token;
}

final ArgResults command;
Expand Down
29 changes: 19 additions & 10 deletions lib/sourcemap/sourcemap_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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 All @@ -18,12 +20,10 @@ ArgParser buildParserSourcemap() {
..addOption(
'app-id',
help: 'Raygun\'s application ID',
mandatory: true,
)
..addOption(
'token',
help: 'Raygun\'s access token',
mandatory: true,
)
..addOption(
'platform',
Expand Down Expand Up @@ -56,18 +56,27 @@ void parseSourcemapCommand(ArgResults command, bool verbose) {
print(buildParserSourcemap().usage);
exit(0);
}
if (!command.wasParsed('app-id') || !command.wasParsed('token')) {
print('Missing mandatory arguments');
print(buildParserSourcemap().usage);
exit(2);
}
final configProps = ConfigProps.load(command, verbose: verbose);

switch (command.option('platform')) {
case null:
SourcemapSingleFile(command: command, verbose: verbose).upload();
SourcemapSingleFile(
command: command,
verbose: verbose,
config: configProps,
).upload();
case 'flutter':
SourcemapFlutter(command: command, verbose: verbose).upload();
SourcemapFlutter(
command: command,
verbose: verbose,
config: configProps,
).upload();
case 'node':
SourcemapNode(command: command, verbose: verbose).upload();
SourcemapNode(
command: command,
verbose: verbose,
config: configProps,
).upload();
default:
print('Unsupported platform');
exit(1);
Expand Down
1 change: 1 addition & 0 deletions lib/sourcemap/sourcemap_single_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class SourcemapSingleFile extends SourcemapBase {
SourcemapSingleFile({
required super.command,
required super.verbose,
required super.config,
});

@override
Expand Down
13 changes: 4 additions & 9 deletions lib/symbols/flutter_symbols.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:io';

import 'package:args/args.dart';
import 'package:raygun_cli/config_props.dart';
import 'package:raygun_cli/symbols/flutter_symbols_api.dart';

const kSymbolsCommand = 'symbols';
Expand All @@ -12,15 +13,11 @@ void parseSymbolsCommand(ArgResults command, bool verbose) {
print(buildParserSymbols().usage);
exit(0);
}
if (!command.wasParsed('app-id') || !command.wasParsed('token')) {
print('Missing mandatory arguments');
print(buildParserSymbols().usage);
exit(2);
}
final configProps = ConfigProps.load(command, verbose: verbose);
_run(
command: command,
appId: command['app-id'],
token: command['token'],
appId: configProps.appId,
token: configProps.token,
).then((result) {
if (result) {
exit(0);
Expand Down Expand Up @@ -88,12 +85,10 @@ ArgParser buildParserSymbols() {
..addOption(
'app-id',
help: 'Raygun\'s application ID',
mandatory: true,
)
..addOption(
'token',
help: 'Raygun\'s access token',
mandatory: true,
)
..addOption(
'path',
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,4 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.5.0 <4.0.0"
dart: ">=3.6.0 <4.0.0"
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: raygun_cli
description: Command-line tool for Raygun.com
# Update version in bin/raygun_cli.dart as well
version: 0.0.2
repository: https://github.com/MindscapeHQ/raygun-cli/
homepage: https://raygun.com
Expand Down
Empty file removed test/.gitkeep
Empty file.
Loading
Loading