Skip to content

Commit

Permalink
feat(cli): add waiting when test connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
wenfengwang committed Apr 25, 2024
1 parent 755c91b commit 3eeca15
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,44 @@ Verify the installation by running `npi version`. If you see the output similar
"BuildDate": "2024-04-23_02:23:50-0700",
"GitCommit": "934739f",
"Platform": "darwin/arm64",
"Version": "v0.0.0"
"Version": "v0.0.1"
}
```

#### Setting Up NPi Server with Docker

Replace `{YOUR_OAI_KEY}` with your actual OpenAI API Key, then execute:
Replace `YOUR_OAI_KEY` with your actual OpenAI API Key, then execute:

```sh
docker run -d --name npi \
-p 9140:9140 \
-p 9141:9141 \
-e OPENAI_API_KEY={YOUR_OAI_KEY} \
npiai/npi
-e OPENAI_API_KEY=YOUR_OAI_KEY \
npiai/npi:v0.0.1
```

Confirm server connectivity by running `npi connect test`. If you receive a `server connected` message, the setup is
if you run on macOS, please use:

```sh
docker run -d --name npi \
-p 9140:9140 \
-p 9141:9141 \
-e OPENAI_API_KEY=YOUR_OAI_KEY \
npiai/npi-mac:v0.0.1
```

Confirm server connectivity by running `npi connect test`. If you receive a `NPi Server is operational!` message, the setup is
successful. Otherwise, consult the logs with `docker logs npi` and report issues
to [NPi GitHub Repository](https://github.com/npi-ai/npi/issues/new).

### Try the GitHub App

#### Authorize NPi to access your GitHub account

Generate a new token via [GitHub Tokens Page](https://github.com/settings/tokens) for NPi, and authorize NPi's access to
your GitHub account with:
Generate a new token via [GitHub Tokens Page](https://github.com/settings/tokens) for NPi, you at least need to gather the `repo` scope:
![img.png](assets/github-token-grant-repo.png)

and authorize NPi's access to your GitHub account with:

```sh
npi auth github --access-token YOUR_GITHUB_ACCESS_TOKEN
Expand Down
Binary file added assets/github-token-grant-repo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 20 additions & 7 deletions cli/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/fatih/color"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -48,16 +49,28 @@ func connectTestCommand() *cobra.Command {
Use: "test",
Short: "Testing if NPi Server is connected",
Run: func(cmd *cobra.Command, args []string) {
resp, err := httpClient.R().Get("/ping")
if err != nil {
color.Red("failed to connect NPi Server: %v", err)
os.Exit(-1)
}
if resp.StatusCode() != 200 {
start := time.Now()
color.White("Connecting to NPi Server...")
color.New()
for {
resp, err := httpClient.R().Get("/ping")
if err != nil {
if time.Since(start) < 30*time.Second {
time.Sleep(1 * time.Second)
continue
}
color.Red("failed to connect NPi Server: %v", err)
os.Exit(-1)
}
if resp.StatusCode() == 200 {
break
}

color.Red("Connected to NPi Server, but server returned: %d", resp.StatusCode())
os.Exit(-1)
}
color.Green("NPi Server is operational")

_, _ = color.New(color.FgGreen, color.Bold).Println("NPi Server is operational!")
},
}
return cmd
Expand Down

0 comments on commit 3eeca15

Please sign in to comment.