Skip to content

Commit 35c83b9

Browse files
authored
Merge pull request #4 from bengarrett/dev
V1.2
2 parents aa30def + 695b854 commit 35c83b9

32 files changed

+882
-759
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*.dll
55
*.so
66
*.dylib
7-
myip
87

98
# Test binary, built with `go test -c`
109
*.test
@@ -17,4 +16,6 @@ dist/
1716
vendor/
1817

1918
# Operating system specific
20-
.DS_Store
19+
.DS_Store
20+
21+
cmd/tray

.golangci.yml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
linters:
44
enable-all: true
55
disable:
6-
- interfacer # Deprecated
7-
- scopelint # Deprecated
8-
- golint # Deprecated
9-
- maligned # Deprecated
6+
- interfacer # Deprecated
7+
- scopelint # Deprecated
8+
- golint # Deprecated
9+
- maligned # Deprecated
1010

11-
- exhaustivestruct # 'It is not recommended to use it for all files in a project'
12-
- forbidigo # forbids the use for fmt.Print(f|ln)
13-
- nlreturn # opinionated
14-
- paralleltest # causes tests to randomly fail
15-
- testpackage # removes the ability to test internal functions
16-
- wrapcheck # wrapping 3rd party errors such as those from bolt are unnecessary
17-
- wsl # whitespace linter, slow, aggressive and opinionated
11+
- exhaustivestruct # 'It is not recommended to use it for all files in a project'
12+
- forbidigo # forbids the use for fmt.Print(f|ln)
13+
- nlreturn # opinionated
14+
- paralleltest # causes tests to randomly fail
15+
# - testpackage # removes the ability to test internal functions
16+
- varnamelen # doesn't allow short var names for flags
17+
- wrapcheck # wrapping 3rd party errors such as those from bolt are unnecessary
18+
- wsl # whitespace linter, slow, aggressive and opinionated
1819

1920
linters-settings:
2021
cyclop:
2122
# the maximal code complexity to report
2223
max-complexity: 15
23-
2424
# Fixes
2525
# gofumpt -l -w <file>
26-
# gci -w <file>
26+
# gci -w <file>

.goreleaser.yml

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,66 +8,75 @@ before:
88
hooks:
99
- go mod tidy
1010
builds:
11-
- id: FreeBSD
12-
goos: ['freebsd']
13-
goarch: ['amd64']
14-
- id: Linux Intel
15-
goos: ['linux']
16-
goarch: ['amd64']
17-
- id: Linux arm
18-
goos: ['linux']
19-
goarch: ['arm']
20-
- id: macOS
21-
goos: ['darwin']
22-
goarch: ['amd64', 'arm64']
23-
- id: Windows
24-
goos: ['windows']
25-
goarch: ['amd64']
11+
- main: ./cmd/myip/
12+
id: FreeBSD
13+
goos: ["freebsd"]
14+
goarch: ["amd64"]
15+
16+
- main: ./cmd/myip/
17+
id: Linux Intel
18+
goos: ["linux"]
19+
goarch: ["amd64"]
20+
21+
- main: ./cmd/myip/
22+
id: Linux arm
23+
goos: ["linux"]
24+
goarch: ["arm", "arm64"]
25+
26+
- main: ./cmd/myip/
27+
id: macOS
28+
goos: ["darwin"]
29+
goarch: ["amd64", "arm64"]
30+
31+
- main: ./cmd/myip/
32+
id: Windows
33+
goos: ["windows"]
34+
goarch: ["amd64"]
2635

2736
archives:
2837
- id: FreeBSD
29-
builds: ['FreeBSD']
38+
builds: ["FreeBSD"]
3039
format: tar.gz
3140
files:
32-
- none*
41+
- none*
3342
name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}"
3443
replacements:
3544
amd64: Intel
3645
freebsd: FreeBSD
3746
- id: Linux Intel
38-
builds: ['Linux Intel']
47+
builds: ["Linux Intel"]
3948
format: tar.gz
4049
files:
41-
- none*
50+
- none*
4251
name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}"
4352
replacements:
4453
amd64: Intel
4554
linux: Linux
4655
- id: Linux arm
47-
builds: ['Linux arm']
56+
builds: ["Linux arm"]
4857
format: tar.gz
4958
files:
50-
- none*
59+
- none*
5160
name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}"
5261
replacements:
5362
arm: arm32_
5463
arm64: arm64
5564
linux: Linux
5665
- id: macOS
57-
builds: ['macOS']
66+
builds: ["macOS"]
5867
format: tar.gz
5968
files:
60-
- none*
69+
- none*
6170
name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}"
6271
replacements:
6372
arm64: M-series
6473
amd64: Intel
6574
darwin: macOS
6675
- id: Windows
67-
builds: ['Windows']
76+
builds: ["Windows"]
6877
format: zip
6978
files:
70-
- none*
79+
- none*
7180
name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}"
7281
replacements:
7382
amd64: Intel
@@ -80,7 +89,7 @@ universal_binaries:
8089

8190
nfpms:
8291
- id: Linux
83-
builds: ['Linux Intel']
92+
builds: ["Linux Intel"]
8493
file_name_template: "{{ .ProjectName }}"
8594
replacements:
8695
amd64: Intel

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ It is an excellent tool for quickly determining if your machine or network is co
1818
### Packages
1919

2020
##### macOS [Homebrew](https://brew.sh/)
21+
2122
```sh
2223
brew install bengarrett/myip/myip
2324
```
2425

2526
##### Windows [Scoop](https://scoop.sh/)
27+
2628
```sh
2729
scoop bucket add myip https://github.com/bengarrett/myip.git
2830
scoop install myip
@@ -95,8 +97,8 @@ myip -timeout=900
9597
# clone this repo
9698
git clone git@github.com:bengarrett/myip.git
9799

98-
# access the repo
99-
cd myip
100+
# access the main.go
101+
cd myip/cmd/myip
100102

101103
# target and build the app for the host system
102104
go build

cmd/myip/main.go

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
// Package main determine your Internet-facing
2+
// IP address and location from multiple sources.
3+
// © Ben Garrett https://github.com/bengarrett/myip
4+
package main
5+
6+
import (
7+
"flag"
8+
"fmt"
9+
"os"
10+
"text/tabwriter"
11+
12+
"github.com/bengarrett/myip/pkg/ipv4"
13+
"github.com/bengarrett/myip/pkg/ipv6"
14+
"github.com/bengarrett/myip/pkg/ping"
15+
)
16+
17+
type modes struct {
18+
first bool
19+
ipv6 bool
20+
raw bool
21+
timeout int64
22+
}
23+
24+
const (
25+
// Default HTTP request timeout value in milliseconds.
26+
httpTimeout = 5000
27+
// Tabwriter padding using spaces.
28+
padding = 4
29+
)
30+
31+
var (
32+
version = "0.0.0"
33+
commit = "unset" // nolint: gochecknoglobals
34+
date = "unset" // nolint: gochecknoglobals
35+
)
36+
37+
func main() {
38+
msInSec := func(i int) int {
39+
const second = 1000
40+
return i / second
41+
}
42+
var mode modes
43+
flag.BoolVar(&mode.first, "first", false, "returns the first reported IP address and its location")
44+
flag.BoolVar(&mode.ipv6, "ipv6", false, "return an IPv6 address instead of IPv4")
45+
flag.BoolVar(&mode.raw, "simple", false, "simple mode only displays the IP address")
46+
flag.Int64Var(&mode.timeout, "timeout", httpTimeout,
47+
fmt.Sprintf("https request timeout in milliseconds (default: %d [%d seconds])", httpTimeout, msInSec(httpTimeout)))
48+
ver := flag.Bool("version", false, "version and information for this program")
49+
f := flag.Bool("f", false, "alias for first")
50+
i := flag.Bool("i", false, "alias for ipv6")
51+
s := flag.Bool("s", false, "alias for simple")
52+
t := flag.Int64("t", 0, "alias for timeout")
53+
v := flag.Bool("v", false, "alias for version")
54+
55+
flag.Usage = func() {
56+
const alias = 1
57+
fmt.Fprintln(os.Stderr, "MyIP Usage:")
58+
fmt.Fprintln(os.Stderr, " myip [options]:")
59+
fmt.Fprintln(os.Stderr, "")
60+
w := tabwriter.NewWriter(os.Stderr, 0, 0, padding, ' ', 0)
61+
fmt.Fprintln(w, " -h, --help\tshow this list of options")
62+
flag.VisitAll(func(f *flag.Flag) {
63+
if len(f.Name) == alias {
64+
return
65+
}
66+
fmt.Fprintf(w, " -%v, --%v\t%v\n", f.Name[:1], f.Name, f.Usage)
67+
})
68+
w.Flush()
69+
}
70+
flag.Parse()
71+
72+
// version information
73+
if *ver || *v {
74+
info()
75+
return
76+
}
77+
// aliases
78+
if *i {
79+
mode.ipv6 = true
80+
}
81+
if *s {
82+
mode.raw = true
83+
}
84+
if *t > 0 {
85+
mode.timeout = *t
86+
}
87+
if *f {
88+
mode.first = true
89+
}
90+
switch mode.ipv6 {
91+
case true:
92+
mode.parseIPv6()
93+
case false:
94+
mode.parseIPv4()
95+
}
96+
}
97+
98+
func (m modes) parseIPv4() {
99+
switch {
100+
case m.first && m.raw:
101+
ip := ipv4.One(m.timeout)
102+
fmt.Println(ip)
103+
return
104+
case m.first:
105+
fmt.Print(ping.Zero1)
106+
ip := ipv4.One(m.timeout)
107+
s := ping.Sprint(ip)
108+
fmt.Println(s)
109+
return
110+
case m.raw:
111+
ipv4.All(m.timeout, true)
112+
fmt.Println()
113+
return
114+
default:
115+
fmt.Print(ping.Zero)
116+
ipv4.All(m.timeout, false)
117+
fmt.Println()
118+
}
119+
}
120+
121+
func (m modes) parseIPv6() {
122+
switch {
123+
case m.first && m.raw:
124+
ip := ipv6.One(m.timeout)
125+
fmt.Println(ip)
126+
return
127+
case m.first:
128+
fmt.Print(ping.Zero1)
129+
ip := ipv6.One(m.timeout)
130+
s := ping.Sprint(ip)
131+
fmt.Println(s)
132+
return
133+
case m.raw:
134+
ipv6.All(m.timeout, true)
135+
fmt.Println()
136+
return
137+
default:
138+
fmt.Print(ping.Zero)
139+
ipv6.All(m.timeout, false)
140+
fmt.Println()
141+
}
142+
}
143+
144+
func self() (string, error) {
145+
exe, err := os.Executable()
146+
if err != nil {
147+
return "", fmt.Errorf("self error: %w", err)
148+
}
149+
return exe, nil
150+
}
151+
152+
// Info prints out the program information and version.
153+
func info() {
154+
const copyright = "\u00A9"
155+
fmt.Printf("MyIP Tetrad v%s\n%s 2021-22 Ben Garrett\n", version, copyright)
156+
fmt.Printf("https://github.com/bengarrett/myip\n\n")
157+
fmt.Printf("build: %s (%s)\n", commit, date)
158+
exe, err := self()
159+
if err != nil {
160+
fmt.Printf("path: %s\n", err)
161+
return
162+
}
163+
fmt.Printf("path: %s\n", exe)
164+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ require github.com/oschwald/maxminddb-golang v1.8.0
66

77
require (
88
github.com/davecgh/go-spew v1.1.1 // indirect
9-
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
9+
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
1010
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
99
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
1010
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1111
golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
12-
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b h1:1VkfZQv42XQlA/jchYumAnv1UPo6RgF9rJFkTgZIxO4=
13-
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12+
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1ii9sTLUeBQwQQfUHtrs=
13+
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1414
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1515
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
1616
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)