You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.3 KiB
Go
92 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
//"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
type Project struct {
|
|
Name string `json:"name"`
|
|
LastDir string `json:"last_directory"`
|
|
LastDirPath string `json:"last_directory_path"`
|
|
LastDirTime time.Time `json:"last_directory_time"`
|
|
}
|
|
|
|
func getLastProjects() []Project {
|
|
projectsDir := "d:/projects"
|
|
|
|
// Читаем список каталогов
|
|
entries, err := ioutil.ReadDir(projectsDir)
|
|
if err != nil {
|
|
fmt.Println("Ошибка при чтении каталога:", err)
|
|
return nil
|
|
}
|
|
|
|
var projects []string
|
|
for _, entry := range entries {
|
|
if entry.IsDir() {
|
|
projects = append(projects, entry.Name())
|
|
}
|
|
}
|
|
|
|
// Сортируем проекты по времени модификации
|
|
sort.Slice(projects, func(i, j int) bool {
|
|
infoI, _ := os.Stat(filepath.Join(projectsDir, projects[i]))
|
|
infoJ, _ := os.Stat(filepath.Join(projectsDir, projects[j]))
|
|
return infoI.ModTime().After(infoJ.ModTime())
|
|
})
|
|
|
|
// Выбираем 3 последних проекта
|
|
if len(projects) > 3 {
|
|
projects = projects[:3]
|
|
}
|
|
|
|
var result []Project
|
|
|
|
// Для каждого проекта находим самый свежий подкаталог
|
|
for _, project := range projects {
|
|
projectPath := filepath.Join(projectsDir, project)
|
|
subEntries, err := ioutil.ReadDir(projectPath)
|
|
if err != nil {
|
|
fmt.Println("Ошибка при чтении подкаталогов:", err)
|
|
continue
|
|
}
|
|
|
|
var lastDir string
|
|
var lastDirPath string
|
|
var lastDirTime time.Time
|
|
|
|
for _, subEntry := range subEntries {
|
|
if subEntry.IsDir() {
|
|
if lastDirTime.IsZero() || subEntry.ModTime().After(lastDirTime) {
|
|
lastDir = subEntry.Name()
|
|
lastDirPath = filepath.Join(projectPath, lastDir)
|
|
lastDirTime = subEntry.ModTime()
|
|
}
|
|
}
|
|
}
|
|
|
|
result = append(result, Project{
|
|
Name: project,
|
|
LastDir: lastDir,
|
|
LastDirPath: lastDirPath,
|
|
LastDirTime: lastDirTime,
|
|
})
|
|
}
|
|
|
|
// Выводим результат в формате JSON
|
|
//jsonResult, err := json.MarshalIndent(result, "", " ")
|
|
//if err != nil {
|
|
// fmt.Println("Ошибка при преобразовании в JSON:", err)
|
|
// return
|
|
//}
|
|
|
|
//fmt.Println(string(jsonResult))
|
|
return result
|
|
}
|