Clean up code and add --dest-dir

This commit is contained in:
Elara 2020-12-06 00:08:39 -08:00
parent 52b3490e08
commit 8331e6b543
3 changed files with 10 additions and 11 deletions

View File

@ -117,10 +117,7 @@ func (config *Config) ReadFile(filePath string) {
} }
// Execute action specified in config // Execute action specified in config
func (config *Config) ExecuteAction(srcDir string) { func (config *Config) ExecuteAction(srcDir string, destDir string) {
// Get user's home directory
homeDir, err := os.UserHomeDir()
if err != nil { log.Fatal().Err(err).Msg("Error getting home directory") }
// Use ConsoleWriter logger // Use ConsoleWriter logger
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{}) log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Hook(FatalHook{})
// If action is file // If action is file
@ -131,7 +128,7 @@ func (config *Config) ExecuteAction(srcDir string) {
// Close source file at the end of this function // Close source file at the end of this function
defer src.Close() defer src.Close()
// Create file in user's Downloads directory // Create file in user's Downloads directory
dst, err := os.Create(homeDir + "/Downloads/" + config.ActionData) dst, err := os.Create(filepath.Clean(destDir) + "/" + config.ActionData)
if err != nil { log.Fatal().Err(err).Msg("Error creating file") } if err != nil { log.Fatal().Err(err).Msg("Error creating file") }
// Close destination file at the end of this function // Close destination file at the end of this function
defer dst.Close() defer dst.Close()
@ -146,7 +143,7 @@ func (config *Config) ExecuteAction(srcDir string) {
// If action is dir // If action is dir
} else if config.ActionType == "dir" { } else if config.ActionType == "dir" {
// Set destination directory to ~/Downloads/{dir name} // Set destination directory to ~/Downloads/{dir name}
dstDir := homeDir + "/Downloads/" + config.ActionData dstDir := filepath.Clean(destDir) + "/" + config.ActionData
// Try to create destination directory // Try to create destination directory
err := os.MkdirAll(dstDir, 0755) err := os.MkdirAll(dstDir, 0755)
if err != nil { log.Fatal().Err(err).Msg("Error creating directory") } if err != nil { log.Fatal().Err(err).Msg("Error creating directory") }
@ -179,7 +176,7 @@ func (config *Config) ExecuteAction(srcDir string) {
// If regular file // If regular file
case tar.TypeReg: case tar.TypeReg:
// Try to create containing folder ignoring errors // Try to create containing folder ignoring errors
_ = os.MkdirAll(strings.TrimSuffix(targetPath, filepath.Base(targetPath)), 0755) _ = os.MkdirAll(filepath.Dir(targetPath), 0755)
// Create file with mode contained in header at target path // Create file with mode contained in header at target path
dstFile, err := os.OpenFile(targetPath, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)) dstFile, err := os.OpenFile(targetPath, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil { log.Fatal().Err(err).Msg("Error creating file during unarchiving") } if err != nil { log.Fatal().Err(err).Msg("Error creating file during unarchiving") }

View File

@ -130,7 +130,7 @@ func RecvFiles(senderAddr string) {
response, err = http.Get(serverAddr + "/index") response, err = http.Get(serverAddr + "/index")
// If no error, set index failed to false // If no error, set index failed to false
if err == nil { indexGetFailed = false } if err == nil { indexGetFailed = false }
// Wait 500s // Wait 500ms
time.Sleep(500*time.Millisecond) time.Sleep(500*time.Millisecond)
} }
} }

View File

@ -49,12 +49,14 @@ func main() {
// Create --send-to flag to send to a specific IP // Create --send-to flag to send to a specific IP
sendTo := flag.String("send-to", "", "Use IP address of receiver instead of mDNS") sendTo := flag.String("send-to", "", "Use IP address of receiver instead of mDNS")
// Create --dest-dir flag to save to a specified folder
destDir := flag.String("dest-dir", homeDir + "/Downloads", "Destination directory for files or dirs sent over opensend")
// Create --skip-mdns to skip service registration
skipMdns := flag.Bool("skip-mdns", false, "Skip zeroconf service registration (use if mdns fails)")
// Create -t flag for type // Create -t flag for type
actionType := flag.String("t", "","Type of data being sent") actionType := flag.String("t", "","Type of data being sent")
// Create -d flag for data // Create -d flag for data
actionData := flag.String("d", "", "Data to send") actionData := flag.String("d", "", "Data to send")
// Create --skip-mdns to skip service registration
skipMdns := flag.Bool("skip-mdns", false, "Skip zeroconf service registration (use if mdns fails)")
// Create -s flag for sending // Create -s flag for sending
sendFlag := flag.Bool("s", false, "Send data") sendFlag := flag.Bool("s", false, "Send data")
// Create -r flag for receiving // Create -r flag for receiving
@ -174,7 +176,7 @@ func main() {
// Notify user that action is being executed // Notify user that action is being executed
log.Info().Msg("Executing JSON action") log.Info().Msg("Executing JSON action")
// Execute JSON action using files within opensend directory // Execute JSON action using files within opensend directory
config.ExecuteAction(opensendDir) config.ExecuteAction(opensendDir, *destDir)
} else { } else {
flag.Usage() flag.Usage()
log.Fatal().Msg("You must choose sender or receiver mode using -s or -r") log.Fatal().Msg("You must choose sender or receiver mode using -s or -r")