summaryrefslogtreecommitdiffstats
path: root/cmd/bssid.go
blob: 116c63a3c26185f7a8314f5aee89359dcca508e5 (plain)
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
package cmd

import (
	"git.db.org.ai/dborg/internal/formatter"
	"git.db.org.ai/dborg/internal/models"
	"github.com/spf13/cobra"
)

var bssidCmd = &cobra.Command{
	Use:     "bssid [bssid]",
	Aliases: []string{"bs"},
	Short:   "Lookup WiFi access point location by BSSID",
	Long:    `Lookup geographic location of a WiFi access point by its BSSID (MAC address) using Apple's location services`,
	Args:    argsOrStdin(1),
	RunE:    runBSSIDLookup,
}

func init() {
	rootCmd.AddCommand(bssidCmd)
	bssidCmd.Flags().BoolP("all", "a", false, "Show all related results instead of exact match only")
	bssidCmd.Flags().BoolP("google", "g", false, "Include Google Maps URL for the location")
	bssidCmd.Flags().BoolP("osm", "o", false, "Include OpenStreetMap URL for the location")
}

func runBSSIDLookup(cmd *cobra.Command, args []string) error {
	c, err := newUnauthenticatedClient()
	if err != nil {
		return err
	}

	all, _ := cmd.Flags().GetBool("all")
	google, _ := cmd.Flags().GetBool("google")
	osm, _ := cmd.Flags().GetBool("osm")

	return forEachQuery(args, func(bssid string) error {
		params := &models.BSSIDParams{
			BSSID:  bssid,
			All:    all,
			Google: google,
			OSM:    osm,
		}
		response, err := c.LookupBSSID(params)
		if err != nil {
			return err
		}
		return formatter.FormatBSSIDResults(*response, IsJSONOutput())
	})
}