Skip to content
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
43 changes: 19 additions & 24 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,26 @@ etc, etc, etc.
All flights are defined in `extension/flights.ts`.

## Logical Flights
Logical flights are, I guess you could call, "smart links".
Logical flights are, I guess you could call, "smart links".
They arent hardcoded into this repository and use logic to determine where you want to go.
Some logical flights (`gh` for example) may require some initial configuration (see below).
You can set defaults for anything within a logical flight.

### Github - `gh`
The `gh` logical flight works 2 ways.
#### Default Org (config required)
If you visit a specific github org often, you can set the org name so that during the flight you only have to specify the repo name.
Look at the `gh` logical flight as an example. Its logic is as follows: `https://github.com/${gh-org}/${repo}`.

Say you visit the `Apollorion` github org a lot.
If no defaults are specified, the extension will assume you will pass in at least 2 parameters to the flight.
`fly hg Apollorion manifests.io` is calculated positionally in this case. `Apollorion` will replace `${gh-org}` and `manifests.io` will replace `${repo}`.
Which will fly to `https://github.com/Apollorion/manifests.io`.

You can run `fly set gh-org Apollorion` and now the extension will assume the `Apollorion` org if an org isnt directly specified.
Flights like `fly gh manifests.io` will now take you to `https://github.com/Apollorion/manifests.io`.
#### Specifying Org
If you specify an org during your flight, it will use that org instead of the default one.
You can, optionally, set a default for any parameter in a logical flight.
`fly set gh-org Apollorion` will set the default for `gh-org` to `Apollorion`.
Now, when you fly the `gh` flight, it will first replace `${gh-org}` with `Apollorion` and then any remaining parameters will be replaced positionally.
So you could do `fly gh manifests.io` and it will fly to `https://github.com/Apollorion/manifests.io`.

Flights like `fly gh Apollorion manifests.io` will take you to `https://github.com/Apollorion/manifests.io`.

### AWS - `aws`
The `aws` logical flight will take you to the default console home (`https://console.aws.amazon.com/console/home`) if your flight is just `fly aws`.
Alternatively, you can go to a specific region via `fly aws us-east-1`.
Note: ANY unset parameters will be replaced positionally, in order.
If you have a logical flight that is defined as `https://my.com/{$thing1}/{$thing2}/{$thing3}`, and you only set the default for `thing1`.
The extension will expect you to pass in 2 parameters at flight time, otherwise a default flight will be used or an error will be thrown.
`fly my.com test1 test2` will fly to `https://my.com/{thing1 default}/test1/test2/`.
`fly my.com test` will either go to a defined default flight or throw an error.

# Custom flight repos
You can host a custom flight repo and point the extension to it. This will allow you to define standard and logical flights without contributing to this project.
Expand Down Expand Up @@ -87,16 +86,12 @@ interface CustomFlightRepo {
To use the custom flight repo, just run `fly set repo $url`. You can unset this repo at anytime via `fly unset repo`.

# Devving this repo
I'd like to make devving a little less painful somehow, but right now what you need to do is:
1. cd extension
2. yarn install
3. yarn build
4. test things
5. repeat steps 3, 4 infinitely

I know things like nodemon exist, but it doesnt play well with chrome extensions and I havent figured out why quite yet.

Startup the dev server via `./dev.sh`.
You need to `load unpacked` the extension in the `extension/dist` dir after building at least once.

I havent found a work around yet, but whenever you make a change you need to refresh the extension in `chrome://extensions`.


## Logical Flights
Say a users query is `fly gh Apollorion manifests.io`
Expand Down
16 changes: 16 additions & 0 deletions dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

function rebuild(){
echo "Rebuilding"
yarn build
export FLY_RELEASE_VERSION="0.0.1"
envsubst < ./extension/manifest.json > ./dist/manifest.json
cp ./extension/img.png ./dist/img.png
echo "Happy Coding!"
}
export -f rebuild

yarn install
rebuild

fswatch -o ./extension | xargs -t -n1 -P2 bash -c 'rebuild "$@"' _
110 changes: 58 additions & 52 deletions extension/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,11 @@ async function main() {
} else {
const repoFlightResponse = await repoFlightResponsePromise;
const flight = await getFlightFromQuery(query, repoFlightResponse);
if(flight.type == FlightType.LOGICAL){
switch(flight.identifier){
case "gh":
await goToGithub(flight);
break;
default:
await handleFlight(flight);
break;
}
} else {
await handleFlight(flight);
}
await handleFlight(flight);
}

});
}

async function goToGithub(flight: Flight){
if(flight.values !== undefined && flight.values.length === 2) {
return await handleFlight(flight);
}

try {
let org = await getLocalStorage("gh-org");
let values: string[] = [];
if(flight.values !== undefined){
values = flight.values;
}
const newFlight: Flight = {
type: flight.type,
identifier: flight.identifier,
override: flight.override,
values: [org, ...values]
}
await handleFlight(newFlight);
} catch {
const newFlight: Flight = {
type: FlightType.STANDARD,
identifier: "config-github/org-not-set"
};
await handleFlight(newFlight);
}
}

async function handleStandardFlight(flight: Flight){
const repoFlightResponse = await repoFlightResponsePromise;
let newFlights = getNewFlightResponseSynchronous(repoFlightResponse);
Expand All @@ -93,23 +54,68 @@ async function handleFlight(flight: Flight){
if(flight.type === FlightType.LOGICAL && flight.values !== undefined) {
if (flight.identifier in newLogicalFlights) {
let flightDetails = newLogicalFlights[flight.identifier];
let logic = flightDetails["logic"];

// Regex that match everything inside ${}
let regex = /\${([^}]*)}/g;
let matches = logic.match(regex);

if(matches === null) {
console.log("Matches is null");

// If the length of matches and the flight values are the same
// The user wants to supply all the values
} else if (matches.length === flight.values.length) {
let i = 0;
for(let match of matches){
const value = flight.values[i];
if(value !== undefined) {
logic = logic.replace(match, value);
i++;
}
}

if(flight.values.length === 0){
if(flight.override !== undefined) {
return await redirect("override flight", flight.override);
} else {
return await redirect("Incorrect Length", "https://github.com/Apollorion/fly/blob/main/help/logical-logicalFlights.md#incorrect-length");
} else {
for(let match of matches){
try {
const matchNoBrackets = match.replace("${", "").replace("}", "");
const value = await getLocalStorage(matchNoBrackets);
if(value !== undefined) {
logic = logic.replace(match, value);

// Remove the item from the array, so we can replace using the values of the flight later
const index = matches.indexOf(match);
if (index > -1) {
matches.splice(index, 1); // 2nd parameter means remove one item only
}
}
} catch {
console.log("Could not find value for: " + match);
}
}
}

let link = flightDetails["logic"];
let i = 1;
for (let item of flight.values) {
link = link.replace(`$${i}`, item);
i++;
// If there are still matches left, we will treat them as positional variables
let i = 0;
for(let match of matches){
const value = flight.values[i];
if(value !== undefined) {
logic = logic.replace(match, value);
i++;
}
}
}

return await redirect(`Following Link`, link);
if(logic.includes("${") && logic.includes("}")){
if(flight.values.length === 0){
if(flight.override !== undefined) {
return await redirect("override flight", flight.override);
} else {
return await redirect("Incorrect Length", "https://github.com/Apollorion/fly/blob/main/help/logical-logicalFlights.md#incorrect-length");
}
}
} else {
return await redirect("Following Link", logic);
}

} else {
return await redirect("Logic Not Found", "https://github.com/Apollorion/fly/blob/main/help/logical-logicalFlights.md#logic-not-found");
Expand Down
4 changes: 2 additions & 2 deletions extension/flights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import {LogicalFlightDefinition, StandardFlightDefinition} from "./types.js";
export const logicalFlights: LogicalFlightDefinition = {
"aws": {
"override": "https://console.aws.amazon.com/console/home",
"logic": "https://console.aws.amazon.com/console/home?region=$1"
"logic": "https://console.aws.amazon.com/console/home?region=${region}",
},
"gh": {
"logic": "https://github.com/$1/$2"
"logic": "https://github.com/${gh-org}/${repo}",
}
};

Expand Down
2 changes: 1 addition & 1 deletion extension/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export async function getLocalStorage(key: string): Promise<string>{
}

export async function redirect(message: string, link: string){
const dev = false;
const dev = true;
console.log(message, link);
if(!dev){
console.log("following link", link);
Expand Down