Skip to content

Commit

Permalink
Improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusoe committed Feb 11, 2022
1 parent ac3be4b commit 1dd7942
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
14 changes: 10 additions & 4 deletions pkg/internal/git_api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"errors"
"fmt"
"io/ioutil"
"time"
Expand Down Expand Up @@ -29,9 +30,12 @@ type GitApi struct {

// NewGitApi creates a new NewGitApi instance
func NewGitApi(gitUrl string, privateKeyFile string) *GitApi {
authenticator, err := createAuthenticator(privateKeyFile)
authenticator, err := createPublicKeys(privateKeyFile)
if err != nil {
log.Fatal("authentication failed", "error", err.Error())
log.WithFields(log.Fields{
"error": err,
"private-key-file": privateKeyFile,
}).Fatal("Failed to load publiy key from the private key.")
}
inMemoryStore, inMemoryFileSystem := createInMemory()
gitApi := GitApi{gitUrl, authenticator, *inMemoryStore, inMemoryFileSystem, nil}
Expand All @@ -40,11 +44,13 @@ func NewGitApi(gitUrl string, privateKeyFile string) *GitApi {
}

// helper function to create the git authenticator
func createAuthenticator(privateKeyFile string) (*ssh.PublicKeys, error) {
func createPublicKeys(privateKeyFile string) (*ssh.PublicKeys, error) {
if privateKeyFile == "" {
return nil, errors.New("Private key must not be empty.")
}
// git authentication with ssh
authenticator, err := ssh.NewPublicKeysFromFile("git", privateKeyFile, "")
if err != nil {
log.Fatal("generate public keys failed", "error", err.Error())
return nil, err
}

Expand Down
17 changes: 13 additions & 4 deletions pkg/internal/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func NewSynchronizer(options SynchronizeOptions) *Synchronization {
options: options,
}

log.WithFields(log.Fields{
"job": options.JobName,
"repository-url": options.GitRepositoryUrl,
"private-key-file": options.PrivateKeyFile,
"grafana-url": options.GrafanaUrl,
}).Info("Initialize synchronizer job.")

synchronization.grafanaApi = NewGrafanaApi(options.GrafanaUrl, options.GrafanaToken)
synchronization.gitApi = NewGitApi(options.GitRepositoryUrl, options.PrivateKeyFile)

Expand All @@ -56,10 +63,8 @@ type Synchronization struct {
// Executes the synchronization using the configuration stored in this struct.
func (s *Synchronization) Synchronize(dryRun bool) error {
log.WithFields(log.Fields{
"job": s.options.JobName,
"dry-run": strconv.FormatBool(dryRun),
"repository-url": s.options.GitRepositoryUrl,
"grafana-url": s.options.GrafanaUrl,
"job": s.options.JobName,
"dry-run": strconv.FormatBool(dryRun),
}).Info("Starting synchronization.")

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

log.WithFields(log.Fields{
"job": s.options.JobName,
}).Info("Job was successfully completed.")

return nil
}

Expand Down

0 comments on commit 1dd7942

Please sign in to comment.