Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/cli/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ func SendRequestAndReadResponse(url *url.URL, enableAuth bool, method string, bo
}
return data, nil
}

func CheckConnection(url string) error {
_, err := http.Get(url)
if err != nil {
// fang/lipgloss lowk nukes this custom formatting
return fmt.Errorf("\x1b[1;37;41mUNABLE TO CONNECT\x1b[0m | %s\n\t↳ %v",
"Did you forget to start the server?",
err)
}
return nil
}
77 changes: 11 additions & 66 deletions internal/cli/events/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ package events

import (
"fmt"
"io"
"net/http"
"net/url"
"os"

"github.com/charmbracelet/huh"
"github.com/spf13/cobra"

"github.com/acmcsufoss/api.acmcsuf.com/internal/cli/client"
"github.com/acmcsufoss/api.acmcsuf.com/internal/cli/config"
"github.com/acmcsufoss/api.acmcsuf.com/internal/cli/oauth"
"github.com/acmcsufoss/api.acmcsuf.com/utils"
)

Expand All @@ -20,32 +17,8 @@ var DeleteEvent = &cobra.Command{
Short: "Delete an event with its id",

Run: func(cmd *cobra.Command, args []string) {
var uuidVal string
cmd.Flags().Set("id", uuidVal)
err := huh.NewForm().Run()
if err != nil {
if err == huh.ErrUserAborted {
fmt.Println("User canceled the form — exiting.")
}
fmt.Println("Uh oh:", err)
os.Exit(1)
}
err = huh.NewInput().
Title("ACMCSUF-CLI Event Delete:").
Description("Please enter the event's uuid:").
Prompt("> ").
Value(&uuidVal).
Run()
if err != nil {
if err == huh.ErrUserAborted {
fmt.Println("User canceled the form — exiting.")
}
fmt.Println("Uh oh:", err)
os.Exit(1)
}
cmd.Flags().Set("id", uuidVal)
id, _ := cmd.Flags().GetString("id")
deleteEvent(id, config.Cfg)
uuid, _ := cmd.Flags().GetString("id")
deleteEvent(uuid, config.Cfg)
},
}

Expand All @@ -55,42 +28,14 @@ func init() {
}

func deleteEvent(id string, cfg *config.Config) {
baseURL := &url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%s", cfg.Host, cfg.Port),
}
if err := utils.CheckConnection(baseURL.JoinPath("/health").String()); err != nil {
fmt.Println(err)
return
}

deleteURL := baseURL.JoinPath(fmt.Sprint("/v1/events/", id))

// ----- Delete Request -----
request, err := oauth.NewRequestWithAuth(http.MethodDelete, deleteURL.String(), nil)
if err != nil {
fmt.Println("Error making delete request:", err)
return
}

client := &http.Client{}
response, err := client.Do(request)
if err != nil {
fmt.Println("Error with delete response:", err)
return
}
defer response.Body.Close()
deleteURL := config.GetBaseURL(cfg).JoinPath("v1", "events", id)

// ----- Read Response Info -----
if response.StatusCode != http.StatusOK {
fmt.Println("Response status:", response.Status)
return
}

body, err := io.ReadAll(response.Body)
if err != nil {
fmt.Println("Error reading delete response body:", err)
return
if body, err := client.SendRequestAndReadResponse(deleteURL, true, http.MethodDelete, nil); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
if body != nil {
utils.PrettyPrintJSONErr(body)
}
} else {
utils.PrettyPrintJSON(body)
}
utils.PrettyPrintJSON(body)
}
70 changes: 0 additions & 70 deletions internal/cli/events/events.go

This file was deleted.

104 changes: 8 additions & 96 deletions internal/cli/events/get.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,21 @@
package events

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"

"github.com/acmcsufoss/api.acmcsuf.com/internal/api/dbmodels"
"github.com/acmcsufoss/api.acmcsuf.com/internal/cli/client"
"github.com/acmcsufoss/api.acmcsuf.com/internal/cli/config"
"github.com/acmcsufoss/api.acmcsuf.com/utils"
"github.com/charmbracelet/huh"
"github.com/spf13/cobra"
)

var GetEvent = &cobra.Command{
Use: "get",
Short: "Get events",
Short: "Get one or all events",

Run: func(cmd *cobra.Command, args []string) {
blankUUID := ""
cmd.Flags().Set("id", blankUUID)
var flagsChosen []string
err := huh.NewForm(
huh.NewGroup(
huh.NewMultiSelect[string]().
//Ask the user what commands they want to use.
Title("ACMCSUF-CLI Event Get").
Description("Choose a command(s). Note: Use spacebar to select and if done click enter.\nTo get all events, simply click enter.").
Options(
huh.NewOption("Get Specific ID", "id"),
).
Value(&flagsChosen),
),
).Run()
if err != nil {
if err == huh.ErrUserAborted {
fmt.Println("User canceled the form — exiting.")
}
fmt.Println("Uh oh:", err)
os.Exit(1)
}
for _, flag := range flagsChosen {
var uuidVal string
switch flag {
case "id":
err = huh.NewInput().
Title("ACMCSUF-CLI Event Get:").
Description("Please enter the event's ID:").
Prompt("> ").
Value(&uuidVal).
Run()
cmd.Flags().Set("id", uuidVal)
}
if err != nil {
if err == huh.ErrUserAborted {
fmt.Println("User canceled the form — exiting.")
}
fmt.Println("Uh oh:", err)
os.Exit(1)
}
}
// If these where global, unexpected behavior would be expected :(
id, _ := cmd.Flags().GetString("id")
getEvents(id, config.Cfg)
},
Expand All @@ -72,56 +26,14 @@ func init() {
}

func getEvents(id string, cfg *config.Config) {
baseURL := &url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%s", cfg.Host, cfg.Port),
}
if err := utils.CheckConnection(baseURL.JoinPath("/health").String()); err != nil {
fmt.Println(err)
return
}
getUrl := config.GetBaseURL(cfg).JoinPath("v1", "events", id)

getURL := baseURL.JoinPath(fmt.Sprint("v1/events/", id))

// ----- Get -----
req, err := http.NewRequest(http.MethodGet, getURL.String(), nil)
if err != nil {
fmt.Println("Error getting the request:", err)
return
}

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: couldn't make GET request: %v", err)
}
defer resp.Body.Close()

// ----- Read Response Information -----
if resp.StatusCode != http.StatusOK {
fmt.Println("Response status:", resp.Status)
return
}

if id == "" {
var getPayload []dbmodels.CreateEventParams
err = json.NewDecoder(resp.Body).Decode(&getPayload)
if err != nil {
fmt.Println("Failed to read response body without id:", err)
return
}

for i := range getPayload {
fmt.Println(utils.PrintStruct(getPayload[i]))
if body, err := client.SendRequestAndReadResponse(getUrl, false, http.MethodGet, nil); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
if body != nil {
utils.PrettyPrintJSONErr(body)
}
} else {
var getPayload dbmodels.CreateEventParams
err = json.NewDecoder(resp.Body).Decode(&getPayload)
if err != nil {
fmt.Println("Failed to read response body with id:", err)
return
}

fmt.Println(utils.PrintStruct(getPayload))
utils.PrettyPrintJSON(body)
}
}
Loading
Loading