Skip to content

Commit f7566ac

Browse files
Add missing run-* and send-* to all command sections (#504)
* move send monitoring sections * add unit tests to verify commands and monitoring sections * fix missing run-command and send monitoring * add missing setRootPath method
1 parent 8fd3ddb commit f7566ac

11 files changed

Lines changed: 197 additions & 138 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ $(GOBIN)/golangci-lint: verify $(GOBIN)/eget
9393

9494
$(GOBIN)/hugo: $(GOBIN)/eget
9595
@echo "[*] $@"
96-
"$(GOBIN)/eget" gohugoio/hugo --tag 0.145.0 --upgrade-only --asset=extended_0 --to '$(GOBIN)'
96+
"$(GOBIN)/eget" gohugoio/hugo --tag v0.145.0 --upgrade-only --asset=extended_0 --to '$(GOBIN)'
9797

9898
prepare_build: verify download
9999
@echo "[*] $@"

config/profile.go

Lines changed: 77 additions & 117 deletions
Large diffs are not rendered by default.

config/profile_test.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,8 @@ profile:
13931393
{"copy", true},
13941394
{"prune", true},
13951395
{"forget", true},
1396-
{"init", false},
1396+
{"init", true},
1397+
{"other", false},
13971398
}
13981399

13991400
for _, config := range configs {
@@ -1604,3 +1605,51 @@ func TestGetCopyStructFields(t *testing.T) {
16041605
assert.Equal(t, copySection.getInitFlags(profile).GetAll(), profile.GetCopyInitializeFlags().GetAll())
16051606
})
16061607
}
1608+
1609+
// make sure all commands are supporting run-before
1610+
func TestRunBeforeOnAllCommands(t *testing.T) {
1611+
commands := restic.CommandNames()
1612+
assert.NotEmpty(t, commands)
1613+
1614+
for _, command := range commands {
1615+
t.Run(command, func(t *testing.T) {
1616+
testConfig := `
1617+
[profile]
1618+
[profile.%[1]s]
1619+
run-before = ["echo hello"]
1620+
`
1621+
profile, err := getProfile("toml", fmt.Sprintf(testConfig, command), "profile", "")
1622+
require.NoError(t, err)
1623+
assert.NotNil(t, profile)
1624+
profileCommands, sectionCommands := profile.GetRunShellCommandsSections(command)
1625+
assert.Empty(t, profileCommands)
1626+
assert.NotEmpty(t, sectionCommands)
1627+
assert.Equal(t, []string{"echo hello"}, sectionCommands.RunBefore)
1628+
assert.Empty(t, sectionCommands.RunAfter)
1629+
})
1630+
}
1631+
}
1632+
1633+
// make sure all commands are supporting send-before
1634+
func TestSendBeforeOnAllCommands(t *testing.T) {
1635+
commands := restic.CommandNames()
1636+
assert.NotEmpty(t, commands)
1637+
1638+
for _, command := range commands {
1639+
t.Run(command, func(t *testing.T) {
1640+
testConfig := `
1641+
[profile]
1642+
[profile.%[1]s]
1643+
[[profile.%[1]s.send-before]]
1644+
url = "http://example.com"
1645+
`
1646+
profile, err := getProfile("toml", fmt.Sprintf(testConfig, command), "profile", "")
1647+
require.NoError(t, err)
1648+
assert.NotNil(t, profile)
1649+
monitoring := profile.GetMonitoringSections(command)
1650+
assert.NotEmpty(t, monitoring)
1651+
assert.Equal(t, 1, len(monitoring.SendBefore))
1652+
assert.Empty(t, monitoring.SendAfter)
1653+
})
1654+
}
1655+
}

config/send.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package config
2+
3+
// SendMonitoringSections is a group of target to send monitoring information
4+
type SendMonitoringSections struct {
5+
SendBefore []SendMonitoringSection `mapstructure:"send-before" description:"Send HTTP request(s) before a restic command"`
6+
SendAfter []SendMonitoringSection `mapstructure:"send-after" description:"Send HTTP request(s) after a successful restic command"`
7+
SendAfterFail []SendMonitoringSection `mapstructure:"send-after-fail" description:"Send HTTP request(s) after failed restic or shell commands"`
8+
SendFinally []SendMonitoringSection `mapstructure:"send-finally" description:"Send HTTP request(s) always, after all other commands"`
9+
}
10+
11+
func (s *SendMonitoringSections) setRootPath(_ *Profile, rootPath string) {
12+
for _, sections := range s.getAllSendMonitoringSections() {
13+
for index := range sections {
14+
sections[index].BodyTemplate = fixPath(sections[index].BodyTemplate, expandEnv, expandUserHome, absolutePrefix(rootPath))
15+
}
16+
}
17+
}
18+
19+
func (s *SendMonitoringSections) GetSendMonitoring() *SendMonitoringSections { return s }
20+
21+
func (s *SendMonitoringSections) getAllSendMonitoringSections() [][]SendMonitoringSection {
22+
return [][]SendMonitoringSection{
23+
s.SendBefore,
24+
s.SendAfter,
25+
s.SendAfterFail,
26+
s.SendFinally,
27+
}
28+
}
29+
30+
// SendMonitoringSection is used to send monitoring information to third party software
31+
type SendMonitoringSection struct {
32+
Method string `mapstructure:"method" enum:"GET;DELETE;HEAD;OPTIONS;PATCH;POST;PUT;TRACE" default:"GET" description:"HTTP method of the request"`
33+
URL ConfidentialValue `mapstructure:"url" format:"uri" description:"URL of the target to send to"`
34+
Headers []SendMonitoringHeader `mapstructure:"headers" description:"Additional HTTP headers to send with the request"`
35+
Body string `mapstructure:"body" description:"Request body, overrides \"body-template\""`
36+
BodyTemplate string `mapstructure:"body-template" description:"Path to a file containing the request body (go template). See https://creativeprojects.github.io/resticprofile/configuration/http_hooks/#body-template"`
37+
SkipTLS bool `mapstructure:"skip-tls-verification" description:"Enables insecure TLS (without verification), see also \"global.ca-certificates\""`
38+
}
39+
40+
// SendMonitoringHeader is used to send HTTP headers
41+
type SendMonitoringHeader struct {
42+
Name string `mapstructure:"name" regex:"^\\w([\\w-]+)\\w$" examples:"\"Authorization\";\"Cache-Control\";\"Content-Disposition\";\"Content-Type\"" description:"Name of the HTTP header"`
43+
Value ConfidentialValue `mapstructure:"value" examples:"\"Bearer ...\";\"Basic ...\";\"no-cache\";\"attachment;; filename=stats.txt\";\"application/json\";\"text/plain\";\"text/xml\"" description:"Value of the header"`
44+
}

contrib/templates/reference.gomd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ archetype: "chapter"
44
pre: "<b>6. </b>"
55
linkTitle: Reference
66
slug: reference
7+
aliases:
8+
- /configuration/reference
79
{{ if .Env.META_TITLE -}}
810
title: {{ .Env.META_TITLE }}
911
{{- else -}}

docs/version.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[params]
2-
version = "v0.30.0"
2+
version = "v0.31.0"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/creativeprojects/resticprofile
22

3-
go 1.24.2
3+
go 1.24.3
44

55
require (
66
github.com/Masterminds/semver/v3 v3.3.1

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
// These fields are populated by the goreleaser build
2828
var (
29-
version = "0.30.0-dev"
29+
version = "0.31.0-dev"
3030
commit = ""
3131
date = ""
3232
builtBy = ""

monitor/hook/sender_test.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,13 @@ func TestConfidentialURL(t *testing.T) {
252252

253253
profile := &config.Profile{
254254
Backup: &config.BackupSection{
255-
SectionWithScheduleAndMonitoring: config.SectionWithScheduleAndMonitoring{
256-
SendMonitoringSections: config.SendMonitoringSections{
257-
SendBefore: []config.SendMonitoringSection{
258-
{
259-
URL: config.NewConfidentialValue(serverURL),
255+
GenericSectionWithSchedule: config.GenericSectionWithSchedule{
256+
GenericSection: config.GenericSection{
257+
SendMonitoringSections: config.SendMonitoringSections{
258+
SendBefore: []config.SendMonitoringSection{
259+
{
260+
URL: config.NewConfidentialValue(serverURL),
261+
},
260262
},
261263
},
262264
},
@@ -286,13 +288,15 @@ func TestConfidentialHeader(t *testing.T) {
286288

287289
profile := &config.Profile{
288290
Backup: &config.BackupSection{
289-
SectionWithScheduleAndMonitoring: config.SectionWithScheduleAndMonitoring{
290-
SendMonitoringSections: config.SendMonitoringSections{
291-
SendBefore: []config.SendMonitoringSection{
292-
{
293-
URL: config.NewConfidentialValue(server.URL),
294-
Headers: []config.SendMonitoringHeader{
295-
{Name: headerKey, Value: config.NewConfidentialValue(headerValue)},
291+
GenericSectionWithSchedule: config.GenericSectionWithSchedule{
292+
GenericSection: config.GenericSection{
293+
SendMonitoringSections: config.SendMonitoringSections{
294+
SendBefore: []config.SendMonitoringSection{
295+
{
296+
URL: config.NewConfidentialValue(server.URL),
297+
Headers: []config.SendMonitoringHeader{
298+
{Name: headerKey, Value: config.NewConfidentialValue(headerValue)},
299+
},
296300
},
297301
},
298302
},

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
sonar.organization=creativeprojects
22
sonar.projectKey=creativeprojects_resticprofile
33
sonar.projectName=resticprofile
4-
sonar.projectVersion=0.30.0
4+
sonar.projectVersion=0.31.0
55

66
sonar.sources=.
77
sonar.exclusions=**/*_test.go,/docs/**

0 commit comments

Comments
 (0)