Add test notification flag (#23) (#73)

Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2020-05-26 04:55:23 +02:00
committed by GitHub
parent 3577624682
commit 42784e6ffe
5 changed files with 57 additions and 7 deletions

View File

@@ -70,6 +70,12 @@ func main() {
log.Fatal().Err(err).Msg("Cannot initialize Diun")
}
// Test notif
if cli.TestNotif {
diun.TestNotif()
return
}
// Start
if err = diun.Start(); err != nil {
log.Fatal().Err(err).Msg("Cannot start Diun")

View File

@@ -21,14 +21,14 @@ Usage: diun --config=STRING
Docker image update notifier. More info: https://github.com/crazy-max/diun
Flags:
--help Show context-sensitive help.
--help Show context-sensitive help.
--version
--config=STRING Diun configuration file ($CONFIG).
--timezone="UTC" Timezone assigned to Diun ($TZ).
--log-level="debug" Set log level ($LOG_LEVEL).
--log-json Enable JSON logging output ($LOG_JSON).
--log-caller Add file:line of the caller to log output
($LOG_CALLER).
--config=STRING Diun configuration file ($CONFIG).
--timezone="UTC" Timezone assigned to Diun ($TZ).
--log-level="info" Set log level ($LOG_LEVEL).
--log-json Enable JSON logging output ($LOG_JSON).
--log-caller Add file:line of the caller to log output ($LOG_CALLER).
--test-notif Test notification settings.
```
## Server configuration

View File

@@ -11,3 +11,4 @@
* `--log-level <level>`: Log level output. (default `info`).
* `--log-json`: Enable JSON logging output. (default `false`).
* `--log-caller`: Add file:line of the caller to log output. (default `false`).
* `--test-notif`: Enable to test notification settings.

View File

@@ -15,6 +15,7 @@ import (
dockerPrd "github.com/crazy-max/diun/internal/provider/docker"
filePrd "github.com/crazy-max/diun/internal/provider/file"
swarmPrd "github.com/crazy-max/diun/internal/provider/swarm"
"github.com/crazy-max/diun/pkg/registry"
"github.com/hako/durafmt"
"github.com/panjf2000/ants/v2"
"github.com/robfig/cron/v3"
@@ -144,3 +145,44 @@ func (di *Diun) Close() {
log.Warn().Err(err).Msg("Cannot close database")
}
}
// TestNotif test the notification settings
func (di *Diun) TestNotif() {
createdAt, _ := time.Parse("2006-01-02T15:04:05Z", "2020-03-26T12:23:56Z")
image, _ := registry.ParseImage("crazymax/diun:latest")
log.Info().Msg("Testing notification settings...")
di.notif.Send(model.NotifEntry{
Status: "new",
Provider: "file",
Image: image,
Manifest: registry.Manifest{
Name: "docker.io/crazymax/diun",
Tag: "latest",
MIMEType: "application/vnd.docker.distribution.manifest.list.v2+json",
Digest: "sha256:216e3ae7de4ca8b553eb11ef7abda00651e79e537e85c46108284e5e91673e01",
Created: &createdAt,
DockerVersion: "",
Labels: map[string]string{
"maintainer": "CrazyMax",
"org.label-schema.build-date": "2020-03-26T12:23:56Z",
"org.label-schema.description": "Docker image update notifier",
"org.label-schema.name": "Diun",
"org.label-schema.schema-version": "1.0",
"org.label-schema.url": "https://github.com/crazy-max/diun",
"org.label-schema.vcs-ref": "e13f097c",
"org.label-schema.vcs-url": "https://github.com/crazy-max/diun",
"org.label-schema.vendor": "CrazyMax",
"org.label-schema.version": "2.6.1",
},
Layers: []string{
"sha256:aad63a9339440e7c3e1fff2b988991b9bfb81280042fa7f39a5e327023056819",
"sha256:166c6f165b73185ede72415d780538a55c0c8e854bd177925bc007193e5b0d1b",
"sha256:e05682efa9cc9d6239b2b9252fe0dc1e58d6e1585679733bb94a6549d49e9b10",
"sha256:c6a5bfed445b3ed7e85523cd73c6532ac9f9b72bb588ca728fd5b33987ca6538",
"sha256:df2140efb8abeb727ef0b27ff158b7010a7941eb1cfdade505f510a6e1eaf016",
},
Platform: "linux/adm64",
},
})
}

View File

@@ -10,4 +10,5 @@ type Cli struct {
LogLevel string `kong:"name='log-level',env='LOG_LEVEL',default='info',help='Set log level.'"`
LogJSON bool `kong:"name='log-json',env='LOG_JSON',default='false',help='Enable JSON logging output.'"`
LogCaller bool `kong:"name='log-caller',env='LOG_CALLER',default='false',help='Add file:line of the caller to log output.'"`
TestNotif bool `kong:"name='test-notif',default='false',help='Test notification settings.'"`
}