-
Notifications
You must be signed in to change notification settings - Fork 11
/
cli.js
75 lines (59 loc) · 1.67 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env node
const whereami = require('./')
const minimist = require('minimist')
const Loading = require('loading-indicator')
const options = {
boolean: ['help', 'version', 'raw'],
alias: {
h: 'help',
v: 'version',
f: 'format',
r: 'raw'
},
default: {
raw: false,
format: 'sexagesimal'
}
}
const argv = minimist(process.argv.slice(2), options)
const [ipOrHostname] = argv._
const help = `
Usage: whereami [OPTIONS]
Get your geolocation information using freegeoip.net from the CLI
Example:
$ whereami
-23.4733,-46.6658
$ whereami -f human
San Francisco, CA, United States
$ whereami 2.151.255.255 -f human
Stabekk, Akershus, Norway
Options:
-v --version Display current software version
-h --help Display help and usage details
-f --format Output format (either human, json or sexagesimal)
-r --raw Output raw data from freegeoip.net
`
function exitWithSuccess (message) {
process.stdout.write(`${message}\n`)
process.exit(0)
}
function exitWithError (message, code = 1) {
process.stderr.write(`${message}\n`)
process.exit(code)
}
if (argv.help) exitWithSuccess(help)
if (argv.version) exitWithSuccess(require('./package.json').version)
const isValidFormat = ['human', 'json', 'sexagesimal'].includes(argv.format)
if (!isValidFormat) exitWithError(`Format "${argv.format}" is not supported`)
async function run () {
const loading = Loading.start()
try {
const output = await whereami({ ...argv, ipOrHostname })
Loading.stop(loading)
exitWithSuccess(output)
} catch (error) {
Loading.stop(loading)
exitWithError(error.message)
}
}
run()