Makefile, server entry point prototype, compareFiles function + other minor changes
This commit is contained in:
parent
2efb8c4130
commit
c1ccb61346
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
.vscode/
|
||||
lab/
|
||||
bin/*
|
5
Makefile
Normal file
5
Makefile
Normal file
|
@ -0,0 +1,5 @@
|
|||
client:
|
||||
go build -o bin/client cmd/client/main.go
|
||||
|
||||
server:
|
||||
go build -o bin/server cmd/server/main.go
|
|
@ -1,9 +1,10 @@
|
|||
package main
|
||||
package client
|
||||
|
||||
import (
|
||||
"cosync/internal/client"
|
||||
"cosync/internal/server"
|
||||
"cosync/tests"
|
||||
|
||||
// "cosync/tests"
|
||||
"os"
|
||||
)
|
||||
|
||||
|
@ -14,10 +15,9 @@ func main() {
|
|||
panic("Not enough arguments (try --help)")
|
||||
}
|
||||
|
||||
|
||||
for i := 1; i < argc; i++ {
|
||||
|
||||
switch (os.Args[i]) {
|
||||
switch os.Args[i] {
|
||||
|
||||
case "-s":
|
||||
server.Run()
|
||||
|
@ -33,8 +33,8 @@ func main() {
|
|||
case "--help":
|
||||
panic("Not implemented")
|
||||
|
||||
case "--test":
|
||||
tests.TestAll()
|
||||
// case "--test":
|
||||
// tests.TestAll()
|
||||
|
||||
default:
|
||||
panic("Bad argument : \"" + os.Args[i] + "\"")
|
||||
|
|
|
@ -1 +1,38 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"cosync/internal/server"
|
||||
// "cosync/tests"
|
||||
"os"
|
||||
)
|
||||
|
||||
// TODO: handle arguments correctly
|
||||
func main() {
|
||||
argc := len(os.Args)
|
||||
if argc < 2 {
|
||||
panic("Not enough arguments (try --help)")
|
||||
}
|
||||
|
||||
for i := 1; i < argc; i++ {
|
||||
|
||||
switch os.Args[i] {
|
||||
|
||||
case "-s":
|
||||
server.Run()
|
||||
// break
|
||||
|
||||
case "-p":
|
||||
panic("Not implemented")
|
||||
|
||||
case "--help":
|
||||
panic("Not implemented")
|
||||
|
||||
// case "--test":
|
||||
// tests.TestAll()
|
||||
|
||||
default:
|
||||
panic("Bad argument : \"" + os.Args[i] + "\"")
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,14 +7,11 @@ import (
|
|||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
|
||||
// Config will follow this model
|
||||
// server : string
|
||||
// port : int
|
||||
|
||||
|
||||
|
||||
|
||||
var config_loaded bool = false
|
||||
|
||||
// Load config file
|
||||
func LoadConfig() error {
|
||||
|
@ -43,6 +40,7 @@ func LoadConfig() error {
|
|||
// return err
|
||||
}
|
||||
|
||||
config_loaded = true
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -57,7 +55,16 @@ func LoadSyncList() error {
|
|||
}
|
||||
|
||||
// returns all config variables
|
||||
// ! WARNING: Do not use this function without executing LoadConfig prior
|
||||
func GetConfig() map[string]any {
|
||||
|
||||
if config_loaded {
|
||||
return viper.AllSettings()
|
||||
} else {
|
||||
err := LoadConfig()
|
||||
if err != nil {
|
||||
panic("GetConfig: Failed to load config file -> " + err.Error())
|
||||
}
|
||||
|
||||
return viper.AllSettings()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
|
||||
type File struct {
|
||||
ID string
|
||||
ParentDirectory string
|
||||
ParentDirectory Folder
|
||||
Metadata FileMetadata
|
||||
State SynchronizationState
|
||||
}
|
||||
|
@ -23,3 +23,12 @@ type SynchronizationState struct {
|
|||
Progress float64
|
||||
LastError error
|
||||
}
|
||||
|
||||
type Folder struct {
|
||||
ID string
|
||||
Files []File
|
||||
Subfolders []Folder
|
||||
|
||||
Name string
|
||||
Path string
|
||||
}
|
|
@ -1,8 +1,36 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
//! Not implemented
|
||||
func CompareFiles(file1 File, file2 File) bool {
|
||||
return false
|
||||
// Panic if e contains an error
|
||||
func Check(e error) {
|
||||
if e != nil {
|
||||
panic("Check: A fatal error occured during execution -> " + e.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if files correspond
|
||||
func CompareFiles(file1 File, file2 File) bool {
|
||||
return file1.Metadata.Hash == file2.Metadata.Hash
|
||||
}
|
||||
|
||||
// Gets the sha256 hash of a file's content
|
||||
func GetHash(file File) ([]byte, error) {
|
||||
// init
|
||||
hasher := sha256.New()
|
||||
file_path := "" //TODO: missing
|
||||
// Open file
|
||||
f, err := os.Open(file_path)
|
||||
if err != nil { // Path error
|
||||
return nil, err
|
||||
}
|
||||
// Compute hash
|
||||
io.Copy(hasher, f)
|
||||
sum := hasher.Sum(nil)
|
||||
|
||||
return sum, nil
|
||||
}
|
||||
|
|
22
tests/cosync_test.go
Normal file
22
tests/cosync_test.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"cosync/internal/client"
|
||||
"cosync/internal/server"
|
||||
)
|
||||
|
||||
func TestAll(t *testing.T) {
|
||||
TestClientServerCommunication(t)
|
||||
}
|
||||
|
||||
func TestClientServerCommunication(t *testing.T) {
|
||||
go client.Run()
|
||||
go server.Run()
|
||||
}
|
||||
|
||||
func TestConfigLoading(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestConfigCreation(t *testing.T) {
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
package tests
|
||||
|
||||
import (
|
||||
// "testing"
|
||||
"cosync/internal/client"
|
||||
"cosync/internal/server"
|
||||
)
|
||||
|
||||
func TestAll() bool {
|
||||
TestClientServerCommunication()
|
||||
return false
|
||||
}
|
||||
|
||||
func TestClientServerCommunication() bool {
|
||||
go client.Run()
|
||||
go server.Run()
|
||||
return false
|
||||
}
|
||||
|
||||
func TestConfigLoading() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func TestConfigCreation() bool {
|
||||
return false
|
||||
}
|
Loading…
Reference in a new issue