Skip to content

Commit

Permalink
Fix: Error while directory watching instead of Fatal
Browse files Browse the repository at this point in the history
  • Loading branch information
Unic-X committed Nov 27, 2024
1 parent 2d99baa commit 372021b
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 8 deletions.
1 change: 1 addition & 0 deletions Directory.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"dir":""}
78 changes: 78 additions & 0 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func ClientLoop(cmd *cobra.Command, args []string) {
}

switch selectedOption {

case "Upload File":
path, err := promptForFilePath()

Expand Down Expand Up @@ -147,6 +148,29 @@ func ClientLoop(cmd *cobra.Command, args []string) {
log.Warn("ssh request for finish-file-upload failed", "reply", string(reply))
continue
}

case "Add Directory to Sync":
dir, err := promptForDirectory()
if err != nil {
log.Error("Could not get directory for sync", "err", err)
continue
}

if dir == "" {
dir = "./.data" // Default directory
}

log.Infof("Updating Directory.json with directory: %s", dir)

// Update the JSON file
configPath := "Directory.json"
err = updateDirectoryInConfig(configPath, dir)
if err != nil {
log.Error("Failed to update directory in config file", "err", err)
continue
}

log.Infof("Directory.json updated successfully with: %s", dir)
case "Exit":
fmt.Println("Exiting client.")
_, _, err := sshC.SendRequest("close-connection", false, []byte(worker.Ip))
Expand Down Expand Up @@ -185,6 +209,25 @@ func SendWorkerDetails(worker server.Worker, sshC *ssh.Client) error {
return nil
}

func promptForDirectory() (string, error) {
var directory string
directoryPrompt := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Enter Directory to watch").
Prompt("? ").
Placeholder(".data").
Suggestions([]string{"./.data"}).
Value(&directory),
),
)
err := directoryPrompt.Run()
if err != nil {
return "", err
}
return directory, nil
}

func promptForIP() (string, error) {
var ip string
ipPrompt := huh.NewForm(
Expand Down Expand Up @@ -214,6 +257,7 @@ func promptForAction() (string, error) {
Options(
huh.NewOption("Upload File", "Upload File"),
huh.NewOption("List Directory", "List Directory"),
huh.NewOption("Add Directory to Sync", "Add Directory to Sync"),
huh.NewOption("Exit", "Exit"),
).
Value(&selectedOption),
Expand Down Expand Up @@ -256,6 +300,40 @@ func validateFilePath(input string) error {
return nil
}

func updateDirectoryInConfig(path, newDir string) error {
config := server.Config{}

if _, err := os.Stat(path); os.IsNotExist(err) {
config.Directory = newDir
} else {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open config file: %w", err)
}
defer file.Close()

decoder := json.NewDecoder(file)
if err := decoder.Decode(&config); err != nil {
return fmt.Errorf("failed to parse config file: %w", err)
}

config.Directory = newDir
}

file, err := os.Create(path)
if err != nil {
return fmt.Errorf("failed to create or truncate config file: %w", err)
}
defer file.Close()

encoder := json.NewEncoder(file)
if err := encoder.Encode(&config); err != nil {
return fmt.Errorf("failed to write updated config to file: %w", err)
}

return nil
}

func init() {
// Persistent flags for subcommands
rootCmd.AddCommand(clientCmd)
Expand Down
62 changes: 54 additions & 8 deletions server/worker.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package server

import (
"log"
"encoding/json"
"os"
"path/filepath"

"github.com/bsach64/goback/utils"
"github.com/charmbracelet/log"
)

// Worker is just a usual SFTP server that handles the file request
Expand All @@ -20,17 +22,25 @@ type Worker struct {
func (w *Worker) StartSFTPServer() {
wd, err := os.Getwd()
if err != nil {
log.Fatalf("Cannot get the working directory: %v", err)
log.Errorf("Cannot get the working directory: %v", err)
}

rsaPath := wd + "/private/id_rsa"
rsaPath := filepath.Join(wd, "private", "id_rsa")
configPath := filepath.Join(wd, "Directory.json")

// Start directory watcher
watcher, err := utils.WatchDirectory(wd + "/.data")
if err != nil {
log.Fatalf("Error while creating watcher: %v", err)
config := readConf(configPath)

watchDir := filepath.Join(wd, config.Directory)

if config.Directory != "" {
watcher, err := utils.WatchDirectory(watchDir)
if err != nil {
log.Errorf("Error while creating watcher: %v", err)
defer watcher.Close() // Ensure the watcher is cleaned up on server shutdown
}
} else {
log.Info("No directory is being watched you can add using", "command", "Add Directory to Sync")
}
defer watcher.Close() // Ensure the watcher is cleaned up on server shutdown

sftpServer := New(w.Ip, rsaPath, w.Port)
w.sftpServer = &sftpServer
Expand All @@ -45,3 +55,39 @@ func (w *Worker) StartSFTPServer() {
// Keep the worker process alive
select {}
}

type Config struct {
Directory string `json:"dir"`
}

func readConf(path string) Config {
config := Config{}

if _, err := os.Stat(path); os.IsNotExist(err) {
config.Directory = "./.data"
file, err := os.Create(path)
if err != nil {
log.Fatalf("Failed to create config file: %v", err)
}
defer file.Close()

encoder := json.NewEncoder(file)
if err := encoder.Encode(config); err != nil {
log.Fatalf("Failed to write default config to file: %v", err)
}
log.Printf("Config file created with default directory: %s", config.Directory)
} else {
file, err := os.Open(path)
if err != nil {
log.Fatalf("Failed to open config file: %v", err)
}
defer file.Close()

decoder := json.NewDecoder(file)
if err := decoder.Decode(&config); err != nil {
log.Fatalf("Failed to parse config file: %v", err)
}
}

return config
}

0 comments on commit 372021b

Please sign in to comment.