Skip to content

Commit 6ded948

Browse files
authored
Merge pull request #104 from planetscale/joem/cli-lookup
feat: add cloudranger cli
2 parents b71409f + a35d23f commit 6ded948

File tree

5 files changed

+95
-47
lines changed

5 files changed

+95
-47
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@ on:
1010
- reopened
1111
- synchronize
1212

13-
permissions:
14-
contents: write
15-
packages: write
16-
1713
jobs:
1814
test:
1915
strategy:
2016
matrix:
21-
go-version: ['1.20', '1.21', stable]
17+
go-version: ['1.21', '1.22', stable]
2218
runs-on: ubuntu-latest
2319
steps:
2420
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
@@ -32,45 +28,3 @@ jobs:
3228
- run: make lint
3329
- run: make test
3430
- run: make bench
35-
36-
# only create a new tag/release on main builds:
37-
release:
38-
if: github.ref == 'refs/heads/main'
39-
needs: [test]
40-
runs-on: ubuntu-latest
41-
steps:
42-
- name: checkout code with full history (unshallow)
43-
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
44-
with:
45-
fetch-depth: 0
46-
fetch-tags: true
47-
48-
# only generate a new release if certain files change:
49-
- uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3
50-
id: filter
51-
with:
52-
filters: |
53-
app:
54-
- '**.go'
55-
- 'go.mod'
56-
- 'go.sum'
57-
58-
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5
59-
if: steps.filter.outputs.app == 'true'
60-
with:
61-
go-version-file: go.mod
62-
63-
- name: install autotag binary
64-
if: steps.filter.outputs.app == 'true'
65-
run: |
66-
curl -sL https://git.io/autotag-install | sudo sh -s -- -b /usr/local/bin
67-
68-
- name: increment tag and create release
69-
if: steps.filter.outputs.app == 'true'
70-
run: |
71-
set -eou pipefail
72-
73-
new_version=$(autotag -vn)
74-
gh release create v"${new_version}" --target main --title "v${new_version}" --generate-notes
75-
env:
76-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/release.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: create release assets
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
# limit when we create a release by the files changed:
8+
paths:
9+
- go.mod
10+
- go.sum
11+
- '**.go'
12+
workflow_dispatch:
13+
14+
jobs:
15+
release:
16+
runs-on: ubuntu-latest
17+
18+
permissions:
19+
contents: write
20+
packages: write
21+
22+
steps:
23+
- name: checkout code with full history (unshallow)
24+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
25+
with:
26+
fetch-depth: 0
27+
fetch-tags: true
28+
29+
- name: install autotag binary
30+
run: |
31+
curl -sL https://git.io/autotag-install | sh -s -- -b "${RUNNER_TEMP}/bin"
32+
33+
- name: increment tag and create release
34+
run: |
35+
set -eou pipefail
36+
37+
new_version=$(autotag -vn)
38+
gh release create v"${new_version}" --target main --title "v${new_version}" --generate-notes
39+
env:
40+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Attic/
22
HACK.md
3+
cloudranger

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ func main() {
3232
}
3333
```
3434

35+
A small cli is included in [cmd/cloudranger](cmd/cloudranger). It is EXPERIMENTAL and its behavior, flags, and output is likely to change.
36+
37+
```sh
38+
$ go run cmd/cloudranger/main.go 3.5.140.101
39+
40+
{"cloud":"AWS","region":"ap-northeast-2"}
41+
```
42+
3543
## Testing and Benchmarks
3644

3745
```sh

cmd/cloudranger/main.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
8+
"github.com/planetscale/cloudranger"
9+
)
10+
11+
type out struct {
12+
Cloud string `json:"cloud"`
13+
Region string `json:"region"`
14+
}
15+
16+
func main() {
17+
if len(os.Args) < 2 {
18+
usage()
19+
os.Exit(1)
20+
}
21+
22+
ranger := cloudranger.New()
23+
24+
ip := os.Args[1]
25+
26+
info, found := ranger.GetIP(ip)
27+
if !found {
28+
fmt.Fprintf(os.Stderr, "IP not in the database: %q\n", ip)
29+
os.Exit(0)
30+
}
31+
32+
o := out{
33+
Cloud: info.Cloud(),
34+
Region: info.Region(),
35+
}
36+
37+
if err := json.NewEncoder(os.Stdout).Encode(o); err != nil {
38+
fmt.Fprintf(os.Stderr, "error: %v\n", err)
39+
os.Exit(1)
40+
}
41+
}
42+
43+
func usage() {
44+
fmt.Println("Usage: cloudranger <ip>")
45+
}

0 commit comments

Comments
 (0)