2022-08-02 05:16:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-08-19 22:03:00 +00:00
|
|
|
"context"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"log"
|
2022-08-02 05:16:11 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
2022-08-22 19:53:27 +00:00
|
|
|
"path/filepath"
|
2022-08-02 05:16:11 +00:00
|
|
|
|
2022-08-19 22:03:00 +00:00
|
|
|
zordTree "g.arns.lt/zordsdavini/zord-tree"
|
2022-08-02 05:16:11 +00:00
|
|
|
)
|
|
|
|
|
2022-08-19 22:03:00 +00:00
|
|
|
type server struct {
|
|
|
|
UnimplementedTreeManagerServer
|
2022-08-02 05:16:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 22:03:00 +00:00
|
|
|
func (TreeManagerServer *server) GetSummery(_ context.Context, in *TreeRequest) (*Tree, error) {
|
2022-08-20 20:44:07 +00:00
|
|
|
log.Println(in)
|
2022-08-19 22:03:00 +00:00
|
|
|
tree, err := zordTree.BuildTree(dest, meta)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-02 05:16:11 +00:00
|
|
|
|
2022-08-19 22:03:00 +00:00
|
|
|
if *in.Path != "" {
|
|
|
|
ntree, err := tree.Slice(dest + *in.Path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return &Tree{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tree = ntree
|
2022-08-02 05:16:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 22:03:00 +00:00
|
|
|
if len(in.Filter) != 0 {
|
|
|
|
ntree, found := tree.Filter(simplifyFilter(in.Filter))
|
|
|
|
if !found {
|
|
|
|
return &Tree{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tree = ntree
|
|
|
|
}
|
|
|
|
|
|
|
|
formattedTree := FormatTreeSummery(tree)
|
|
|
|
|
|
|
|
return &formattedTree, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (TreeManagerServer *server) GetFile(_ context.Context, in *FileRequest) (*FileContent, error) {
|
2022-08-20 20:44:07 +00:00
|
|
|
log.Println(in)
|
2022-08-19 22:03:00 +00:00
|
|
|
tree, err := zordTree.BuildTree(dest, meta)
|
2022-08-02 05:16:11 +00:00
|
|
|
if err != nil {
|
2022-08-19 22:03:00 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
return nil, err
|
2022-08-02 05:16:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 22:03:00 +00:00
|
|
|
file, err := tree.FileById(in.Id)
|
2022-08-02 05:16:11 +00:00
|
|
|
if err != nil {
|
2022-08-19 22:03:00 +00:00
|
|
|
return nil, err
|
2022-08-02 05:16:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 22:03:00 +00:00
|
|
|
fileContent := GetContent(file)
|
|
|
|
|
|
|
|
return &fileContent, nil
|
|
|
|
}
|
|
|
|
|
2022-08-22 19:53:27 +00:00
|
|
|
func (TreeManagerServer *server) RebuildTree(ctx context.Context, in *TreeSecret) (*RebuildResponse, error) {
|
|
|
|
success := false
|
|
|
|
if in.Key == key {
|
|
|
|
err := removeContents(dest)
|
|
|
|
if err == nil {
|
|
|
|
Collect()
|
|
|
|
success = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response := RebuildResponse{Success: success}
|
|
|
|
return &response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeContents(dir string) error {
|
|
|
|
d, err := os.Open(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer d.Close()
|
|
|
|
names, err := d.Readdirnames(-1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, name := range names {
|
|
|
|
err = os.RemoveAll(filepath.Join(dir, name))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-19 22:03:00 +00:00
|
|
|
func simplifyFilter(filter []*TreeRequest_Filter) map[string]string {
|
|
|
|
simpleFilter := make(map[string]string)
|
|
|
|
for _, f := range filter {
|
|
|
|
simpleFilter[f.Key] = f.Value
|
2022-08-02 05:16:11 +00:00
|
|
|
}
|
2022-08-19 22:03:00 +00:00
|
|
|
|
|
|
|
return simpleFilter
|
2022-08-02 05:16:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 22:03:00 +00:00
|
|
|
func Run() {
|
|
|
|
if _, err := os.Stat(dest); os.IsNotExist(err) {
|
|
|
|
log.Fatalf("Destination directory does not exist.")
|
2022-08-02 05:16:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 22:03:00 +00:00
|
|
|
ln, err := net.Listen("tcp", ":8301")
|
2022-08-02 05:16:11 +00:00
|
|
|
if err != nil {
|
2022-08-19 22:03:00 +00:00
|
|
|
log.Fatalf("Failed to listen: " + err.Error())
|
2022-08-02 05:16:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 22:03:00 +00:00
|
|
|
var opts []grpc.ServerOption
|
|
|
|
grpcServer := grpc.NewServer(opts...)
|
|
|
|
RegisterTreeManagerServer(grpcServer, &server{})
|
|
|
|
err = grpcServer.Serve(ln)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to serve: %v", err)
|
2022-08-02 05:16:11 +00:00
|
|
|
}
|
|
|
|
}
|