Skip to content

Commit

Permalink
add autotrigger command
Browse files Browse the repository at this point in the history
Signed-off-by: Seán C McCord <[email protected]>
  • Loading branch information
Ulexus committed Jun 14, 2022
1 parent de31791 commit d5002c4
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/autotrigger/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/autotrigger
3 changes: 3 additions & 0 deletions cmd/autotrigger/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM gcr.io/distroless/static
COPY autotrigger /go/bin/app
ENTRYPOINT ["/go/bin/app"]
106 changes: 106 additions & 0 deletions cmd/autotrigger/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"

"github.com/CyCoreSystems/audimance/agenda"
)

var cueName string
var interval time.Duration
var baseURL string

func init() {
flag.DurationVar(&interval, "r", 3*time.Minute, "repeat interval")
flag.StringVar(&cueName, "c", "", "name of cue to be triggered")
flag.StringVar(&baseURL, "u", "http://localhost:3000", "base URL")
}

func main() {
flag.Parse()

ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()

if cueName == "" {
log.Fatalln("cue name must be set")
}

cueID, err := findCue(cueName)
if err != nil {
log.Fatalln("failed to find cue: %s", err.Error())
}

if err := trigger(cueID); err != nil {
log.Printf("failed to trigger cue: %s", err.Error())
} else {
log.Printf("triggered cue %s (%s)", cueName, cueID)
}

for {
select {
case <-ctx.Done():
os.Exit(0)
case <-time.After(interval):
if err := trigger(cueID); err != nil {
log.Printf("failed to trigger cue: %s", err.Error())
} else {
log.Printf("triggered cue %s (%s)", cueName, cueID)
}
}
}
}

func findCue(cueName string) (string, error) {
resp, err := http.Get(fmt.Sprintf("%s/agenda.json", baseURL))
if err != nil {
return "", fmt.Errorf("failed to retrieve agenda: %w", err)
}

defer resp.Body.Close()

var a agenda.Agenda

if err := json.NewDecoder(resp.Body).Decode(&a); err != nil {
return "", fmt.Errorf("failed to decode agenda: %w", err)
}

for _, c := range a.Cues {
if c.Name == cueName {
return c.ID, nil
}

// Handle cases where the user passes the ID instaed of the name
if c.ID == cueName {
return c.ID, nil
}
}

return "", fmt.Errorf("cue %q not found in agenda", cueName)
}

func trigger(cueID string) error {
req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("%s/cues/%s", baseURL, cueID), nil)
if err != nil {
return fmt.Errorf("failed to construct PUT request: %w", err)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("non-200 OK response: %s", resp.Status)
}

return nil
}

0 comments on commit d5002c4

Please sign in to comment.