Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.

Commit 38fc88a

Browse files
committed
update ping
1 parent 98b4c00 commit 38fc88a

178 files changed

Lines changed: 8717 additions & 3301 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/codeql-analysis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ jobs:
4848
# If you wish to specify custom queries, you can do so here or in a config file.
4949
# By default, queries listed here will override any specified in a config file.
5050
# Prefix the list here with "+" to use these queries and those in the config file.
51-
51+
5252
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
5353
# queries: security-extended,security-and-quality
5454

55-
55+
5656
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
5757
# If this step fails, then you should remove it and run the build manually (see below)
5858
- name: Autobuild
@@ -61,7 +61,7 @@ jobs:
6161
# ℹ️ Command-line programs to run using the OS shell.
6262
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
6363

64-
# If the Autobuild fails above, remove it and uncomment the following three lines.
64+
# If the Autobuild fails above, remove it and uncomment the following three lines.
6565
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
6666

6767
# - run: |

.github/workflows/release.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ jobs:
2222
- name: Install Go
2323
uses: actions/setup-go@v3
2424
with:
25-
go-version: 1.18
25+
go-version: 1.24
2626

2727
- name: Check config
2828
uses: goreleaser/goreleaser-action@v3
2929
with:
30-
version: v1.13.1
30+
version: v2.8.2
3131
# version: latest
3232
args: check
3333

3434
- name: Run GoReleaser
3535
uses: goreleaser/goreleaser-action@v3
3636
with:
37-
version: v1.13.1
37+
version: v2.8.2
3838
# version: latest
3939
args: release --rm-dist
4040
env:
@@ -59,7 +59,7 @@ jobs:
5959
- name: Install Go
6060
uses: actions/setup-go@v3
6161
with:
62-
go-version: 1.18
62+
go-version: 1.24
6363

6464
- name: Install Docker Buildx
6565
uses: docker/setup-buildx-action@v2

.github/workflows/test.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
fail-fast: false
3333
matrix:
3434
goVersion:
35-
- 18
35+
- 24
3636
os:
3737
- macos
3838
- ubuntu
@@ -57,7 +57,7 @@ jobs:
5757
uses: golangci/golangci-lint-action@v3
5858
with:
5959
args: --timeout=5m
60-
version: v1.48.0
60+
version: v2.0.2
6161

6262
go-test:
6363
needs:
@@ -66,7 +66,7 @@ jobs:
6666
fail-fast: false
6767
matrix:
6868
goVersion:
69-
- 18
69+
- 24
7070
os:
7171
- macos
7272
- ubuntu

.golangci.yaml

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,27 @@
1+
# https://github.com/golangci/golangci-lint/blob/main/.golangci.reference.yml
2+
version: "2"
3+
14
run:
2-
go: "1.18"
5+
go: "1.24"
36
timeout: 3m
47

5-
linters-settings:
6-
cyclop:
7-
max-complexity: 30
8-
package-average: 10.0
9-
10-
errcheck:
11-
check-type-assertions: true
12-
13-
funlen:
14-
lines: 200
15-
statements: 100
168

17-
gocognit:
18-
min-complexity: 50
19-
20-
gosec:
21-
excludes:
22-
- G204 # Subprocess launched with a potential tainted input or cmd arguments # Use os/exec
23-
- G401 # Use of weak cryptographic primitive # md5, sha1
24-
- G501 # Blocklisted import crypto/md5: weak cryptographic primitive
25-
- G505 # Import blocklist: crypto/sha1
26-
27-
lll:
28-
line-length: 200
299

3010
linters:
31-
disable-all: true
11+
disable: # temporarily
12+
- errcheck
13+
- goconst
14+
- gosec
15+
- revive
16+
- govet
17+
- staticcheck
3218
enable:
3319
## enabled by default
34-
35-
- deadcode # Finds unused code
36-
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
37-
- gosimple # Linter for Go source code that specializes in simplifying a code
20+
# - errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
3821
- govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
3922
- ineffassign # Detects when assignments to existing variables are not used
40-
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
41-
- structcheck # Finds unused struct fields
42-
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code
23+
# - staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
4324
- unused # Checks Go code for unused constants, variables, functions and types
44-
- varcheck # Finds unused global variables and constants
4525

4626
## disabled by default
4727

@@ -55,24 +35,21 @@ linters:
5535
- durationcheck # check for two durations multiplied together
5636
- errname # Checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error.
5737
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.
58-
- execinquery # execinquery is a linter about query string checker in Query function which reads your Go src files and warning it finds
5938
- exhaustive # check exhaustiveness of enum switch statements
60-
- exportloopref # checks for pointers to enclosing loop variables
6139
# - forbidigo # Forbids identifiers
6240
- funlen # Tool for detection of long functions
6341
# - gochecknoglobals # check that no global variables exist
6442
# - gochecknoinits # Checks that no init functions are present in Go code
6543
- gocognit # Computes and checks the cognitive complexity of functions
66-
- goconst # Finds repeated strings that could be replaced by a constant
44+
# - goconst # Finds repeated strings that could be replaced by a constant
6745
- gocritic # Provides diagnostics that check for bugs, performance and style issues.
6846
- gocyclo # Computes and checks the cyclomatic complexity of functions
6947
- godot # Check if comments end in a period
70-
- goimports # In addition to fixing imports, goimports also formats your code in the same style as gofmt.
7148
# - gomnd # An analyzer to detect magic numbers.
7249
- gomoddirectives # Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.
7350
- gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations.
7451
- goprintffuncname # Checks that printf-like functions are named with f at the end
75-
- gosec # Inspects source code for security problems
52+
# - gosec # Inspects source code for security problems
7653
- lll # Reports long lines
7754
- makezero # Finds slice declarations with non-zero initial length
7855
- nakedret # Finds naked returns in functions greater than a specified function length
@@ -85,11 +62,9 @@ linters:
8562
- nosprintfhostport # Checks for misuse of Sprintf to construct a host with port in a URL.
8663
- predeclared # find code that shadows one of Go's predeclared identifiers
8764
- promlinter # Check Prometheus metrics naming via promlint
88-
- revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint.
65+
# - revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint.
8966
- rowserrcheck # checks whether Err of rows is checked successfully
9067
- sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed.
91-
- stylecheck # Stylecheck is a replacement for golint
92-
- tenv # tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17
9368
- testpackage # linter that makes you use a separate _test package
9469
- tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes
9570
- unconvert # Remove unnecessary type conversions
@@ -134,6 +109,30 @@ linters:
134109
#- interfacer # [deprecated] Linter that suggests narrower interface types
135110
#- maligned # [deprecated, replaced by govet fieldalignment] Tool to detect Go structs that would take less memory if their fields were sorted
136111
#- scopelint # [deprecated, replaced by exportloopref] Scopelint checks for unpinned variables in go programs
112+
settings:
113+
cyclop:
114+
max-complexity: 30
115+
package-average: 10.0
116+
117+
errcheck:
118+
check-type-assertions: true
119+
120+
funlen:
121+
lines: 200
122+
statements: 100
123+
124+
gocognit:
125+
min-complexity: 50
126+
127+
gosec:
128+
excludes:
129+
- G204 # Subprocess launched with a potential tainted input or cmd arguments # Use os/exec
130+
- G401 # Use of weak cryptographic primitive # md5, sha1
131+
- G501 # Blocklisted import crypto/md5: weak cryptographic primitive
132+
- G505 # Import blocklist: crypto/sha1
133+
134+
lll:
135+
line-length: 200
137136

138137
issues:
139138
max-same-issues: 10

.goreleaser.yaml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# This is an example .goreleaser.yml file with some sensible defaults.
22
# Make sure to check the documentation at https://goreleaser.com
3+
# https://github.com/goreleaser/goreleaser/blob/main/.goreleaser.yaml
4+
5+
version: 2
36
env:
47
- GO111MODULE=on
58
- CGO_ENABLED=0
@@ -42,12 +45,11 @@ archives:
4245
wrap_in_directory: true
4346

4447
changelog:
45-
skip: true
46-
# sort: asc
47-
# filters:
48-
# exclude:
49-
# - "^docs:"
50-
# - "^test:"
48+
sort: asc
49+
filters:
50+
exclude:
51+
- "^docs:"
52+
- "^test:"
5153

5254
nfpms:
5355
- id: packages
@@ -90,6 +92,7 @@ nfpms:
9092
brews:
9193
- name: "{{ .ProjectName }}"
9294
homepage: https://github.com/linzeyan/homebrew-tools
93-
tap:
95+
license: Apache-2.0
96+
- repository:
9497
owner: linzeyan
9598
name: homebrew-tools

cmd/ping.go

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ import (
2222
"fmt"
2323
"math"
2424
"net"
25+
"net/netip"
2526
"os"
2627
"os/signal"
28+
"strconv"
29+
"strings"
2730
"time"
2831

2932
"github.com/linzeyan/ops-cli/cmd/common"
@@ -232,9 +235,17 @@ func (p *Ping) Connect(c context.Context, host string) {
232235
if p.IPv6 {
233236
network = "ip6"
234237
}
238+
if network == "ip4" {
239+
addr, err := ParseAnyIPv4Netip(host)
240+
if err != nil {
241+
logger.Error(err.Error())
242+
return
243+
}
244+
host = addr.String()
245+
}
235246
addr, err := net.ResolveIPAddr(network, host)
236247
if err != nil {
237-
logger.Debug(err.Error())
248+
logger.Error(err.Error())
238249
return
239250
}
240251
if p.IPv6 {
@@ -334,3 +345,82 @@ func (p *Ping) summary(host string, t time.Duration) {
334345
p.stat.Min, avg, p.stat.Max, time.Duration(math.Sqrt(variance)))
335346
printer.Printf(out)
336347
}
348+
349+
func ParseAnyIPv4Netip(input string) (netip.Addr, error) {
350+
parts := strings.Split(input, ".")
351+
switch len(parts) {
352+
case 1:
353+
val, err := strconv.ParseUint(parts[0], 10, 32)
354+
if err != nil {
355+
return netip.Addr{}, err
356+
}
357+
ip := netip.AddrFrom4([4]byte{
358+
byte(val >> 24),
359+
byte(val >> 16),
360+
byte(val >> 8),
361+
byte(val),
362+
})
363+
364+
return ip, nil
365+
case 2:
366+
a, err := strconv.Atoi(parts[0])
367+
if err != nil {
368+
return netip.Addr{}, err
369+
}
370+
b, err := strconv.Atoi(parts[1])
371+
if err != nil {
372+
return netip.Addr{}, err
373+
}
374+
ip := netip.AddrFrom4([4]byte{
375+
byte(a),
376+
byte(b >> 16),
377+
byte((b >> 8) & 0xFF),
378+
byte(b & 0xFF),
379+
})
380+
return ip, nil
381+
case 3:
382+
a, err := strconv.Atoi(parts[0])
383+
if err != nil {
384+
return netip.Addr{}, err
385+
}
386+
b, err := strconv.Atoi(parts[1])
387+
if err != nil {
388+
return netip.Addr{}, err
389+
}
390+
c, err := strconv.Atoi(parts[2])
391+
if err != nil {
392+
return netip.Addr{}, err
393+
}
394+
ip := netip.AddrFrom4([4]byte{
395+
byte(a),
396+
byte(b),
397+
byte(c >> 8),
398+
byte(c & 0xFF),
399+
})
400+
return ip, nil
401+
case 4:
402+
ip, err := netip.ParseAddr(input)
403+
if err != nil || !ip.Is4() {
404+
return netip.Addr{}, errors.New("invalid IPv4 address")
405+
}
406+
return ip, nil
407+
default:
408+
return netip.Addr{}, errors.New("invalid format")
409+
}
410+
}
411+
412+
func IPv4NetipToAllForms(ip netip.Addr) (string, string, string, string) {
413+
if !ip.Is4() {
414+
panic("not an IPv4 address")
415+
}
416+
b := ip.As4()
417+
418+
f3 := uint32(b[2])<<8 | uint32(b[3])
419+
f2 := uint32(b[1])<<16 | f3
420+
f1 := uint32(b[0])<<24 | f2
421+
form2 := fmt.Sprintf("%d.%d", b[0], f2)
422+
form3 := fmt.Sprintf("%d.%d.%d", b[0], b[1], f3)
423+
form4 := ip.String()
424+
425+
return strconv.FormatUint(uint64(f1), 10), form2, form3, form4
426+
}

cmd/tree.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,19 @@ func initTree() *cobra.Command {
141141
inf.Print(opts)
142142
if !flags.noReport {
143143
footer := "\n%d "
144-
switch {
144+
switch nd {
145145
default:
146146
footer += "directories"
147-
case nd == 1:
147+
case 1:
148148
footer += "directory"
149149
}
150150

151151
footer = fmt.Sprintf(footer, nd)
152152
if !flags.dirs {
153-
switch {
153+
switch nf {
154154
default:
155155
footer += ", %d files\n"
156-
case nf == 1:
156+
case 1:
157157
footer += ", %d file\n"
158158
}
159159
footer = fmt.Sprintf(footer, nf)

0 commit comments

Comments
 (0)