From 0d8ad64ce72c20233ad60a1fc68c3bfd6a1c7556 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 20 Aug 2020 00:52:54 +0200 Subject: [PATCH] Allow to use --test-notif without providers and DB connection (#157 #150) --- cmd/main.go | 4 ++-- docs/faq.md | 3 +-- go.sum | 2 -- internal/app/diun.go | 41 +++++++++++++++++++++------------------ internal/config/config.go | 18 ++++++++++++----- internal/db/client.go | 4 ++-- 6 files changed, 40 insertions(+), 32 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index bc6e954e..69d8d146 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -75,14 +75,14 @@ func main() { }() // Load configuration - cfg, err := config.Load(cli.Cfgfile) + cfg, err := config.Load(cli) if err != nil { log.Fatal().Err(err).Msg("Cannot load configuration") } log.Debug().Msg(cfg.String()) // Init - if diun, err = app.New(meta, cfg, location); err != nil { + if diun, err = app.New(meta, cli, cfg, location); err != nil { log.Fatal().Err(err).Msgf("Cannot initialize %s", meta.Name) } diff --git a/docs/faq.md b/docs/faq.md index 21699744..8c271118 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -11,8 +11,7 @@ $ diun --config ./diun.yml --test-notif Or within a container: ```shell -$ docker run --rm -it -v "$(pwd)/diun.yml:/diun.yml" \ - crazymax/diun:latest --config /diun.yml --test-notif +$ docker-compose exec diun --test-notif ``` ## field docker|swarm uses unsupported type: invalid diff --git a/go.sum b/go.sum index e6b367d0..3490ffce 100644 --- a/go.sum +++ b/go.sum @@ -65,8 +65,6 @@ github.com/containers/ocicrypt v1.0.2/go.mod h1:nsOhbP19flrX6rE7ieGFvBlr7modwmNj github.com/containers/storage v1.20.2 h1:tw/uKRPDnmVrluIzer3dawTFG/bTJLP8IEUyHFhltYk= github.com/containers/storage v1.20.2/go.mod h1:oOB9Ie8OVPojvoaKWEGSEtHbXUAs+tSyr7RO7ZGteMc= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= -github.com/crazy-max/gonfig v0.2.0 h1:xmhIS7rgd/JGAxGsVCp1dLVSEr15DOBQXvN5AvCkyhA= -github.com/crazy-max/gonfig v0.2.0/go.mod h1:UNwOsugQKi1lY7sXRAYJNP5rftyUC5LeKyaly9Efh+c= github.com/crazy-max/gonfig v0.3.0 h1:/HFdLQjXSNhImgeQgD2eXhc5svX4PhUkGSbl4fJRp4s= github.com/crazy-max/gonfig v0.3.0/go.mod h1:7vmzltkoa1RHpGB5fTom0ebnqelHdd7fzhtXTi8sVoQ= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/internal/app/diun.go b/internal/app/diun.go index 0ef05286..974eca10 100644 --- a/internal/app/diun.go +++ b/internal/app/diun.go @@ -35,28 +35,30 @@ type Diun struct { } // New creates new diun instance -func New(meta model.Meta, cfg *config.Config, location *time.Location) (*Diun, error) { - // DB client - dbcli, err := db.New(*cfg.Db) - if err != nil { - return nil, err - } +func New(meta model.Meta, cli model.Cli, cfg *config.Config, location *time.Location) (*Diun, error) { + var err error - // Notification client - notifcli, err := notif.New(cfg.Notif, meta) - if err != nil { - return nil, err - } - - return &Diun{ + diun := &Diun{ meta: meta, cfg: cfg, cron: cron.New(cron.WithLocation(location), cron.WithParser(cron.NewParser( cron.SecondOptional|cron.Minute|cron.Hour|cron.Dom|cron.Month|cron.Dow|cron.Descriptor), )), - db: dbcli, - notif: notifcli, - }, nil + } + + diun.notif, err = notif.New(cfg.Notif, meta) + if err != nil { + return nil, err + } + + if !cli.TestNotif { + diun.db, err = db.New(*cfg.Db) + if err != nil { + return nil, err + } + } + + return diun, nil } // Start starts diun @@ -147,8 +149,9 @@ func (di *Diun) Close() { func (di *Diun) TestNotif() { createdAt, _ := time.Parse("2006-01-02T15:04:05Z", "2020-03-26T12:23:56Z") image, _ := registry.ParseImage(registry.ParseImageOptions{ - Name: "crazymax/diun:latest", + Name: "diun/testnotif:latest", }) + image.HubLink = "" log.Info().Msg("Testing notification settings...") di.notif.Send(model.NotifEntry{ @@ -156,7 +159,7 @@ func (di *Diun) TestNotif() { Provider: "file", Image: image, Manifest: registry.Manifest{ - Name: "docker.io/crazymax/diun", + Name: "diun/testnotif", Tag: "latest", MIMEType: "application/vnd.docker.distribution.manifest.list.v2+json", Digest: "sha256:216e3ae7de4ca8b553eb11ef7abda00651e79e537e85c46108284e5e91673e01", @@ -172,7 +175,7 @@ func (di *Diun) TestNotif() { "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", + "org.label-schema.version": "x.x.x", }, Layers: []string{ "sha256:aad63a9339440e7c3e1fff2b988991b9bfb81280042fa7f39a5e327023056819", diff --git a/internal/config/config.go b/internal/config/config.go index 8b1ac32f..6bb08373 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -18,18 +18,18 @@ type Config struct { Watch *model.Watch `yaml:"watch,omitempty" json:"watch,omitempty"` Notif *model.Notif `yaml:"notif,omitempty" json:"notif,omitempty"` RegOpts model.RegOpts `yaml:"regopts,omitempty" json:"regopts,omitempty" validate:"unique=Name,dive"` - Providers *model.Providers `yaml:"providers,omitempty" json:"providers,omitempty" validate:"required"` + Providers *model.Providers `yaml:"providers,omitempty" json:"providers,omitempty"` } // Load returns Config struct -func Load(cfgfile string) (*Config, error) { +func Load(cli model.Cli) (*Config, error) { cfg := Config{ Db: (&model.Db{}).GetDefaults(), Watch: (&model.Watch{}).GetDefaults(), } fileLoader := gonfig.NewFileLoader(gonfig.FileLoaderConfig{ - Filename: cfgfile, + Filename: cli.Cfgfile, Finder: gonfig.Finder{ BasePaths: []string{"/etc/diun/diun", "$XDG_CONFIG_HOME/diun", "$HOME/.config/diun", "./diun"}, Extensions: []string{"yaml", "yml"}, @@ -54,20 +54,28 @@ func Load(cfgfile string) (*Config, error) { log.Info().Msgf("Configuration loaded from %d environment variable(s)", len(envLoader.GetVars())) } - if err := cfg.validate(); err != nil { + if err := cfg.validate(cli); err != nil { return nil, err } return &cfg, nil } -func (cfg *Config) validate() error { +func (cfg *Config) validate(cli model.Cli) error { if len(cfg.Db.Path) > 0 { if err := os.MkdirAll(path.Dir(cfg.Db.Path), os.ModePerm); err != nil { return errors.Wrap(err, "Cannot create database destination folder") } } + if cfg.Notif == nil && cli.TestNotif { + return errors.New("At least one notifier is required") + } + + if cfg.Providers == nil && !cli.TestNotif { + return errors.New("At least one provider is required") + } + return validator.New().Struct(cfg) } diff --git a/internal/db/client.go b/internal/db/client.go index 5d5ea5b9..070c6edf 100644 --- a/internal/db/client.go +++ b/internal/db/client.go @@ -1,11 +1,11 @@ package db import ( - "fmt" "reflect" "time" "github.com/crazy-max/diun/v4/internal/model" + "github.com/pkg/errors" "github.com/rs/zerolog/log" bolt "go.etcd.io/bbolt" ) @@ -52,7 +52,7 @@ func New(cfg model.Db) (*Client, error) { log.Debug().Msgf("%d entries found in manifest bucket", stats.KeyN) return nil }); err != nil { - return nil, fmt.Errorf("cannot count entries in manifest bucket, %v", err) + return nil, errors.Wrap(err, "Cannot count entries in manifest bucket") } c := &Client{