Explorar el Código

codestyle: extract code into methods

bergquist hace 8 años
padre
commit
7858965117

+ 6 - 6
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{

+ 38 - 19
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
-		}
 
-		if !strings.HasSuffix(fileInfo.Name(), ".json") {
-			return nil
+		isValid, err := validateWalkablePath(fileInfo)
+		if !isValid {
+			return err
 		}
 
-		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 {