Skip to content

Commit

Permalink
✨ Use env files for handling secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Nov 13, 2022
1 parent da1eebf commit 5347cd7
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 45 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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=
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ build/
# macOS specific
.DS_Store

bin/query_cache/
bin/update_cache/
bin/*_cache/
sign.sh

.env

6 changes: 3 additions & 3 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,11 +48,11 @@ void main(List<String> arguments) {
List<String> 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();

Expand Down
13 changes: 6 additions & 7 deletions bin/main_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 <enter> to auto-update to a new version of this workflow.',
arg: 'update:workflow',
match:
'Auto-Update available! Press <enter> to auto-update to a new version of this workflow.',
'Auto-Update available! Press <enter> to auto-update to a new version of this workflow.',
icon: AlfredItemIcon(path: 'alfredhatcog.png'),
valid: true,
);
Expand All @@ -37,7 +36,7 @@ Future<void> _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,
Expand All @@ -59,7 +58,7 @@ Future<void> _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(
Expand All @@ -75,4 +74,4 @@ Future<void> _performSearch(String query, {String? version}) async {
),
);
}
}
}
24 changes: 0 additions & 24 deletions bin/src/constants/config.dart

This file was deleted.

2 changes: 2 additions & 0 deletions bin/src/env/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.g.dart
!.gitignore
26 changes: 26 additions & 0 deletions bin/src/env/env.dart
Original file line number Diff line number Diff line change
@@ -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<String> supportedVersions = _supportedVersions.split(',');
}
10 changes: 5 additions & 5 deletions bin/src/services/algolia_search.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
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<AlgoliaQuerySnapshot> query(
String queryString, {
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)
Expand Down
2 changes: 1 addition & 1 deletion info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
<key>variablesdontexport</key>
<array/>
<key>version</key>
<string>2.1.4</string>
<string>2.1.5</string>
<key>webaddress</key>
<string>https://github.com/techouse</string>
</dict>
Expand Down
23 changes: 22 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
1 change: 0 additions & 1 deletion version

This file was deleted.

0 comments on commit 5347cd7

Please sign in to comment.