From 87fce975c7261c045c024ff8ab31ae2c49fa936d Mon Sep 17 00:00:00 2001 From: Maksym Postument Date: Tue, 8 Feb 2022 18:27:36 +0200 Subject: [PATCH] Add exit code in some cases when error is not returned but printed (#45) * bump versions * added docker publish * Added exit status in cases when no error returned * bump docker build --- .github/workflows/publish_docker.yml | 2 +- cmd/root.go | 26 +++++++++++++++++++++++++- go.mod | 2 +- go.sum | 4 ++-- grafana/common.go | 2 ++ grafana/dashboard.go | 5 ++++- grafana/datasource.go | 2 ++ grafana/folder.go | 3 +++ grafana/notification.go | 3 +++ 9 files changed, 43 insertions(+), 6 deletions(-) diff --git a/.github/workflows/publish_docker.yml b/.github/workflows/publish_docker.yml index 847f028..7617a34 100644 --- a/.github/workflows/publish_docker.yml +++ b/.github/workflows/publish_docker.yml @@ -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 diff --git a/cmd/root.go b/cmd/root.go index 4fdf7e1..a88bdea 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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{ @@ -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) + } }, } @@ -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) + } }, } @@ -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) + } }, } @@ -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) + } }, } @@ -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) + } }, } @@ -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) + } }, } @@ -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) + } }, } @@ -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) + } }, } diff --git a/go.mod b/go.mod index 370794a..b928673 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index da25459..1d6c4ed 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/grafana/common.go b/grafana/common.go index 6bedd31..a95f250 100644 --- a/grafana/common.go +++ b/grafana/common.go @@ -7,6 +7,8 @@ import ( "path/filepath" ) +var ExecutionErrorHappened = false + func writeToFile(directory string, content []byte, name string, tag string) error { var ( err error diff --git a/grafana/dashboard.go b/grafana/dashboard.go index 0f92434..43a76d3 100644 --- a/grafana/dashboard.go +++ b/grafana/dashboard.go @@ -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 { @@ -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 @@ -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{ @@ -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 } } diff --git a/grafana/datasource.go b/grafana/datasource.go index aaaf641..087dfd6 100644 --- a/grafana/datasource.go +++ b/grafana/datasource.go @@ -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 } } diff --git a/grafana/folder.go b/grafana/folder.go index 979d0b6..6a2a1ff 100644 --- a/grafana/folder.go +++ b/grafana/folder.go @@ -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 } } diff --git a/grafana/notification.go b/grafana/notification.go index a92a78e..bea2d21 100644 --- a/grafana/notification.go +++ b/grafana/notification.go @@ -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, ¬ification); 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 } }