Skip to content

Commit ef29485

Browse files
committed
Add hint when config is malformed
1 parent db591ed commit ef29485

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

cmd/git-backup/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77
"os"
88
"path/filepath"
9+
"time"
910
)
1011

1112
var configFilePath = flag.String("config.file", "git-backup.yml", "The path to your config file.")
@@ -15,7 +16,14 @@ func main() {
1516
flag.Parse()
1617

1718
config := loadConfig()
18-
for _, source := range config.GetSources() {
19+
sources := config.GetSources()
20+
if len(sources) == 0 {
21+
log.Printf("Found a config file at [%s] but detected no sources. Are you sure the file is properly formed?", *configFilePath)
22+
os.Exit(111)
23+
}
24+
repoCount := 0
25+
backupStart := time.Now()
26+
for _, source := range sources {
1927
sourceName := source.GetName()
2028
log.Printf("=== %s ===", sourceName)
2129
if err := source.Test(); err != nil {
@@ -41,7 +49,9 @@ func main() {
4149
os.Exit(100)
4250
}
4351
}
52+
repoCount++
4453
}
54+
log.Printf("Backed up %d repositories in %s", repoCount, time.Now().Sub(backupStart))
4555
}
4656

4757
func loadConfig() gitbackup.Config {

config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package git_backup
22

33
import (
4-
"gopkg.in/yaml.v2"
4+
"gopkg.in/yaml.v3"
55
"io"
66
"os"
77
)
@@ -54,7 +54,7 @@ func LoadFile(path string) (out Config, err error) {
5454

5555
func LoadReader(reader io.Reader) (out Config, err error) {
5656
dec := yaml.NewDecoder(reader)
57-
dec.SetStrict(true)
57+
dec.KnownFields(true)
5858
err = dec.Decode(&out)
5959
out.setDefaults()
6060
return

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/google/go-github/v43 v43.0.0
88
github.com/xanzy/go-gitlab v0.60.0
99
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
10-
gopkg.in/yaml.v2 v2.4.0
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
1111
)
1212

1313
require (
@@ -34,4 +34,5 @@ require (
3434
google.golang.org/appengine v1.6.7 // indirect
3535
google.golang.org/protobuf v1.27.1 // indirect
3636
gopkg.in/warnings.v0 v0.1.2 // indirect
37+
gopkg.in/yaml.v2 v2.4.0 // indirect
3738
)

0 commit comments

Comments
 (0)