Skip to content

Commit

Permalink
Merge pull request #1 from LosEagle/config
Browse files Browse the repository at this point in the history
Add config file support and production build setup
  • Loading branch information
miloslavnosek authored Apr 18, 2021
2 parents 63bd7c8 + 7745ce8 commit b9a2517
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 47 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ freetube_updater-*.tar

# Temporary files for e.g. tests
/tmp

config/config.json
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

An app that makes updating FreeTube from nightly build artifacts more automated.

Usage:
```
FreeTubeUpdater.get_download_link(personal_access_token, os, format)
```

Supported parameters are as follows:
os: win, linux, mac
format:
- for win:
- setup, portable
- for linux:
- portable, AppImage
- for mac:
- dmg
There is a simple one-time setup for using this app:
- copy `config.sample.json` to `config.json` in `/rel/freetube_updater/config`
- fill in the three values in file
- token is your GitHub personal access token. You can easily generate one in https://github.com/settings/tokens
- I know this is inconvenient, but it takes just a minute or two and it's necessary, because
GitHub doesn't allow downloading artifacts for non-logged users.
- os value accepts `win`, `linux` or `mac`
- format value depends on your `os` type
- `win` accepts `setup` or `portable`
- `linux` accepts `portable` or `AppImage`
- `mac` accepts `dmg`

- finally, you can start `run` script file or use command `rel/freetube_updater/bin/freetube_updater start`
from the app root directory which will give you a download link for newest release of FreeTube
5 changes: 5 additions & 0 deletions config/config.sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"token": "",
"os": "",
"format": ""
}
45 changes: 32 additions & 13 deletions lib/freetube_updater.ex
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
defmodule FreeTubeUpdater do
def get_download_link(token, os, format) do
@env Mix.env()

def env, do: @env

def get_download_link() do
config_path =
case FreeTubeUpdater.env() do
:dev -> File.cwd!() <> "/config/config.json"
:prod -> File.cwd!() <> "/rel/freetube_updater/config/config.json"
end

config = FreeTubeUpdater.Config.parse!(config_path)

nightly_artifact_url = "https://api.github.com/repos/freetubeapp/freetube/actions/artifacts"

nightly_artifacts = FreeTubeUpdater.GithubApiHandler.send_authenticated_request(nightly_artifact_url, token)
|> FreeTubeUpdater.GithubApiHandler.get_response_body
|> get_in(["artifacts"])
nightly_artifacts =
FreeTubeUpdater.GithubApiHandler.send_authenticated_request(
nightly_artifact_url,
config["token"]
)
|> FreeTubeUpdater.GithubApiHandler.get_response_body()
|> get_in(["artifacts"])

download_link =
nightly_artifacts
|> FreeTubeUpdater.ArtifactParser.get_artifact(config["os"], config["format"])
|> get_in(["archive_download_url"])
|> FreeTubeUpdater.GithubApiHandler.send_authenticated_request(config["token"])
|> FreeTubeUpdater.GithubApiHandler.get_response_headers()
|> Enum.filter(fn {key, _value} -> key === "Location" end)
|> Enum.at(0)
|> Tuple.to_list()
|> Enum.at(1)

nightly_artifacts
|> FreeTubeUpdater.ArtifactParser.get_artifact(os, format)
|> get_in(["archive_download_url"])
|> FreeTubeUpdater.GithubApiHandler.send_authenticated_request(token)
|> FreeTubeUpdater.GithubApiHandler.get_response_headers
|> Enum.filter(fn ({ key, _value }) -> key === "Location" end)
|> Enum.at(0)
|> Tuple.to_list
|> Enum.at(1)
{":ok", download_link}
end
end
2 changes: 2 additions & 0 deletions lib/freetube_updater/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule FreeTubeUpdater.Application do

@impl true
def start(_type, _args) do
IO.inspect(FreeTubeUpdater.get_download_link())

children = [
# Starts a worker by calling: FreeTubeUpdater.Worker.start_link(arg)
# {FreeTubeUpdater.Worker, arg}
Expand Down
41 changes: 22 additions & 19 deletions lib/freetube_updater/artifact_parser/artifact_parser.ex
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
defmodule FreeTubeUpdater.ArtifactParser do
def get_artifact(artifacts, os, format) do
patterns = %{
win: %{
setup: ~r/setup-x64.exe/,
portable: ~r/portable-x64.exe/,
},
linux: %{
portable: ~r/linux_portable_x64/,
AppImage: ~r/amd64.AppImage/,
},
mac: %{
dmg: ~r/mac.dmg/
}
}

Enum.find(artifacts, &(String.match?(&1["name"], get_in(patterns, [String.to_atom(os), String.to_atom(format)]))))
end
end
defmodule FreeTubeUpdater.ArtifactParser do
def get_artifact(artifacts, os, format) do
patterns = %{
win: %{
setup: ~r/setup-x64.exe/,
portable: ~r/portable-x64.exe/
},
linux: %{
portable: ~r/linux_portable_x64/,
AppImage: ~r/amd64.AppImage/
},
mac: %{
dmg: ~r/mac.dmg/
}
}

Enum.find(
artifacts,
&String.match?(&1["name"], get_in(patterns, [String.to_atom(os), String.to_atom(format)]))
)
end
end
6 changes: 6 additions & 0 deletions lib/freetube_updater/config/config.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule FreeTubeUpdater.Config do
def parse!(path) do
File.read!(path)
|> Poison.decode!()
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule FreeTubeUpdater.GithubApiHandler do
end

def send_authenticated_request(url, token) do
headers = ["Authorization": "Bearer #{token}", "Accept": "Application/json; Charset=utf-8"]
headers = [Authorization: "Bearer #{token}", Accept: "Application/json; Charset=utf-8"]

HTTPoison.get!(url, headers)
end
Expand Down

0 comments on commit b9a2517

Please sign in to comment.