Last active
July 29, 2023 01:25
-
-
Save smothiki/8de93d8c1d34a95e000d85248815f4a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"os" | |
"path/filepath" | |
"sync" | |
"time" | |
) | |
// Configuration represents the data structure for config.json. | |
type Configuration struct { | |
DefaultRetention int `json:"default_retention"` | |
SpecificRetention map[int]int `json:"specific_retention"` | |
} | |
const ( | |
defaultRetention = 30 | |
) | |
func loadConfiguration(configPath string) (Configuration, error) { | |
config := Configuration{ | |
DefaultRetention: defaultRetention, | |
SpecificRetention: make(map[int]int), | |
} | |
file, err := os.Open(configPath) | |
if err != nil { | |
return config, err | |
} | |
defer file.Close() | |
decoder := json.NewDecoder(file) | |
if err := decoder.Decode(&config); err != nil { | |
return config, err | |
} | |
return config, nil | |
} | |
func deleteFilesOlderThan(directory string, days int) error { | |
limitDate := time.Now().AddDate(0, 0, -days) | |
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error { | |
if err != nil { | |
return err | |
} | |
if !info.IsDir() && info.ModTime().Before(limitDate) { | |
fmt.Printf("Deleting file: %s\n", path) | |
return os.Remove(path) | |
} | |
return nil | |
}) | |
return err | |
} | |
func processCompanyFiles(companyDir string, retention int, wg *sync.WaitGroup) { | |
defer wg.Done() | |
directories, err := filepath.Glob(filepath.Join(companyDir, "*", "*", "*", "*", "*", "*")) | |
if err != nil { | |
fmt.Printf("Error: %v\n", err) | |
return | |
} | |
for _, dir := range directories { | |
err := deleteFilesOlderThan(dir, retention) | |
if err != nil { | |
fmt.Printf("Error deleting files from %s: %v\n", dir, err) | |
} | |
} | |
} | |
func main() { | |
// Replace "config.json" with the path to the configuration file. | |
configPath := "config.json" | |
config, err := loadConfiguration(configPath) | |
if err != nil { | |
fmt.Printf("Error loading configuration: %v\n", err) | |
return | |
} | |
// Replace "root_directory" with the path to the root directory where company directories are located. | |
rootPath := "root_directory" | |
companies, err := filepath.Glob(filepath.Join(rootPath, "*")) | |
if err != nil { | |
fmt.Printf("Error: %v\n", err) | |
return | |
} | |
var wg sync.WaitGroup | |
for _, companyDir := range companies { | |
companyID := filepath.Base(companyDir) | |
retention, ok := config.SpecificRetention[companyID] | |
if !ok { | |
retention = config.DefaultRetention | |
} | |
wg.Add(1) | |
go processCompanyFiles(companyDir, retention, &wg) | |
} | |
wg.Wait() | |
fmt.Println("File deletion process completed.") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"path/filepath" | |
"strconv" | |
"time" | |
) | |
func extractTimestampFromPath(path string) (time.Time, error) { | |
_, file := filepath.Split(path) | |
components := filepath.SplitList(file) | |
if len(components) != 5 { | |
return time.Time{}, fmt.Errorf("invalid directory structure: %s", path) | |
} | |
year, err := strconv.Atoi(components[0]) | |
if err != nil { | |
return time.Time{}, fmt.Errorf("failed to parse year: %v", err) | |
} | |
month, err := strconv.Atoi(components[1]) | |
if err != nil { | |
return time.Time{}, fmt.Errorf("failed to parse month: %v", err) | |
} | |
day, err := strconv.Atoi(components[2]) | |
if err != nil { | |
return time.Time{}, fmt.Errorf("failed to parse day: %v", err) | |
} | |
hour, err := strconv.Atoi(components[3]) | |
if err != nil { | |
return time.Time{}, fmt.Errorf("failed to parse hour: %v", err) | |
} | |
min, err := strconv.Atoi(components[4]) | |
if err != nil { | |
return time.Time{}, fmt.Errorf("failed to parse minute: %v", err) | |
} | |
timestamp := time.Date(year, time.Month(month), day, hour, min, 0, 0, time.UTC) | |
return timestamp, nil | |
} | |
func main() { | |
// Replace "directory_path" with the actual path of the directory you want to extract the timestamp from. | |
directoryPath := "/data/company_id/device_id/2023/07/28/12/30/" | |
timestamp, err := extractTimestampFromPath(directoryPath) | |
if err != nil { | |
fmt.Println("Error:", err) | |
return | |
} | |
fmt.Println("Timestamp:", timestamp) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment