-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Seán C McCord <[email protected]>
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/autotrigger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |