From e16e9a7aa9f4be01bf82093e4e2f50a1dd311530 Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Mon, 22 Dec 2014 00:19:29 +0100 Subject: [PATCH] Cleanup --- .gitignore | 8 +++----- Makefile | 12 +++++------- README.md | 5 +---- Vagrantfile | 1 + ifconfig.go | 14 +++++++------- ifconfig_test.go | 6 +++--- 6 files changed, 20 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 9649cad..0bc48c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ -ifconfig -*.lock -*.pid -*.log -cmd.js +bin/ +pkg/ +src/ \ No newline at end of file diff --git a/Makefile b/Makefile index 29cfb1e..69ad5ac 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,13 @@ -TARGET = ifconfig +NAME = ifconfig -all: install - -clean: - rm -f -- $(TARGET) +all: test build fmt: gofmt -w=true *.go -install: - go build $(TARGET).go +build: + @mkdir bin + go build -o bin/$(NAME) test: go test diff --git a/README.md b/README.md index 3b9a847..ef5da4e 100644 --- a/README.md +++ b/README.md @@ -29,13 +29,10 @@ Features * Open source * Fast * Supports typical CLI tools (curl, wget and fetch) +* JSON output (optional) Why? ==== * To scratch an itch * An excuse to use Go for something * Faster than ifconfig.me and has IPv6 support - -Code style -========== - $ gofmt -tabs=false -tabwidth=4 diff --git a/Vagrantfile b/Vagrantfile index f5b3bc1..8b00872 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -3,6 +3,7 @@ Vagrant.configure("2") do |config| config.vm.box = "chef/ubuntu-14.04" + config.vm.network :forwarded_port, guest: 8080, host: 8080 config.vm.synced_folder ".", "/vagrant" config.vm.synced_folder "salt/roots/", "/srv/salt/" config.vm.provider :virtualbox do |vb| diff --git a/ifconfig.go b/ifconfig.go index 949d99f..17bccfe 100644 --- a/ifconfig.go +++ b/ifconfig.go @@ -12,16 +12,16 @@ import ( "strings" ) +var agentExp = regexp.MustCompile("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$") + type Client struct { IP net.IP JSON string Header http.Header } -func isCli(userAgent string) bool { - match, _ := regexp.MatchString("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$", - userAgent) - return match +func isCLI(userAgent string) bool { + return agentExp.MatchString(userAgent) } func parseRealIP(req *http.Request) net.IP { @@ -40,7 +40,7 @@ func pathToKey(path string) string { return re.ReplaceAllLiteralString(strings.ToLower(path), "") } -func isJson(req *http.Request) bool { +func isJSON(req *http.Request) bool { return strings.HasSuffix(req.URL.Path, ".json") || strings.Contains(req.Header.Get("Accept"), "application/json") } @@ -54,7 +54,7 @@ func handler(w http.ResponseWriter, req *http.Request) { ip := parseRealIP(req) header := pathToKey(req.URL.Path) - if isJson(req) { + if isJSON(req) { if header == "all" { b, _ := json.MarshalIndent(req.Header, "", " ") io.WriteString(w, fmt.Sprintf("%s\n", b)) @@ -65,7 +65,7 @@ func handler(w http.ResponseWriter, req *http.Request) { b, _ := json.MarshalIndent(m, "", " ") io.WriteString(w, fmt.Sprintf("%s\n", b)) } - } else if isCli(req.UserAgent()) { + } else if isCLI(req.UserAgent()) { if header == "" || header == "ip" { io.WriteString(w, fmt.Sprintf("%s\n", ip)) } else { diff --git a/ifconfig_test.go b/ifconfig_test.go index 2423fd5..18dc14e 100644 --- a/ifconfig_test.go +++ b/ifconfig_test.go @@ -2,12 +2,12 @@ package main import "testing" -func TestIsCLi(t *testing.T) { +func TestIsCLI(t *testing.T) { userAgents := []string{"curl/7.26.0", "Wget/1.13.4 (linux-gnu)", "fetch libfetch/2.0"} for _, userAgent := range userAgents { - if !isCli(userAgent) { + if !isCLI(userAgent) { t.Errorf("Expected true for %s", userAgent) } } @@ -15,7 +15,7 @@ func TestIsCLi(t *testing.T) { browserUserAgent := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.28 " + "Safari/537.36" - if isCli(browserUserAgent) { + if isCLI(browserUserAgent) { t.Errorf("Expected false for %s", browserUserAgent) } }