codestyle: extract code into methods

This commit is contained in:
bergquist
2018-01-18 12:04:12 +01:00
parent d6667c4fa0
commit 7858965117
2 changed files with 45 additions and 26 deletions

View File

@@ -28,12 +28,12 @@ func (tw *DatasourcePluginWrapper) Query(ctx context.Context, ds *models.DataSou
pbQuery := &datasource.DatasourceRequest{ pbQuery := &datasource.DatasourceRequest{
Datasource: &datasource.DatasourceInfo{ Datasource: &datasource.DatasourceInfo{
Name: ds.Name, Name: ds.Name,
Type: ds.Type, Type: ds.Type,
Url: ds.Url, Url: ds.Url,
Id: ds.Id, Id: ds.Id,
OrgId: ds.OrgId, OrgId: ds.OrgId,
JsonData: string(jsonData), JsonData: string(jsonData),
DecryptedSecureJsonData: ds.SecureJsonData.Decrypt(), DecryptedSecureJsonData: ds.SecureJsonData.Decrypt(),
}, },
TimeRange: &datasource.TimeRange{ TimeRange: &datasource.TimeRange{

View File

@@ -135,35 +135,39 @@ func getOrCreateFolderId(cfg *DashboardsAsConfig, repo dashboards.Repository) (i
return cmd.Result.Id, nil 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 { func createWalkFn(fr *fileReader, folderId int64) filepath.WalkFunc {
return func(path string, fileInfo os.FileInfo, err error) error { return func(path string, fileInfo os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }
if fileInfo.IsDir() {
if strings.HasPrefix(fileInfo.Name(), ".") { isValid, err := validateWalkablePath(fileInfo)
return filepath.SkipDir if !isValid {
} return err
return nil
} }
if !strings.HasSuffix(fileInfo.Name(), ".json") { resolvedFileInfo, err := resolveSymlink(fileInfo, path)
return nil if err != nil {
} return err
checkFilepath, err := filepath.EvalSymlinks(path)
if path != checkFilepath {
path = checkFilepath
fi, err := os.Lstat(checkFilepath)
if err != nil {
return err
}
fileInfo = fi
} }
cachedDashboard, exist := fr.cache.getCache(path) cachedDashboard, exist := fr.cache.getCache(path)
if exist && cachedDashboard.UpdatedAt == fileInfo.ModTime() { if exist && cachedDashboard.UpdatedAt == resolvedFileInfo.ModTime() {
return nil return nil
} }
@@ -194,7 +198,7 @@ func createWalkFn(fr *fileReader, folderId int64) filepath.WalkFunc {
} }
// break if db version is newer then fil version // 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 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) { func (fr *fileReader) readDashboardFromFile(path string, folderId int64) (*dashboards.SaveDashboardItem, error) {
reader, err := os.Open(path) reader, err := os.Open(path)
if err != nil { if err != nil {