Skip to content

Commit

Permalink
added keyring
Browse files Browse the repository at this point in the history
  • Loading branch information
sepehrrasooli authored Sep 16, 2024
1 parent 415b44a commit 9dec823
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,11 @@ pip install git+http://github.com/lnxpy/hey.git
</details>

<details>
<summary><h4>2. Set the <code>HEY_TOKEN</code> environment variable</h4></summary>
<summary><h4>2. Set the <code>HEY_TOKEN</code></h4></summary>

Once you got the package installed on your system, it's time to add the token that you just copied from [mdb.ai](https://mdb.ai) into either the `.bashrc` (or `.zshrc`) file.

- If you use the default bash shell
```sh
echo "export HEY_TOKEN=<TOKEN>" >> ~/.bashrc
```
- If you use ZSH
```sh
echo "export HEY_TOKEN=<TOKEN>" >> ~/.zshrc
```
Once you got the package installed on your system, it's time to add the token that you just copied from [mdb.ai](https://mdb.ai) into hey, use `hey auth` to add your token.

> :warning:: Your token will not be echoed.
</details>
### Usage
Expand Down
11 changes: 11 additions & 0 deletions hey/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from typing import Annotated, Optional

import typer
import keyring
import getpass
from rich.markdown import Markdown
from rich.panel import Panel

Expand All @@ -10,6 +12,7 @@
from hey.consts import APP_NAME
from hey.editor import open_tmp_editor
from hey.openai import answer
from hey.consts import HEY_TOKEN_KEY

app = typer.Typer()
app.add_typer(cli.app, name="config")
Expand Down Expand Up @@ -74,3 +77,11 @@ def ask(

result = answer(user_input, no_style)
console.print(result)

@app.command()
def auth():
"""
Take HEY_TOKEN from user.
"""
password = getpass.getpass("Copy and paste your token here, [Token will not be echoed] > ")
keyring.set_password("system",HEY_TOKEN_KEY,password)
1 change: 1 addition & 0 deletions hey/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# App name
APP_NAME = "Hey"
HEY_TOKEN_KEY = "HEY_TOKEN"

if platform.system().lower() == "windows":
DEFAULT_EDITOR = os.environ.get("EDITOR", "notepad")
Expand Down
11 changes: 6 additions & 5 deletions hey/openai.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import time
import keyring

from openai import OpenAI, OpenAIError
from rich.console import Console
Expand All @@ -8,6 +9,7 @@

from hey.configs import configs
from hey.consts import BASE_CONFIG
from hey.consts import HEY_TOKEN_KEY
from hey.exceptions import ConnectionIssue, TokenIsNotDefined


Expand All @@ -16,14 +18,13 @@ def __init__(self) -> None:
self.is_valid = False

def validate(self) -> str:
token = os.environ.get("HEY_TOKEN", None)
token = keyring.get_password("system",HEY_TOKEN_KEY)
if token:
self.is_valid = True
return token
else:
raise TokenIsNotDefined(
"make sure the `HEY_TOKEN` is defined in the .bashrc/.zshrc file."
)
raise TokenIsNotDefined(
"token is not defined, Use `hey auth` to set your token."
)


class ChatGPT:
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ dependencies = [
"typer >= 0.12.3",
"rich >= 13.7.1",
"openai >= 1.30.1",
"platformdirs >= 4.2.2"
"platformdirs >= 4.2.2",
"keyring >= 25.3.0"
]
readme = { file = "README.md", content-type = "text/markdown" }
license = { file = "LICENSE" }
Expand Down

0 comments on commit 9dec823

Please sign in to comment.