Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 1dd7942

Browse files
committed
Improved error handling
1 parent ac3be4b commit 1dd7942

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

pkg/internal/git_api.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package internal
22

33
import (
4+
"errors"
45
"fmt"
56
"io/ioutil"
67
"time"
@@ -29,9 +30,12 @@ type GitApi struct {
2930

3031
// NewGitApi creates a new NewGitApi instance
3132
func NewGitApi(gitUrl string, privateKeyFile string) *GitApi {
32-
authenticator, err := createAuthenticator(privateKeyFile)
33+
authenticator, err := createPublicKeys(privateKeyFile)
3334
if err != nil {
34-
log.Fatal("authentication failed", "error", err.Error())
35+
log.WithFields(log.Fields{
36+
"error": err,
37+
"private-key-file": privateKeyFile,
38+
}).Fatal("Failed to load publiy key from the private key.")
3539
}
3640
inMemoryStore, inMemoryFileSystem := createInMemory()
3741
gitApi := GitApi{gitUrl, authenticator, *inMemoryStore, inMemoryFileSystem, nil}
@@ -40,11 +44,13 @@ func NewGitApi(gitUrl string, privateKeyFile string) *GitApi {
4044
}
4145

4246
// helper function to create the git authenticator
43-
func createAuthenticator(privateKeyFile string) (*ssh.PublicKeys, error) {
47+
func createPublicKeys(privateKeyFile string) (*ssh.PublicKeys, error) {
48+
if privateKeyFile == "" {
49+
return nil, errors.New("Private key must not be empty.")
50+
}
4451
// git authentication with ssh
4552
authenticator, err := ssh.NewPublicKeysFromFile("git", privateKeyFile, "")
4653
if err != nil {
47-
log.Fatal("generate public keys failed", "error", err.Error())
4854
return nil, err
4955
}
5056

pkg/internal/synchronizer.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ func NewSynchronizer(options SynchronizeOptions) *Synchronization {
4141
options: options,
4242
}
4343

44+
log.WithFields(log.Fields{
45+
"job": options.JobName,
46+
"repository-url": options.GitRepositoryUrl,
47+
"private-key-file": options.PrivateKeyFile,
48+
"grafana-url": options.GrafanaUrl,
49+
}).Info("Initialize synchronizer job.")
50+
4451
synchronization.grafanaApi = NewGrafanaApi(options.GrafanaUrl, options.GrafanaToken)
4552
synchronization.gitApi = NewGitApi(options.GitRepositoryUrl, options.PrivateKeyFile)
4653

@@ -56,10 +63,8 @@ type Synchronization struct {
5663
// Executes the synchronization using the configuration stored in this struct.
5764
func (s *Synchronization) Synchronize(dryRun bool) error {
5865
log.WithFields(log.Fields{
59-
"job": s.options.JobName,
60-
"dry-run": strconv.FormatBool(dryRun),
61-
"repository-url": s.options.GitRepositoryUrl,
62-
"grafana-url": s.options.GrafanaUrl,
66+
"job": s.options.JobName,
67+
"dry-run": strconv.FormatBool(dryRun),
6368
}).Info("Starting synchronization.")
6469

6570
// push dashboard into Git
@@ -78,6 +83,10 @@ func (s *Synchronization) Synchronize(dryRun bool) error {
7883
}
7984
}
8085

86+
log.WithFields(log.Fields{
87+
"job": s.options.JobName,
88+
}).Info("Job was successfully completed.")
89+
8190
return nil
8291
}
8392

0 commit comments

Comments
 (0)