Skip to content

Commit

Permalink
Added docs and better error propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
pixxon committed Feb 6, 2024
1 parent 701d2bb commit 950474e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 58 deletions.
8 changes: 4 additions & 4 deletions cmd/backup/config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func loadConfig(lookup envProxy) (*Config, error) {

var c = &Config{}
if err := envconfig.Process("", c); err != nil {
return nil, fmt.Errorf("failed to process configuration values, error: %v", err)
return nil, fmt.Errorf("failed to process configuration values, error: %w", err)
}

return c, nil
Expand All @@ -54,7 +54,7 @@ func loadEnvFiles(directory string) ([]*Config, error) {
if os.IsNotExist(err) {
return nil, err
}
return nil, fmt.Errorf("failed to read files from env directory, error: %v", err)
return nil, fmt.Errorf("failed to read files from env directory, error: %w", err)
}

var cs = make([]*Config, 0)
Expand All @@ -63,15 +63,15 @@ func loadEnvFiles(directory string) ([]*Config, error) {
p := filepath.Join(directory, item.Name())
envFile, err := godotenv.Read(p)
if err != nil {
return nil, fmt.Errorf("error reading config file %s, error: %v", p, err)
return nil, fmt.Errorf("error reading config file %s, error: %w", p, err)
}
lookup := func(key string) (string, bool) {
val, ok := envFile[key]
return val, ok
}
c, err := loadConfig(lookup)
if err != nil {
return nil, fmt.Errorf("error loading config from file %s, error: %v", p, err)
return nil, fmt.Errorf("error loading config from file %s, error: %w", p, err)
}
cs = append(cs, c)
}
Expand Down
110 changes: 59 additions & 51 deletions cmd/backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/robfig/cron/v3"
)

func runBackup(c *Config) (ret error) {
func runScript(c *Config) (ret error) {
s, err := newScript(c)
if err != nil {
return err
Expand Down Expand Up @@ -86,66 +86,68 @@ func runBackup(c *Config) (ret error) {
return nil
}

func app(serve bool) error {
if serve {
cr := cron.New(
cron.WithParser(
cron.NewParser(
cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor,
),
func runInForeground() error {
cr := cron.New(
cron.WithParser(
cron.NewParser(
cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor,
),
)
),
)

addJob := func(c *Config) error {
_, err := cr.AddFunc(c.BackupCronExpression, func() {
err := runBackup(c)
if err != nil {
slog.Error("unexpected error during backup", "error", err)
}
})
return err
addJob := func(c *Config) error {
_, err := cr.AddFunc(c.BackupCronExpression, func() {
err := runScript(c)
if err != nil {
slog.Error("unexpected error during backup", "error", err)
}
})
return err
}

cs, err := loadEnvFiles("/etc/dockervolumebackup/conf.d")
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("could not load config from environment files, error: %w", err)
}

cs, err := loadEnvFiles("/etc/dockervolumebackup/conf.d")
c, err := loadEnvVars()
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("could not load config from environment files, error: %v", err)
}

c, err := loadEnvVars()
if err != nil {
return fmt.Errorf("could not load config from environment variables")
} else {
err = addJob(c)
if err != nil {
return fmt.Errorf("could not add cron job, error: %v", err)
}
}
return fmt.Errorf("could not load config from environment variables")
} else {
for _, c := range cs {
err = addJob(c)
if err != nil {
return fmt.Errorf("could not add cron job, error: %v", err)
}
err = addJob(c)
if err != nil {
return fmt.Errorf("could not add cron job, error: %w", err)
}
}

var quit = make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
cr.Start()
<-quit
ctx := cr.Stop()
<-ctx.Done()
} else {
c, err := loadEnvVars()
if err != nil {
return fmt.Errorf("could not load config from environment variables, error: %v", err)
for _, c := range cs {
err = addJob(c)
if err != nil {
return fmt.Errorf("could not add cron job, error: %w", err)
}
}
}

err = runBackup(c)
if err != nil {
return fmt.Errorf("unexpected error during backup, error: %v", err)
}
var quit = make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
cr.Start()
<-quit
ctx := cr.Stop()
<-ctx.Done()

return nil
}

func runAsCommand() error {
c, err := loadEnvVars()
if err != nil {
return fmt.Errorf("could not load config from environment variables, error: %w", err)
}

err = runScript(c)
if err != nil {
return fmt.Errorf("unexpected error during backup, error: %w", err)
}

return nil
Expand All @@ -155,7 +157,13 @@ func main() {
serve := flag.Bool("foreground", false, "run the tool in the foreground")
flag.Parse()

err := app(*serve)
var err error
if *serve {
err = runInForeground()
} else {
err = runAsCommand()
}

if err != nil {
slog.Error("ran into an issue during execution", "error", err)
os.Exit(1)
Expand Down
19 changes: 16 additions & 3 deletions docs/reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@ You can populate below template according to your requirements and use it as you
```
########### BACKUP SCHEDULE
# Backups run on the given cron schedule in `busybox` flavor. If no
# value is set, `@daily` will be used. If you do not want the cron
# to ever run, use `0 0 5 31 2 ?`.
# A cron expression represents a set of times, using 5 or 6 space-separated fields.
#
# Field name | Mandatory? | Allowed values | Allowed special characters
# ---------- | ---------- | -------------- | --------------------------
# Seconds | No | 0-59 | * / , -
# Minutes | Yes | 0-59 | * / , -
# Hours | Yes | 0-23 | * / , -
# Day of month | Yes | 1-31 | * / , - ?
# Month | Yes | 1-12 or JAN-DEC | * / , -
# Day of week | Yes | 0-6 or SUN-SAT | * / , - ?
#
# Month and Day-of-week field values are case insensitive.
# "SUN", "Sun", and "sun" are equally accepted.
# If no value is set, `@daily` will be used.
# If you do not want the cron to ever run, use `0 0 5 31 2 ?`.
# BACKUP_CRON_EXPRESSION="0 2 * * *"
Expand Down

0 comments on commit 950474e

Please sign in to comment.