From 7858965117696b47c660bd835ae41686e3b604b8 Mon Sep 17 00:00:00 2001 From: bergquist Date: Thu, 18 Jan 2018 12:04:12 +0100 Subject: [PATCH] codestyle: extract code into methods --- .../wrapper/datasource_plugin_wrapper.go | 12 ++-- .../provisioning/dashboards/file_reader.go | 59 ++++++++++++------- 2 files changed, 45 insertions(+), 26 deletions(-) diff --git a/pkg/plugins/datasource/wrapper/datasource_plugin_wrapper.go b/pkg/plugins/datasource/wrapper/datasource_plugin_wrapper.go index 490fa0f573a..0afa2a400e4 100644 --- a/pkg/plugins/datasource/wrapper/datasource_plugin_wrapper.go +++ b/pkg/plugins/datasource/wrapper/datasource_plugin_wrapper.go @@ -28,12 +28,12 @@ func (tw *DatasourcePluginWrapper) Query(ctx context.Context, ds *models.DataSou pbQuery := &datasource.DatasourceRequest{ Datasource: &datasource.DatasourceInfo{ - Name: ds.Name, - Type: ds.Type, - Url: ds.Url, - Id: ds.Id, - OrgId: ds.OrgId, - JsonData: string(jsonData), + Name: ds.Name, + Type: ds.Type, + Url: ds.Url, + Id: ds.Id, + OrgId: ds.OrgId, + JsonData: string(jsonData), DecryptedSecureJsonData: ds.SecureJsonData.Decrypt(), }, TimeRange: &datasource.TimeRange{ diff --git a/pkg/services/provisioning/dashboards/file_reader.go b/pkg/services/provisioning/dashboards/file_reader.go index fbe1a03e287..6b9eeec9e48 100644 --- a/pkg/services/provisioning/dashboards/file_reader.go +++ b/pkg/services/provisioning/dashboards/file_reader.go @@ -135,35 +135,39 @@ func getOrCreateFolderId(cfg *DashboardsAsConfig, repo dashboards.Repository) (i return cmd.Result.Id, nil } +func resolveSymlink(fileinfo os.FileInfo, path string) (os.FileInfo, error) { + checkFilepath, err := filepath.EvalSymlinks(path) + if path != checkFilepath { + path = checkFilepath + fi, err := os.Lstat(checkFilepath) + if err != nil { + return nil, err + } + + return fi, nil + } + + return fileinfo, err +} + func createWalkFn(fr *fileReader, folderId int64) filepath.WalkFunc { return func(path string, fileInfo os.FileInfo, err error) error { if err != nil { return err } - if fileInfo.IsDir() { - if strings.HasPrefix(fileInfo.Name(), ".") { - return filepath.SkipDir - } - return nil + + isValid, err := validateWalkablePath(fileInfo) + if !isValid { + return err } - if !strings.HasSuffix(fileInfo.Name(), ".json") { - return nil - } - - checkFilepath, err := filepath.EvalSymlinks(path) - - if path != checkFilepath { - path = checkFilepath - fi, err := os.Lstat(checkFilepath) - if err != nil { - return err - } - fileInfo = fi + resolvedFileInfo, err := resolveSymlink(fileInfo, path) + if err != nil { + return err } cachedDashboard, exist := fr.cache.getCache(path) - if exist && cachedDashboard.UpdatedAt == fileInfo.ModTime() { + if exist && cachedDashboard.UpdatedAt == resolvedFileInfo.ModTime() { return nil } @@ -194,7 +198,7 @@ func createWalkFn(fr *fileReader, folderId int64) filepath.WalkFunc { } // break if db version is newer then fil version - if cmd.Result.Updated.Unix() >= fileInfo.ModTime().Unix() { + if cmd.Result.Updated.Unix() >= resolvedFileInfo.ModTime().Unix() { return nil } @@ -205,6 +209,21 @@ func createWalkFn(fr *fileReader, folderId int64) filepath.WalkFunc { } } +func validateWalkablePath(fileInfo os.FileInfo) (bool, error) { + if fileInfo.IsDir() { + if strings.HasPrefix(fileInfo.Name(), ".") { + return false, filepath.SkipDir + } + return false, nil + } + + if !strings.HasSuffix(fileInfo.Name(), ".json") { + return false, nil + } + + return true, nil +} + func (fr *fileReader) readDashboardFromFile(path string, folderId int64) (*dashboards.SaveDashboardItem, error) { reader, err := os.Open(path) if err != nil {