commit f2777007cb973a23e3dce4b7a1762758aa6cb5cf
parent 641b9a869c5b636ba66a69fa47f20abf30c2e129
Author: phoebos <ben@bvnf.space>
Date: Fri, 16 Apr 2021 01:51:22 +0000
read credentials from file
Diffstat:
3 files changed, 41 insertions(+), 2 deletions(-)
diff --git a/TODO b/TODO
@@ -1,4 +1,4 @@
* Only get locations of opted-in users
* Anonymize users who wish
* Create webpage (interactive map?)
-* Read credentials from file?
+* Write man page
diff --git a/credentials.json b/credentials.json
@@ -0,0 +1,7 @@
+{
+ "k": "IPstackApikey",
+ "mboxa": "mapboxApikey",
+ "mboxp": 5,
+ "mboxs": "mapboxStyle",
+ "mboxu": "mapboxUname"
+}
diff --git a/where.go b/where.go
@@ -29,7 +29,7 @@ import (
)
func usage() {
- fmt.Fprintf(os.Stderr, "usage: %s\t[-h] [-k]\n\t\t[-mboxu -mboxa -mboxs] [-mboxp]\n", os.Args[0])
+ fmt.Fprintf(os.Stderr, "usage: %s\t[-h] [-p] [-k]\n\t\t[-c | -mboxu -mboxa -mboxs [-mboxp]]\n", os.Args[0])
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nwhere finds users who have opted in by creating a \".here\" file in their home directory,\nfinds their approximate location from their IP address, and creates a map of the locations of those users.\n")
}
@@ -37,6 +37,7 @@ func usage() {
func main() {
apiKey := flag.String("k", "", "API key for ipstack")
usePretendWhoips := flag.Bool("p", false, "use a cached output of who --ips")
+ useCredFile := flag.String("c", "", "read credentials from a json file (keys are command-line flags)")
var mboxDetails MapboxDetails
flag.StringVar(&mboxDetails.Uname, "mboxu", "", "mapbox.com username")
flag.StringVar(&mboxDetails.Apikey, "mboxa", "", "mapbox.com API key")
@@ -61,6 +62,37 @@ func main() {
}
}
+ // if necessary, get the credentials from a file
+ // (overrides credentials specified with flags)
+ if *useCredFile != "" {
+ bytes, err := read(*useCredFile)
+ if err != nil {
+ fmt.Printf("error reading cred file: %s\n", err)
+ os.Exit(1)
+ }
+ fmt.Printf("%s\n", bytes)
+ var creds struct {
+ K string
+ Mboxa string
+ Mboxp int
+ Mboxs string
+ Mboxu string
+ }
+ err = json.Unmarshal(bytes, &creds)
+ if err != nil {
+ fmt.Printf("error unmarshalling creds: %s\n", err)
+ os.Exit(1)
+ }
+ // set global variables to our credentials
+ *apiKey = creds.K
+ mboxDetails = MapboxDetails{
+ Apikey: creds.Mboxa,
+ Padding: creds.Mboxp,
+ Style: creds.Mboxs,
+ Uname: creds.Mboxu,
+ }
+ }
+
// extract data from the who output
lines := parseLines(ips)
responseChan := make(chan MarkResponse)