accept $RECURSIVE_MISSING_WEBHOOK $RECURSIVE_MISSING_WEBHOOK_CACHE_D
This commit is contained in:
45
main.go
45
main.go
@@ -6,9 +6,12 @@ import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
@@ -21,13 +24,15 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
Debug = os.Getenv("DEBUG") == "true"
|
||||
ConstTitle = os.Getenv("YAML_C_TITLE")
|
||||
ConstSeason = os.Getenv("YAML_C_SEASON")
|
||||
ConstEpisode = os.Getenv("YAML_C_EPISODE")
|
||||
ConstOutd = os.Getenv("YAML_O")
|
||||
Dry = os.Getenv("YAML_D") == "true" || os.Getenv("DRY") == "true"
|
||||
ConstPatterns = os.Getenv("YAML_P")
|
||||
Debug = os.Getenv("DEBUG") == "true"
|
||||
ConstTitle = os.Getenv("YAML_C_TITLE")
|
||||
ConstSeason = os.Getenv("YAML_C_SEASON")
|
||||
ConstEpisode = os.Getenv("YAML_C_EPISODE")
|
||||
ConstOutd = os.Getenv("YAML_O")
|
||||
Dry = os.Getenv("YAML_D") == "true" || os.Getenv("DRY") == "true"
|
||||
ConstPatterns = os.Getenv("YAML_P")
|
||||
WebhookOnRecursiveMiss = os.Getenv("RECURSIVE_MISSING_WEBHOOK")
|
||||
WebhookOnRecursiveMissCacheD = os.Getenv("RECURSIVE_MISSING_WEBHOOK_CACHE_D")
|
||||
)
|
||||
|
||||
type Yaml struct {
|
||||
@@ -81,6 +86,32 @@ func Recursive(ctx context.Context) error {
|
||||
|
||||
p := path.Join(d, YamlFile)
|
||||
if _, err := os.Stat(p); err != nil {
|
||||
log.Printf("%s has no %s", d, YamlFile)
|
||||
if WebhookOnRecursiveMiss != "" && WebhookOnRecursiveMissCacheD != "" {
|
||||
cacheP := regexp.MustCompile(`[^a-zA-Z0-9]`).ReplaceAllString(p, `_`)
|
||||
cacheP = path.Join(WebhookOnRecursiveMissCacheD, cacheP)
|
||||
if _, err := os.Stat(cacheP); err != nil {
|
||||
u, err := url.Parse(WebhookOnRecursiveMiss)
|
||||
if err != nil {
|
||||
return fmt.Errorf("WebhookOnRecursiveMiss (%s) invalid: %w", WebhookOnRecursiveMiss, err)
|
||||
}
|
||||
q := u.Query()
|
||||
q.Set("p", p)
|
||||
u.RawQuery = q.Encode()
|
||||
resp, err := http.Post(u.String(), "text/plain", strings.NewReader(p))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to call %s for missing %s: %w", WebhookOnRecursiveMiss, p, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer io.Copy(io.Discard, resp.Body)
|
||||
if resp.StatusCode > 250 {
|
||||
return fmt.Errorf("unexpected status code from %s for %s: %w", WebhookOnRecursiveMiss, p, err)
|
||||
}
|
||||
|
||||
os.MkdirAll(path.Dir(cacheP), os.ModePerm)
|
||||
ioutil.WriteFile(cacheP, []byte{}, os.ModePerm)
|
||||
}
|
||||
}
|
||||
} else if err := func() error {
|
||||
y, err := NewYaml(path.Join(d, YamlFile))
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user