173 lines
3.8 KiB
Plaintext
173 lines
3.8 KiB
Plaintext
package lab
|
|
|
|
|
|
import (
|
|
"bufio"
|
|
"os/user"
|
|
"time"
|
|
|
|
// "fmt"
|
|
// "io"q
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"cosync/internal/client"
|
|
)
|
|
|
|
const (
|
|
colorRed = "\033[0;31m"
|
|
colorGreen = "\033[1;32m"
|
|
colorNone = "\033[0m"
|
|
separator = "\n ============================================== \n\n"
|
|
)
|
|
|
|
var (
|
|
currentUser, _ = user.Current()
|
|
configFolder = "/home/" + currentUser.Username + "/.config/cosync/"
|
|
configFilePath = configFolder + "files.sync"
|
|
server = "https://sync.oblic-parallels.fr"
|
|
)
|
|
|
|
type file struct {
|
|
path string
|
|
modTime time.Time
|
|
}
|
|
|
|
func main() {
|
|
|
|
// Print logo
|
|
println(" ______ ______ \n .' ___ | .' ____ \\ \n/ .' \\_| .--. | (___ \\_| _ __ _ .--. .---. \n| | / .'`\\ \\ _.____`. [ \\ [ ][ `.-. | / /'`\\] \n\\ `.___.'\\| \\__. || \\____) | \\ '/ / | | | | | \\__. \n `.____ .' '.__.' \\______.'[\\_: / [___||__]'.___.' \n \\__.' ")
|
|
|
|
// Open config file or create it if doesn't exists
|
|
configFile, err := os.Open(configFilePath)
|
|
if err == nil {
|
|
client.Setup()
|
|
}
|
|
defer configFile.Close()
|
|
|
|
// Get status from the server
|
|
resp, err := http.Get(server)
|
|
if err != nil {
|
|
print("\n\n" + colorRed)
|
|
panic(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Print server status
|
|
print(separator)
|
|
println("using server: ", server)
|
|
println("Response status:", resp.Status)
|
|
|
|
// Authenticate
|
|
// I'll do it later
|
|
|
|
print(separator)
|
|
println("Checking files :")
|
|
|
|
// Check if config file isn't empty
|
|
configFileStats, _ := configFile.Stat()
|
|
if configFileStats.Size() == 0 {
|
|
println("\n No files listed in config. There is nothing to do.")
|
|
println("\n\n > Exiting ...")
|
|
return
|
|
}
|
|
|
|
// Check and index all lines from config
|
|
|
|
configFileScanner := bufio.NewScanner(configFile)
|
|
register := make(map[string]file)
|
|
|
|
for configFileScanner.Scan() {
|
|
line := configFileScanner.Text()
|
|
|
|
print("\n ", line)
|
|
|
|
if line != "" {
|
|
index(line, register)
|
|
print(colorGreen + " ✔" + colorNone)
|
|
}
|
|
}
|
|
println()
|
|
|
|
// Read from server
|
|
// scanner := bufio.NewScanner(resp.Body)
|
|
// for scanner.Scan() {
|
|
// println(scanner.Text())
|
|
// }
|
|
|
|
// if err := scanner.Err(); err != nil {
|
|
// panic(err)
|
|
// }
|
|
|
|
//test
|
|
// f, err := os.Open("/home/guillem/.vscode/extensions/extensions.json")
|
|
// if err != nil {
|
|
// panic(err)
|
|
// }
|
|
// defer f.Close()
|
|
|
|
// fs, _ := f.Stat()
|
|
// if fs.ModTime().Before()
|
|
|
|
}
|
|
|
|
func index(line string, register map[string]file) {
|
|
|
|
decomposedLine := strings.Split(line, " : ")
|
|
|
|
// check separator
|
|
if len(decomposedLine) != 2 {
|
|
println(colorRed + " ✘\n\nThis line is not valid" + colorNone)
|
|
panic(line + " is not a valid syntax")
|
|
}
|
|
|
|
// Check if id is valid
|
|
if !IsValidID(decomposedLine[0]) {
|
|
println(colorRed + " ✘\n\nThis line is not valid" + colorNone)
|
|
panic("Id must be composed only by alphanumerical characters")
|
|
}
|
|
|
|
appendFiles(decomposedLine[0], decomposedLine[1], register)
|
|
|
|
}
|
|
|
|
func IsValidID(id string) bool {
|
|
return strings.ContainsFunc(id, IsSpecialRune)
|
|
}
|
|
func IsSpecialRune(r rune) bool {
|
|
return (r < 'A' || r > 'z' || (r > 'Z' && r < 'a')) && (r < '0' || r > '9') && r != '_'
|
|
}
|
|
|
|
func appendFiles(id string, path string, register map[string]file) {
|
|
|
|
// get subfiles and subfolders
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
println(colorRed + " ✘\n\n" + colorNone)
|
|
panic(err)
|
|
}
|
|
defer f.Close()
|
|
|
|
fstat, err := f.Stat()
|
|
if err != nil {
|
|
println(colorRed + " ✘\n\n" + colorNone)
|
|
panic(err)
|
|
}
|
|
|
|
// if path points to a folder
|
|
if fstat.IsDir() {
|
|
files, err := f.ReadDir(0)
|
|
if err != nil {
|
|
println(colorRed + " ✘\n\n" + colorNone)
|
|
panic(err)
|
|
}
|
|
|
|
// call this function recursively for all subfiles and folders
|
|
for i := 0; i < len(files); i++ {
|
|
appendFiles(id+"-"+files[i].Name(), path+"/"+files[i].Name(), register)
|
|
}
|
|
} else {
|
|
register[id] = file{path, fstat.ModTime()}
|
|
}
|
|
}
|