From 5347cd75ea136ea824cc7108ee711a5e50e61ad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Sun, 13 Nov 2022 10:44:53 +0000 Subject: [PATCH] :sparkles: Use env files for handling secrets --- .env.example | 6 ++++++ .gitignore | 6 ++++-- bin/main.dart | 6 +++--- bin/main_helpers.dart | 13 ++++++------- bin/src/constants/config.dart | 24 ------------------------ bin/src/env/.gitignore | 2 ++ bin/src/env/env.dart | 26 ++++++++++++++++++++++++++ bin/src/services/algolia_search.dart | 10 +++++----- info.plist | 2 +- pubspec.lock | 23 ++++++++++++++++++++++- pubspec.yaml | 7 ++++++- version | 1 - 12 files changed, 81 insertions(+), 45 deletions(-) create mode 100644 .env.example delete mode 100644 bin/src/constants/config.dart create mode 100644 bin/src/env/.gitignore create mode 100644 bin/src/env/env.dart delete mode 100644 version diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..90c6628 --- /dev/null +++ b/.env.example @@ -0,0 +1,6 @@ +APP_VERSION=2.1.5 +GITHUB_REPOSITORY_URL=https://github.com/techouse/alfred-django-docs +SUPPORTED_VERSIONS=v1.8,v1.10,v1.11,v2,v2.1,v2.2,v3,v3.1,v3.2,v4,v4.1 +ALGOLIA_SEARCH_INDEX= +ALGOLIA_APPLICATION_ID= +ALGOLIA_SEARCH_ONLY_API_KEY= diff --git a/.gitignore b/.gitignore index 124e4c7..b64006e 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,8 @@ build/ # macOS specific .DS_Store -bin/query_cache/ -bin/update_cache/ +bin/*_cache/ sign.sh + +.env + diff --git a/bin/main.dart b/bin/main.dart index 96f6bdf..81b3ed9 100644 --- a/bin/main.dart +++ b/bin/main.dart @@ -13,7 +13,7 @@ import 'package:args/args.dart' show ArgParser, ArgResults; import 'package:cli_script/cli_script.dart'; import 'package:collection/collection.dart' show IterableExtension; -import 'src/constants/config.dart' show Config; +import 'src/env/env.dart'; import 'src/extensions/string_helpers.dart' show StringHelpers; import 'src/models/search_result.dart' show SearchResult; import 'src/services/algolia_search.dart' show AlgoliaSearch; @@ -48,11 +48,11 @@ void main(List arguments) { List query = args['query'].replaceAll(RegExp(r'\s+'), ' ').trim().split(' '); String? version = - query.firstWhereOrNull((el) => Config.supportedVersions.contains(el)); + query.firstWhereOrNull((el) => Env.supportedVersions.contains(el)); if (version != null) { query.removeWhere((str) => str == version); } else { - version = Config.supportedVersions.last; + version = Env.supportedVersions.last; } final String queryString = query.join(' ').trim().toLowerCase(); diff --git a/bin/main_helpers.dart b/bin/main_helpers.dart index 092954b..9290e2c 100644 --- a/bin/main_helpers.dart +++ b/bin/main_helpers.dart @@ -3,18 +3,17 @@ part of 'main.dart'; final AlfredWorkflow _workflow = AlfredWorkflow(); final AlfredUpdater _updater = AlfredUpdater( - githubRepositoryUrl: Config.githubRepositoryUrl, - currentVersion: Config.version, + githubRepositoryUrl: Uri.parse(Env.githubRepositoryUrl), + currentVersion: Env.appVersion, updateInterval: Duration(days: 7), ); - const _updateItem = AlfredItem( title: 'Auto-Update available!', subtitle: 'Press to auto-update to a new version of this workflow.', arg: 'update:workflow', match: - 'Auto-Update available! Press to auto-update to a new version of this workflow.', + 'Auto-Update available! Press to auto-update to a new version of this workflow.', icon: AlfredItemIcon(path: 'alfredhatcog.png'), valid: true, ); @@ -37,7 +36,7 @@ Future _performSearch(String query, {String? version}) async { if (snapshot.nbHits > 0) { final AlfredItems items = AlfredItems( snapshot.hits.map((snapshot) => SearchResult.fromJson(snapshot.data)).map( - (result) { + (result) { return AlfredItem( uid: result.objectID, title: result.prettyTitle, @@ -59,7 +58,7 @@ Future _performSearch(String query, {String? version}) async { _workflow.addItems(items.items); } else { final Uri url = - Uri.https('www.google.com', '/search', {'q': 'Django $query'}); + Uri.https('www.google.com', '/search', {'q': 'Django $query'}); _workflow.addItem( AlfredItem( @@ -75,4 +74,4 @@ Future _performSearch(String query, {String? version}) async { ), ); } -} \ No newline at end of file +} diff --git a/bin/src/constants/config.dart b/bin/src/constants/config.dart deleted file mode 100644 index 8670051..0000000 --- a/bin/src/constants/config.dart +++ /dev/null @@ -1,24 +0,0 @@ -class Config { - Config._(); - - static const String version = '2.1.4'; - static final Uri githubRepositoryUrl = - Uri.https('github.com', '/techouse/alfred-django-docs'); - static const String algoliaApplicationId = 'WODHKE4WZG'; - static const String algoliaSearchOnlyApiKey = - '7456cdd91ba8d4f87846549697397759'; - static const String algoliaSearchIndex = 'docs'; - static const List supportedVersions = [ - 'v1.8', - 'v1.10', - 'v1.11', - 'v2', - 'v2.1', - 'v2.2', - 'v3', - 'v3.1', - 'v3.2', - 'v4', - 'v4.1', - ]; -} diff --git a/bin/src/env/.gitignore b/bin/src/env/.gitignore new file mode 100644 index 0000000..ae390e0 --- /dev/null +++ b/bin/src/env/.gitignore @@ -0,0 +1,2 @@ +*.g.dart +!.gitignore \ No newline at end of file diff --git a/bin/src/env/env.dart b/bin/src/env/env.dart new file mode 100644 index 0000000..823cfe1 --- /dev/null +++ b/bin/src/env/env.dart @@ -0,0 +1,26 @@ +import 'package:envied/envied.dart'; + +part 'env.g.dart'; + +@Envied(path: '.env') +abstract class Env { + @EnviedField(varName: 'APP_VERSION') + static const String appVersion = _Env.appVersion; + + @EnviedField(varName: 'GITHUB_REPOSITORY_URL') + static const String githubRepositoryUrl = _Env.githubRepositoryUrl; + + @EnviedField(varName: 'ALGOLIA_APPLICATION_ID', obfuscate: true) + static final String algoliaApplicationId = _Env.algoliaApplicationId; + + @EnviedField(varName: 'ALGOLIA_SEARCH_ONLY_API_KEY', obfuscate: true) + static final String algoliaSearchOnlyApiKey = _Env.algoliaSearchOnlyApiKey; + + @EnviedField(varName: 'ALGOLIA_SEARCH_INDEX', obfuscate: true) + static final String algoliaSearchIndex = _Env.algoliaSearchIndex; + + @EnviedField(varName: 'SUPPORTED_VERSIONS') + static const String _supportedVersions = _Env._supportedVersions; + + static final List supportedVersions = _supportedVersions.split(','); +} diff --git a/bin/src/services/algolia_search.dart b/bin/src/services/algolia_search.dart index cf95131..68e2c42 100644 --- a/bin/src/services/algolia_search.dart +++ b/bin/src/services/algolia_search.dart @@ -1,15 +1,15 @@ import 'package:algolia/algolia.dart' show Algolia, AlgoliaQuery, AlgoliaQuerySnapshot; -import '../constants/config.dart'; +import '../env/env.dart'; import '../models/search_result.dart'; class AlgoliaSearch { AlgoliaSearch._(); static final Algolia _algolia = Algolia.init( - applicationId: Config.algoliaApplicationId, - apiKey: Config.algoliaSearchOnlyApiKey, + applicationId: Env.algoliaApplicationId, + apiKey: Env.algoliaSearchOnlyApiKey, ); static Future query( @@ -17,10 +17,10 @@ class AlgoliaSearch { String? version, }) async { final AlgoliaQuery query = _algolia.instance - .index(Config.algoliaSearchIndex) + .index(Env.algoliaSearchIndex) .query(queryString) .facetFilter( - 'version:${(version ?? Config.supportedVersions.last).replaceAll('v', '')}', + 'version:${(version ?? Env.supportedVersions.last).replaceAll('v', '')}', ) .setAttributesToRetrieve(SearchResult.attributesToRetrieve) .setPage(0) diff --git a/info.plist b/info.plist index ee546fc..70f4e24 100644 --- a/info.plist +++ b/info.plist @@ -215,7 +215,7 @@ variablesdontexport version - 2.1.4 + 2.1.5 webaddress https://github.com/techouse diff --git a/pubspec.lock b/pubspec.lock index 6000e4c..625196a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -23,7 +23,7 @@ packages: source: hosted version: "1.1.1" analyzer: - dependency: transitive + dependency: "direct overridden" description: name: analyzer url: "https://pub.dartlang.org" @@ -204,6 +204,27 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.2.4" + dotenv: + dependency: transitive + description: + name: dotenv + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.1" + envied: + dependency: "direct main" + description: + name: envied + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.4" + envied_generator: + dependency: "direct dev" + description: + name: envied_generator + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.3+1" equatable: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 47a8fd3..ff2eb51 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ description: Search the Django documentation using Alfred # Prevent accidental publishing to pub.dev. publish_to: 'none' -version: 2.1.4 +version: 2.1.5 environment: sdk: '>=2.17.0 <3.0.0' @@ -14,11 +14,16 @@ dependencies: args: ^2.3.0 alfred_workflow: ^0.2.3 cli_script: ^0.2.6 + envied: ^0.2.3 collection: ^1.16.0 json_annotation: ^4.7.0 dev_dependencies: build_runner: ^2.1.7 dart_code_metrics: ^5.0.1 + envied_generator: ^0.2.3 json_serializable: ^6.1.5 lints: ^2.0.0 + +dependency_overrides: + analyzer: ^5.2.0 diff --git a/version b/version deleted file mode 100644 index c346e7a..0000000 --- a/version +++ /dev/null @@ -1 +0,0 @@ -2.1.4 \ No newline at end of file