summaryrefslogtreecommitdiffstats
path: root/cmd/geo.go
blob: 6c7caf925b0a032b7882eca82165d9fc9420bf93 (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
package cmd

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

var geoCmd = &cobra.Command{
	Use:   "geo",
	Short: "Search for address information",
	Long:  `Returns address information including residents, property details, and demographics (costs 1 credit)`,
	RunE:  runGeoSearch,
}

func init() {
	rootCmd.AddCommand(geoCmd)
	geoCmd.Flags().StringP("street", "s", "", "Street address")
	geoCmd.Flags().StringP("city", "c", "", "City")
	geoCmd.Flags().StringP("state", "t", "", "State (2-letter code)")
	geoCmd.Flags().StringP("zip", "z", "", "ZIP code")
	geoCmd.MarkFlagRequired("street")
	geoCmd.MarkFlagRequired("city")
	geoCmd.MarkFlagRequired("state")
	geoCmd.MarkFlagRequired("zip")
}

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

	params := &models.GeoSearchParams{}
	params.Street, _ = cmd.Flags().GetString("street")
	params.City, _ = cmd.Flags().GetString("city")
	params.State, _ = cmd.Flags().GetString("state")
	params.Zip, _ = cmd.Flags().GetString("zip")

	response, err := c.SearchGeo(params)
	if err != nil {
		return err
	}

	return formatter.FormatGeoResults(*response, IsJSONOutput())
}