Skip to content

Commit

Permalink
Add exit code in some cases when error is not returned but printed (#45)
Browse files Browse the repository at this point in the history
* bump versions

* added docker publish

* Added exit status in cases when no error returned

* bump docker build
  • Loading branch information
mpostument authored Feb 8, 2022
1 parent fc94807 commit 87fce97
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
uses: docker/build-push-action@v2.8.0
uses: docker/build-push-action@v2.9.0
with:
context: .
push: true
Expand Down
26 changes: 25 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var rootCmd = &cobra.Command{
Use: "grafana-sync",
Short: "Root command for grafana interaction",
Long: `Root command for grafana interaction.`,
Version: "1.4.1",
Version: "1.4.5",
}

var pullDashboardsCmd = &cobra.Command{
Expand Down Expand Up @@ -65,6 +65,9 @@ only dashboards with given tag are pulled`,
if err := grafana.PullDashboard(url, apiKey, directory, tag, folderId); err != nil {
log.Fatalln("Pull dashboards command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}

Expand Down Expand Up @@ -94,6 +97,9 @@ var pushDashboardsCmd = &cobra.Command{
if err := grafana.PushDashboard(url, apiKey, directory, folderId); err != nil {
log.Fatalln("Push dashboards command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}

Expand All @@ -109,6 +115,9 @@ Directory name specified by flag --directory.`,
if err := grafana.PullFolders(url, apiKey, directory); err != nil {
log.Fatalln("Pull folders command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}

Expand All @@ -123,6 +132,9 @@ var pushFoldersCmd = &cobra.Command{
if err := grafana.PushFolder(url, apiKey, directory); err != nil {
log.Fatalln("Push folders command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}

Expand All @@ -138,6 +150,9 @@ Directory name specified by flag --directory.`,
if err := grafana.PullNotifications(url, apiKey, directory); err != nil {
log.Fatalln("Pull notifications command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}

Expand All @@ -152,6 +167,9 @@ var pushNotificationsCmd = &cobra.Command{
if err := grafana.PushNotification(url, apiKey, directory); err != nil {
log.Fatalln("Push notifications command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}

Expand All @@ -167,6 +185,9 @@ Directory name specified by flag --directory.`,
if err := grafana.PullDatasources(url, apiKey, directory); err != nil {
log.Fatalln("Pull datasources command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}

Expand All @@ -181,6 +202,9 @@ var pushDataSourcesCmd = &cobra.Command{
if err := grafana.PushDatasources(url, apiKey, directory); err != nil {
log.Fatalln("Push datasources command failed", err)
}
if grafana.ExecutionErrorHappened {
os.Exit(1)
}
},
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/mpostument/grafana-sync
go 1.16

require (
github.com/grafana-tools/sdk v0.0.0-20211220201350-966b3088eec9
github.com/grafana-tools/sdk v0.0.0-20220203092117-edae16afa87b
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.3.0
github.com/spf13/viper v1.10.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ github.com/googleapis/gax-go/v2 v2.1.0/go.mod h1:Q3nei7sK6ybPYH7twZdmQpAd1MKb7pf
github.com/googleapis/gax-go/v2 v2.1.1/go.mod h1:hddJymUZASv3XPyGkUpKj8pPO47Rmb0eJc8R6ouapiM=
github.com/gosimple/slug v1.1.1 h1:fRu/digW+NMwBIP+RmviTK97Ho/bEj/C9swrCspN3D4=
github.com/gosimple/slug v1.1.1/go.mod h1:ER78kgg1Mv0NQGlXiDe57DpCyfbNywXXZ9mIorhxAf0=
github.com/grafana-tools/sdk v0.0.0-20211220201350-966b3088eec9 h1:LQAhgcUPnzdjU/OjCJaLlPQI7NmQCRlfjMPSA1VegvA=
github.com/grafana-tools/sdk v0.0.0-20211220201350-966b3088eec9/go.mod h1:AHHlOEv1+GGQ3ktHMlhuTUwo3zljV3QJbC0+8o2kn+4=
github.com/grafana-tools/sdk v0.0.0-20220203092117-edae16afa87b h1:R9LID2XreyUOQfJ/NKLGuYOF4/Wz6ljmYFAhlOaHVQ4=
github.com/grafana-tools/sdk v0.0.0-20220203092117-edae16afa87b/go.mod h1:AHHlOEv1+GGQ3ktHMlhuTUwo3zljV3QJbC0+8o2kn+4=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/consul/api v1.11.0/go.mod h1:XjsvQN+RJGWI2TWy1/kqaE16HrR2J/FWgkYjdZQsX9M=
github.com/hashicorp/consul/api v1.12.0/go.mod h1:6pVBMo0ebnYdt2S3H87XhekM/HHrUoTD2XXb/VrZVy0=
Expand Down
2 changes: 2 additions & 0 deletions grafana/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path/filepath"
)

var ExecutionErrorHappened = false

func writeToFile(directory string, content []byte, name string, tag string) error {
var (
err error
Expand Down
5 changes: 4 additions & 1 deletion grafana/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func PullDashboard(grafanaURL string, apiKey string, directory string, tag strin
)

ctx := context.Background()

c, err := sdk.NewClient(grafanaURL, apiKey, sdk.DefaultHTTPClient)

if err != nil {
Expand All @@ -44,6 +43,7 @@ func PullDashboard(grafanaURL string, apiKey string, directory string, tag strin
for _, link := range boardLinks {
if rawBoard, meta, err = c.GetDashboardByUID(ctx, link.UID); err != nil {
log.Printf("%s for %s\n", err, link.URI)
ExecutionErrorHappened = true
continue
}
rawBoard.ID = 0
Expand Down Expand Up @@ -78,11 +78,13 @@ func PushDashboard(grafanaURL string, apiKey string, directory string, folderId
if strings.HasSuffix(file.Name(), ".json") {
if rawBoard, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", directory, file.Name())); err != nil {
log.Println(err)
ExecutionErrorHappened = true
continue
}
var board sdk.Board
if err = json.Unmarshal(rawBoard, &board); err != nil {
log.Println(err)
ExecutionErrorHappened = true
continue
}
params := sdk.SetDashboardParams{
Expand All @@ -91,6 +93,7 @@ func PushDashboard(grafanaURL string, apiKey string, directory string, folderId
}
if _, err := c.SetDashboard(ctx, board, params); err != nil {
log.Printf("error on importing dashboard %s", board.Title)
ExecutionErrorHappened = true
continue
}
}
Expand Down
2 changes: 2 additions & 0 deletions grafana/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ func PushDatasources(grafanaURL string, apiKey string, directory string) error {
var datasource sdk.Datasource
if err = json.Unmarshal(rawFolder, &datasource); err != nil {
log.Println(err)
ExecutionErrorHappened = true
continue
}
if _, err := c.CreateDatasource(ctx, datasource); err != nil {
log.Printf("error on importing folder %s", datasource.Name)
ExecutionErrorHappened = true
continue
}
}
Expand Down
3 changes: 3 additions & 0 deletions grafana/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,18 @@ func PushFolder(grafanaURL string, apiKey string, directory string) error {
if strings.HasSuffix(file.Name(), ".json") {
if rawFolder, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", directory, file.Name())); err != nil {
log.Println(err)
ExecutionErrorHappened = true
continue
}
var folder sdk.Folder
if err = json.Unmarshal(rawFolder, &folder); err != nil {
log.Println(err)
ExecutionErrorHappened = true
continue
}
if _, err := c.CreateFolder(ctx, folder); err != nil {
log.Printf("error on importing folder %s", folder.Title)
ExecutionErrorHappened = true
continue
}
}
Expand Down
3 changes: 3 additions & 0 deletions grafana/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,18 @@ func PushNotification(grafanaURL string, apiKey string, directory string) error
if strings.HasSuffix(file.Name(), ".json") {
if rawFolder, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", directory, file.Name())); err != nil {
log.Println(err)
ExecutionErrorHappened = true
continue
}
var notification sdk.AlertNotification
if err = json.Unmarshal(rawFolder, &notification); err != nil {
log.Println(err)
ExecutionErrorHappened = true
continue
}
if _, err := c.CreateAlertNotification(ctx, notification); err != nil {
log.Printf("error on importing notification %s", notification.Name)
ExecutionErrorHappened = true
continue
}
}
Expand Down

0 comments on commit 87fce97

Please sign in to comment.