Skip to content

Commit

Permalink
fix(cli): Fix regression with me breaking the API breaking the --list…
Browse files Browse the repository at this point in the history
… flag
  • Loading branch information
etu committed Jun 10, 2024
1 parent ba8b3cb commit aaf8695
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
6 changes: 6 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Content-Type: application/json
DELETE /api/config/server/:name
```

## Get all servers configuration

```http
GET /api/config/server
```

## Start a server

```http
Expand Down
43 changes: 34 additions & 9 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ type Cli struct {
}

func (cli *Cli) List(format string) {
var state ServeFullState
var state map[string]ServerConfig
var runningState ServerItemList

// Build URL based on config
requestUrl := fmt.Sprintf("http://%s:%d/api/state", cli.config.Settings.ListenAddress, cli.config.Settings.ListenPort)
requestUrl := fmt.Sprintf("http://%s:%d/api/config/server", cli.config.Settings.ListenAddress, cli.config.Settings.ListenPort)

// Do request to running instance of program
res, err := http.Get(requestUrl)
defer res.Body.Close()

if err != nil {
log.Printf("Failed to connect to running instance of program: %s\n", err)
Expand All @@ -40,23 +42,46 @@ func (cli *Cli) List(format string) {
}

// Read the body content
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)

// Parse the json
json.Unmarshal(body, &state)

// Build URL based on config
requestUrl = fmt.Sprintf("http://%s:%d/api/state", cli.config.Settings.ListenAddress, cli.config.Settings.ListenPort)

// Get the state
res, err = http.Get(requestUrl)
defer res.Body.Close()

if err != nil {
log.Printf("Failed to connect to running instance of program: %s\n", err)
os.Exit(1)
}

// Validate status code
if res.StatusCode != http.StatusOK {
log.Printf("Unexpected status code when fetching active config: %d\n", res.StatusCode)
os.Exit(2)
}

// Read the body content
body, _ = io.ReadAll(res.Body)

// Parse the json
json.Unmarshal(body, &runningState)

switch format {
case "table":
output := table.NewWriter()
output.SetOutputMirror(os.Stdout)
output.AppendHeader(table.Row{"Name", "Running", "Directory", "Command"})

for _, val := range state.Config.Servers {
for _, val := range state {
isRunning := false

if _, ok := state.RunnerState[val.Name]; ok {
isRunning = true
if _, ok := runningState.Servers[val.Name]; ok {
isRunning = runningState.Servers[val.Name].IsRunning
}

output.AppendRow([]interface{}{val.Name, isRunning, val.Directory, val.Command})
Expand All @@ -70,11 +95,11 @@ func (cli *Cli) List(format string) {

output.Write([]string{"Name", "Running", "Directory", "Command"})

for _, val := range state.Config.Servers {
for _, val := range state {
isRunning := false

if _, ok := state.RunnerState[val.Name]; ok {
isRunning = true
if _, ok := runningState.Servers[val.Name]; ok {
isRunning = runningState.Servers[val.Name].IsRunning
}

output.Write([]string{val.Name, fmt.Sprintf("%t", isRunning), val.Directory, val.Command})
Expand Down
6 changes: 6 additions & 0 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ func (serve *Serve) newRouter() *mux.Router {
json.NewEncoder(w).Encode(resp)
}).Methods(http.MethodDelete)

// Method to fetch all servers configurations
router.HandleFunc("/api/config/server", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(serve.config.Servers)
}).Methods(http.MethodGet)

//
// Endpoints to manage running state of servers
//
Expand Down

0 comments on commit aaf8695

Please sign in to comment.