adding the eclipse .project file
[random_node_code.git] / tftp / node_modules / tftp-client / bin / tftp.js
1 #!/usr/bin/env node\r
2 \r
3 // Require needed modules\r
4 var TFTP = require('..'), // The tftp-client module\r
5         fs = require('fs'), // Simple wrappers around standard POSIX functions\r
6         path = require('path'); // Contains utilities for handling and transforming file paths\r
7 \r
8 /**********************\\r
9 |* Validate arguments *|\r
10 \**********************/\r
11 \r
12 // Too few arguments (node + path_to_this_file + host + cmd + file = 5)\r
13 if (process.argv.length < 5) {\r
14         // Print usage, and exit\r
15         console.log('Needs atleast 3 arguments (you had %d)', process.argv.length-2);\r
16         console.log('');\r
17         printUsage(true);\r
18 }\r
19 \r
20 // Grab arguments\r
21 var host = process.argv[2];\r
22 var cmd  = process.argv[3];\r
23 var file = process.argv[4];\r
24 var port = process.argv[5] || 69; // Port defaults to 69 if omitted\r
25 \r
26 // If cmd is neither read nor write, print usage & exit\r
27 if (['read', 'write'].indexOf(cmd)===-1) {\r
28         console.log('Command must be read or write (you had %s)', cmd);\r
29         printUsage(true);\r
30 }\r
31 \r
32 /*******************\\r
33 |* Validation done *|\r
34 \*******************/\r
35 \r
36 // Create the client\r
37 var client = new TFTP(port, host);\r
38 \r
39 var basename = path.basename(file);\r
40 \r
41 /********\\r
42 |* READ *|\r
43 \********/\r
44 if (cmd == 'read') {\r
45         // Read from server\r
46         client.read(basename, function(err, data) {\r
47                 // If error, output some error message\r
48                 if (err) {\r
49                         console.error('Oh noes! Error while reading file from tftp server:');\r
50                         console.error(err);\r
51                 } else {\r
52 \r
53                         // No error, we got the file, lets write it\r
54                         \r
55                         fs.writeFile(file, data, function (err) {\r
56                                 \r
57                                 // If error, output some error message\r
58                                 if (err) {\r
59                                         console.error('Dang it! Error while writing file!');\r
60                                         console.error(err);\r
61                                 } else {\r
62 \r
63                                         // No error, the file has been written!\r
64 \r
65                                         console.log('File saved (%d bytes)', data.length);\r
66                                 }\r
67                         });\r
68                 }\r
69         });\r
70 }\r
71 /*********\\r
72 |* WRITE *|\r
73 \*********/\r
74 else if (cmd == 'write') {\r
75         // If file does not exist, print error and usage & exit\r
76         if (!fs.existsSync(file)) {\r
77                 console.log('File (%s) does not exist!', file);\r
78                 printUsage(true);\r
79         }\r
80 \r
81         // Read file first, then send to server\r
82         var data = fs.readFileSync(file);\r
83 \r
84         client.write(basename, data, function (err, bytes) {\r
85                 if (err) {\r
86                         console.error('ERROR:');\r
87                         console.error(err);\r
88                         return;\r
89                 }\r
90 \r
91                 console.log('File sent (%d bytes)', bytes);\r
92         });\r
93 } else {\r
94         console.log('???HOW DID YOU GET HERE???');\r
95 }\r
96 \r
97 /**\r
98  * Prints usage - how to use tftp-client\r
99  * @param  {boolean} exit Wether it sould exit the process\r
100  */\r
101 function printUsage(exit) {\r
102         console.log('Usage:');\r
103         console.log('  tftp-client <hostname> (read|write) <filename> [<port>]');\r
104         console.log('');\r
105         console.log('Example:');\r
106         console.log('  tftp-client localhost read 1.txt');\r
107 \r
108         if (exit===true)\r
109                 process.exit(0);\r
110 }