Skip to content

Commit

Permalink
refactor(domain): use strings.CutPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Feb 3, 2023
1 parent 6c506e6 commit a4ce3a7
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
disable-sudo: true
egress-policy: block
allowed-endpoints: >
api.github.com:443
github.com:443
objects.githubusercontent.com:443
proxy.golang.org:443
Expand All @@ -30,12 +31,12 @@ jobs:
- name: 🏗️ Set up Go
uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
with:
go-version: 1.19
go-version: stable
- name: 🚚 Check out the repository
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
with:
persist-credentials: false
- name: 🧹 Run golangci-lint
uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5 # v3.4.0
with:
version: v1.50
version: latest
12 changes: 9 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ jobs:
disable-sudo: true
egress-policy: block
allowed-endpoints: >
api.github.com:443
codecov.io:443
github.com:443
objects.githubusercontent.com:443
proxy.golang.org:443
storage.googleapis.com:443
sum.golang.org:443
Expand All @@ -33,7 +35,7 @@ jobs:
- name: 🏗️ Set up Go
uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
with:
go-version: 1.19
go-version: stable
cache: true
- name: 🧪 Run `go test`
run: |
Expand All @@ -58,8 +60,10 @@ jobs:
disable-sudo: true
egress-policy: block
allowed-endpoints: >
api.github.com:443
codecov.io:443
github.com:443
objects.githubusercontent.com:443
proxy.golang.org:443
storage.googleapis.com:443
sum.golang.org:443
Expand All @@ -72,7 +76,7 @@ jobs:
- name: 🏗️ Set up Go
uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
with:
go-version: 1.19
go-version: stable
cache: true
- name: 🧪 Run `go test`
run: |
Expand All @@ -88,7 +92,9 @@ jobs:
disable-sudo: true
egress-policy: block
allowed-endpoints: >
api.github.com:443
github.com:443
objects.githubusercontent.com:443
proxy.golang.org:443
sum.golang.org:443
Expand All @@ -99,7 +105,7 @@ jobs:
- name: 🏗️ Set up Go
uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
with:
go-version: 1.19
go-version: stable
cache: true
- name: 🎭 Install GoMock
run: go install github.com/golang/mock/mockgen@v1.6.0
Expand Down
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ linters:
- ireturn # ireturn works poorly for the style with private types and public interfaces
- maintidx # It seems the linter never leads to code changes
- maligned # deprecated, and I value readability over bytes saved by alignment
- musttag # The checking itself is great but it currently complains about structures defined in cloudflare-go. There are no ways for me to fix it.
- nlreturn # I don't agree with the style enforced by nlreturn
- nonamedreturns # named returns are needed in the internal updator package
- nosnakecase # revive's var-naming check seems to be better
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/favonia/cloudflare-ddns

go 1.19
go 1.20

require (
github.com/cloudflare/cloudflare-go v0.60.0
Expand Down
14 changes: 9 additions & 5 deletions internal/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,20 @@ func New(domain string) (Domain, error) {
// Remove the final dot for consistency
normalized = strings.TrimRight(normalized, ".")

switch {
case normalized == "*":
// Special case 1: "*"
if normalized == "*" {
return Wildcard(""), nil
case strings.HasPrefix(normalized, "*."):
}

// Special case 2: "*.something"
if normalized, ok := strings.CutPrefix(normalized, "*."); ok {
// redo the normalization after removing the offending "*" to get the true error (if any)
normalized, err := profileKeepingLeadingDots.ToASCII(strings.TrimPrefix(normalized, "*."))
return Wildcard(normalized), err
default:
return FQDN(normalized), err
}

// otherwise
return FQDN(normalized), err
}

// SortDomains sorts a list of domains according to their ASCII representations.
Expand Down
1 change: 0 additions & 1 deletion internal/domainexp/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func scanDomainList(ppfmt pp.PP, input string, tokens []string) ([]domain.Domain
return domains, tokens
}

//nolint:unparam
func scanConstants(_ppfmt pp.PP, _input string, tokens []string, wanted []string) (string, []string) {
if len(tokens) == 0 {
return "", nil
Expand Down

0 comments on commit a4ce3a7

Please sign in to comment.