You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.1 KiB
Go

package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "update-dns"
app.Usage = "update dnsimple with public ip address"
app.Action = action
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "url",
Value: "https://whatismyipv6.buddy.wtf/json",
Usage: "url to use to get public ip address",
},
}
app.Run(os.Args)
}
func action(context *cli.Context) error {
result, err := getIP(context.String("url"))
if err != nil {
return errors.Wrap(err, "could not get IP address")
}
fmt.Println(result.IP)
return nil
}
type whatIsMyIPResult struct {
IP string `json:ip`
Version string `json:version`
}
func getIP(url string) (*whatIsMyIPResult, error) {
result, err := http.Get(url)
if err != nil {
return nil, errors.Wrap(err, "could not get url "+url)
}
defer result.Body.Close()
ipResult := &whatIsMyIPResult{}
if err := json.NewDecoder(result.Body).Decode(ipResult); err != nil {
return nil, errors.Wrap(err, "could parse json from url "+url)
}
return ipResult, nil
}