-
Notifications
You must be signed in to change notification settings - Fork 0
feat(cli): add favorite teams/projects, filtering list views by them #61
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ca76dd6
docs: add phase 10 and phase 11 plans - favorite teams/projects, then…
bougyman 9ff3674
feat(cli): add favorite teams/projects, filtering list views by them
bougyman f42e610
test(nope): use unknown type for unknown type test
bougyman 75cefae
docs(cli): clarify --all only bypasses favorites, not mine/team scope
bougyman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| defmodule LinearCli.Favorites do | ||
| @moduledoc """ | ||
| Favorited teams/projects, persisted in the same local SQLite file | ||
| `LinearCli.Profiles` uses (`:profiles_db_path` - see | ||
| `config/runtime.exs`), in their own `favorites` table. Reuses | ||
| `LinearCli.Profiles`'s exact pattern (raw `Exqlite.Sqlite3`, one | ||
| connection open/ensure-schema/close per call, no `Ecto.Repo`) rather | ||
| than sharing code with it - see `documents/phase-10-plan.adoc` for why | ||
| this stays its own module against the same file instead of a change to | ||
| `LinearCli.Profiles` itself. | ||
|
|
||
| `list/1`, once non-empty for a given `kind`, is what | ||
| `LinearCli.CLI.Commands.team_list/1`/`project_list/1` filter their | ||
| results down to by default (a new `--all` flag opts back out). | ||
|
|
||
| New in this port - Ruby has no equivalent. | ||
| """ | ||
|
|
||
| alias Exqlite.Sqlite3 | ||
|
|
||
| @doc "Favorites `value` under `kind` (`\"team\"` or `\"project\"`). A no-op if already favorited." | ||
| @spec add(String.t(), String.t()) :: :ok | ||
| def add(kind, value) do | ||
| with_db(fn conn -> | ||
| exec(conn, "INSERT OR IGNORE INTO favorites (kind, value) VALUES (?, ?)", [kind, value]) | ||
| end) | ||
| end | ||
|
|
||
| @doc "Un-favorites `value` under `kind`. A no-op if it wasn't favorited." | ||
| @spec remove(String.t(), String.t()) :: :ok | ||
| def remove(kind, value) do | ||
| with_db(fn conn -> | ||
| exec(conn, "DELETE FROM favorites WHERE kind = ? AND value = ?", [kind, value]) | ||
| end) | ||
| end | ||
|
|
||
| @doc "Every favorited value under `kind`, ordered by value." | ||
| @spec list(String.t()) :: [String.t()] | ||
| def list(kind) do | ||
| with_db(fn conn -> | ||
| conn | ||
| |> query("SELECT value FROM favorites WHERE kind = ? ORDER BY value", [kind]) | ||
| |> Enum.map(fn [value] -> value end) | ||
| end) | ||
| end | ||
|
|
||
| defp with_db(fun) do | ||
| path = db_path() | ||
| File.mkdir_p!(Path.dirname(path)) | ||
| {:ok, conn} = Sqlite3.open(path) | ||
|
|
||
| try do | ||
| ensure_schema!(conn) | ||
| fun.(conn) | ||
| after | ||
| Sqlite3.close(conn) | ||
| end | ||
| end | ||
|
|
||
| defp ensure_schema!(conn) do | ||
| :ok = | ||
| exec( | ||
| conn, | ||
| """ | ||
| CREATE TABLE IF NOT EXISTS favorites ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| kind TEXT NOT NULL, | ||
| value TEXT NOT NULL, | ||
| UNIQUE(kind, value) | ||
| ) | ||
| """, | ||
| [] | ||
| ) | ||
| end | ||
|
|
||
| defp exec(conn, sql, []), do: Sqlite3.execute(conn, sql) | ||
|
|
||
| defp exec(conn, sql, params) do | ||
| with {:ok, stmt} <- Sqlite3.prepare(conn, sql), | ||
| :ok <- Sqlite3.bind(stmt, params), | ||
| :done <- Sqlite3.step(conn, stmt) do | ||
| :ok | ||
| end | ||
| end | ||
|
|
||
| defp query(conn, sql, params) do | ||
| {:ok, stmt} = Sqlite3.prepare(conn, sql) | ||
| :ok = Sqlite3.bind(stmt, params) | ||
| {:ok, rows} = Sqlite3.fetch_all(conn, stmt) | ||
| rows | ||
| end | ||
|
|
||
| defp db_path, do: Application.fetch_env!(:linear_cli, :profiles_db_path) | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.