From 031ddc626b694d2f2f178ceec3d8595324d2e043 Mon Sep 17 00:00:00 2001 From: Matthew Chan Date: Wed, 18 Mar 2026 13:59:10 -0400 Subject: [PATCH] Add: Asking for email --- internal/cmd/subscription.go | 37 +++++++++++++++++++ internal/subscription/subscription.go | 51 +++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 internal/cmd/subscription.go create mode 100644 internal/subscription/subscription.go diff --git a/internal/cmd/subscription.go b/internal/cmd/subscription.go new file mode 100644 index 0000000..cee761b --- /dev/null +++ b/internal/cmd/subscription.go @@ -0,0 +1,37 @@ +package cmd + +import ( + "fmt" + "github.com/spf13/cobra" + "watcloud-cli/internal/subscription" +) + +var subscriptionCmd = &cobra.Command{ + Use: "subscription [job_id] [email]", + Short: "Get an email notification when a SLURM job finishes", + Long: "Subscribe to a specific SLURM job by its ID. When the job completes, you will receive an email notification.", + + Args: cobra.ExactArgs(2), + + Run: func(cmd *cobra.Command, args []string) { + jobID := args[0] + email := args[1] + + fmt.Printf("Attempting to subscribe %s to job %s...\n", email, jobID) + err := subscription.SubscribeToJobAPI(jobID, email) + + if err != nil { + fmt.Printf("Failed to subscribe to job: %v\n", err) + } else { + fmt.Printf("Success! %s You will be emailed when job %s completes \n", email, jobID) + } + }, +} + +func init() { + rootCmd.AddCommand(subscriptionCmd) +} + +// func subscribeToJobAPI(jobID string) error { +// return nil +// } diff --git a/internal/subscription/subscription.go b/internal/subscription/subscription.go new file mode 100644 index 0000000..f248e0a --- /dev/null +++ b/internal/subscription/subscription.go @@ -0,0 +1,51 @@ +package subscription + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + "time" +) + +type JobSubscriptionData struct { + JobID string `json:"job_id"` + Email string `json:"email"` +} + +func SubscribeToJobAPI(jobID string, email string) error { + + // Create data package + payload := JobSubscriptionData{ + JobID: jobID, + Email: email, + } + + jsonData, err := json.Marshal(payload) + if err != nil { + return fmt.Errorf("failed to format data: %v", err) + } + + apiURL := "http://localhost:8080/subscribe" + + req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData)) + if err != nil { + return fmt.Errorf("failed to create request: %v", err) + } + + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{Timeout: 10 * time.Second} + resp, err := client.Do(req) + if err != nil { + return fmt.Errorf("network error: %v", err) + } + + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("server rejected the request with status: %d", resp.StatusCode) + } + + return nil +} \ No newline at end of file