Restored progress

This commit is contained in:
Gu://em_ 2025-01-01 22:18:08 +01:00
parent f615cff76d
commit 5244f87411
19 changed files with 1061 additions and 0 deletions

42
internal/client/client.go Normal file
View file

@ -0,0 +1,42 @@
package client
import (
"log"
"cosync/internal/core"
"net"
"strconv"
)
//TODO: handle errors, review logging
func Run() {
log.Println("Loading config")
// Loading config into program
err := core.LoadConfig()
if err != nil {
log.Fatal(err)
panic(err)
}
config := core.GetConfig()
log.Println("Loaded")
serverAddress := config["server"].(string)
serverPort := strconv.Itoa(config["port"].(int))
log.Println("Connecting to server")
// Connect to the server
conn, err := net.Dial("tcp", serverAddress + ":" + serverPort)
if err != nil {
log.Fatal(err)
panic(err)
}
defer conn.Close()
log.Println("Connected")
conn.Write([]byte("Hello server"))
}

104
internal/client/setup.go Normal file
View file

@ -0,0 +1,104 @@
package client
import (
"cosync/internal/core"
)
const (
colorRed = "\033[0;31m"
colorGreen = "\033[1;32m"
colorNone = "\033[0m"
separator = "\n ============================================== \n\n"
)
func Setup() {
// Print logo
println(" ______ ______ \n .' ___ | .' ____ \\ \n/ .' \\_| .--. | (___ \\_| _ __ _ .--. .---. \n| | / .'`\\ \\ _.____`. [ \\ [ ][ `.-. | / /'`\\] \n\\ `.___.'\\| \\__. || \\____) | \\ '/ / | | | | | \\__. \n `.____ .' '.__.' \\______.'[\\_: / [___||__]'.___.' \n \\__.' ")
print(separator)
// Greeting
println("Hi, welcome to cosync, let's set it up")
println("Don't hesitate to give a look at the doc to know how it works ! :) \n -> https://docs.oblic-parallels.fr/cosync\n")
// Try create config files
err := core.MkConfig()
if err != nil {
println(colorRed + "Setup: Failed to create config files" + colorNone)
panic(err)
}
// Prompt for server/port assignation
print("Server address: ")
// ...
print("Port")
// ...
// Update config file accordingly
// ...
println(colorGreen + "Config has been updated with new values" + colorNone)
// Prompt to start service
print("Do you want to start CoSync now ? [Y/n]: ")
//...
}
//! This code no longer needs to exist as config is now handled by cosync/config
// Then it will be removed soon
// func mkConfigFiles(configFolderPath string, configFileName string, syncFileName string) {
// // Check if folder already exists
// _, err := os.Stat(configFolderPath)
// if err != nil && os.IsNotExist(err) {
// // Create config folder
// err = os.Mkdir(configFolderPath, os.ModePerm)
// if err != nil {
// println(colorRed + "\n\nCouldn't create the config folder" + colorNone)
// panic("Setup: Couldn't create " + configFolderPath + " -> " + err.Error())
// }
// }
// // Check if config file exists
// configFilePath := configFolderPath + configFileName
// _, err = os.Stat(configFilePath)
// if err != nil && os.IsNotExist(err) {
// // Create config file
// _, err = os.Create(configFilePath)
// if err != nil {
// println(colorRed + "\n\nCouldn't create the config file" + colorNone)
// panic("Setup: Couldn't create " + configFilePath + " -> " + err.Error())
// }
// }
// syncFilePath := configFolderPath + syncFileName
// _, err = os.Stat(syncFilePath)
// if err != nil && os.IsNotExist(err) {
// // Create config folder
// _, err = os.Create(syncFilePath)
// if err != nil {
// println(colorRed + "\n\nCouldn't create the sync file" + colorNone)
// panic("Setup: Couldn't create " + syncFilePath + " -> " + err.Error())
// }
// }
// }
// func WriteDefaultConfig(configFilePath string) {
// // Open file
// f, err := os.Open(configFilePath)
// if err != nil {
// println(colorRed + "\n\nCouldn't open the config file" + colorNone)
// panic("Setup: Couldn't access " + configFilePath + " -> " + err.Error())
// }
// defer f.Close()
// // Write file
// // TODO: import pre existing config file
// _, err = f.WriteString("{}")
// if err != nil {
// println(colorRed + "\n\nCouldn't open the config file" + colorNone)
// panic("Setup: Couldn't access " + configFilePath + " -> " + err.Error())
// }
// }

63
internal/core/config.go Normal file
View file

@ -0,0 +1,63 @@
package core
import (
"errors"
"os/user"
"github.com/spf13/viper"
)
// Config will follow this model
// server : string
// port : int
// Load config file
func LoadConfig() error {
// Get current user
currentUser, err := user.Current()
if err != nil {
// panic("LoadConfig: Failed to retrieve curent user -> " + err.Error())
return err
}
// Set default values
viper.SetDefault("server", "example.com")
viper.SetDefault("port", 5660)
// Set config paths
viper.SetConfigName("config")
viper.SetConfigType("json")
viper.AddConfigPath("/etc/cosync/")
viper.AddConfigPath("/home/" + currentUser.Username + "/.config/cosync")
// Read from config files
err = viper.ReadInConfig()
if err != nil {
panic("LoadConfig: Failed to read config file -> " + err.Error())
// return err
}
return nil
}
// Generate config folder and files
func MkConfig() error {
return errors.New("not implemented")
}
// Load sync list from sync file
func LoadSyncList() error {
return errors.New("not implemented")
}
// returns all config variables
// ! WARNING: Do not use this function without executing LoadConfig prior
func GetConfig() map[string]any {
return viper.AllSettings()
}

25
internal/core/models.go Normal file
View file

@ -0,0 +1,25 @@
package core
import (
"time"
)
type File struct {
ID string
ParentDirectory string
Metadata FileMetadata
State SynchronizationState
}
type FileMetadata struct {
Name string
Size int64
ModifiedTime time.Time
Hash string // For comparing file content
}
type SynchronizationState struct {
Status string // "pending", "in_progress", "completed", "error"
Progress float64
LastError error
}

8
internal/core/utils.go Normal file
View file

@ -0,0 +1,8 @@
package core
//! Not implemented
func CompareFiles(file1 File, file2 File) bool {
return false
}

83
internal/server/server.go Normal file
View file

@ -0,0 +1,83 @@
package server
import (
"bufio"
"cosync/internal/core"
"io"
"log"
"net"
"strconv"
)
// TODO: handle errors, review logging
func Run() {
log.Println("Loading config")
// Loading config into program
err := core.LoadConfig()
if err != nil {
log.Fatal(err)
panic(err)
}
config := core.GetConfig()
log.Println("Loaded")
serverPort := strconv.Itoa(config["port"].(int))
// Listening
listener, err := net.Listen("tcp", ":" + serverPort)
if err != nil {
panic(err)
}
defer listener.Close()
log.Println("Server listening on port " + serverPort)
for {
conn, err := listener.Accept()
if err != nil {
log.Println("Error accepting connection:", err)
continue
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
// tells the connector to close once exchange ends
defer conn.Close()
clientAddress := conn.RemoteAddr().String()
log.Println("Accepting from ", clientAddress)
// Read the message sent by the client (line by line)
reader := bufio.NewReader(conn)
for {
message, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
log.Println(clientAddress, " disconnected")
return
}
log.Println("Error reading from ", clientAddress, ": ", err)
return
}
// to rm
println(message)
// fmt.Println("Received from client:", message)
// Process the message (e.g., send a response)
// ...
// Send a response to the client
_, err = conn.Write([]byte("Server received your message\n"))
if err != nil {
log.Println("Error replying to", clientAddress,": ", err)
return
}
}
}

1
internal/server/setup.go Normal file
View file

@ -0,0 +1 @@
package server