Skip to content

Commit

Permalink
Allows for some khedra commands when it's already running (help, vers…
Browse files Browse the repository at this point in the history
…ion, etc.)
  • Loading branch information
tjayrush committed Feb 1, 2025
1 parent da355ba commit da89081
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package app

import (
"fmt"
"log"
"log/slog"
"os"

Expand All @@ -19,19 +19,9 @@ type KhedraApp struct {

func NewKhedraApp() *KhedraApp {
k := KhedraApp{}

// If khedra is already running, one of these ports is serving the
// control API. We need to make sure it's not running and fail if
// it is.
cntlSvcPorts := []string{"8338", "8337", "8336", "8335"}
for _, port := range cntlSvcPorts {
if utils.PingServer("http://localhost:" + port) {
msg := fmt.Sprintf("Error: Khedra is already running (control service port :%s is in use). Quitting...", port)
fmt.Println(colors.Red+msg, colors.Off)
os.Exit(1)
}
if k.isRunning() {
log.Fatal(colors.BrightBlue + "khedra is already running - cannot run..." + colors.Off)
}

k.cli = initCli(&k)
return &k
}
Expand All @@ -40,6 +30,38 @@ func (k *KhedraApp) Run() {
_ = k.cli.Run(os.Args)
}

func (k *KhedraApp) isRunning() bool {
okArgs := map[string]bool{
"help": true,
"-h": true,
"--help": true,
"version": true,
"-v": true,
"--version": true,
}

if len(os.Args) < 2 || len(os.Args) == 2 && os.Args[1] == "config" {
return false
}

for i, arg := range os.Args {
if okArgs[arg] {
return false
} else if arg == "config" && i < len(os.Args)-1 && os.Args[i+1] == "show" {
return false
}
}

ports := []string{"8338", "8337", "8336", "8335"}
for _, port := range ports {
if utils.PingServer("http://localhost:" + port) {
return true
}
}

return false
}

func (k *KhedraApp) ConfigMaker() (types.Config, error) {
cfg, err := LoadConfig()
if err != nil {
Expand Down

0 comments on commit da89081

Please sign in to comment.