From aa4e8907d9f4079e367453438685db845e4599ff Mon Sep 17 00:00:00 2001 From: jolheiser Date: Tue, 11 Oct 2022 21:22:39 -0500 Subject: [PATCH] initial commit --- .goreleaser.yaml | 25 ++++++++++++ .woodpecker/goreleaser.yml | 39 ++++++++++++++++++ LICENSE | 19 +++++++++ README.md | 7 ++++ go.mod | 11 +++++ go.sum | 11 +++++ main.go | 84 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 196 insertions(+) create mode 100644 .goreleaser.yaml create mode 100644 .woodpecker/goreleaser.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..4d8a0c9 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,25 @@ +builds: + - env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + ldflags: + - "-s -w -X main.Version={{.Version}}" +archives: + - replacements: + 386: i386 + amd64: x86_64 + format_overrides: + - goos: windows + format: zip +checksum: + name_template: 'checksums.txt' +release: + gitea: + owner: jolheiser + name: isitup +gitea_urls: + api: https://git.jojodev.com/api/v1/ + download: https://git.jojodev.com diff --git a/.woodpecker/goreleaser.yml b/.woodpecker/goreleaser.yml new file mode 100644 index 0000000..8f82434 --- /dev/null +++ b/.woodpecker/goreleaser.yml @@ -0,0 +1,39 @@ +clone: + git: + image: woodpeckerci/plugin-git + settings: + tags: true + +pipeline: + compliance: + image: golang:1.18 + commands: + - go test -race ./... + - go vet ./... + - go run github.com/rs/zerolog/cmd/lint@latest go.jolheiser.com/isitup + when: + event: pull_request + + build: + image: goreleaser/goreleaser + commands: + - goreleaser build --snapshot + when: + event: pull_request + + release: + image: goreleaser/goreleaser + commands: + - goreleaser release + secrets: [ gitea_token ] + when: + event: tag + + prune: + image: jolheiser/drone-gitea-prune + settings: + base: https://git.jojodev.com + token: + from_secret: gitea_token + when: + event: tag diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ec2045e --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2022 John Olheiser + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..636f48d --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# isitup + +A CLI that interacts with the [isitup.org API](https://isitup.org). + +## License + +[MIT](LICENSE) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c29b95a --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module go.jolheiser.com/isitup + +go 1.19 + +require github.com/fatih/color v1.13.0 + +require ( + github.com/mattn/go-colorable v0.1.9 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect + golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6ab7ee4 --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= +github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= +github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U= +github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go new file mode 100644 index 0000000..00d8b72 --- /dev/null +++ b/main.go @@ -0,0 +1,84 @@ +package main + +import ( + "encoding/json" + "fmt" + "net/http" + "net/url" + "os" + "strings" + + "github.com/fatih/color" +) + +var Version = "develop" + +func main() { + if len(os.Args) < 2 { + fmt.Println("isitup requires a host to check") + return + } + host := os.Args[1] + + r, err := Check(host) + if err != nil { + fmt.Println(err) + return + } + + var status string + switch r.StatusCode { + case 1: + status = color.GreenString("up") + case 2: + status = color.RedString("down") + case 3: + status = color.BlackString("an invalid domain") + default: + fmt.Printf("isitup returned an invalid status %d\n", r.StatusCode) + return + } + + fmt.Printf("%s is %s!\n", color.YellowString(host), status) +} + +// Response is a response from the API +type Response struct { + Domain string `json:"domain"` + Port int `json:"port"` + StatusCode int `json:"status_code"` + ResponseIP string `json:"response_ip"` + ResponseCode int `json:"response_code"` + ResponseTime float64 `json:"response_time"` +} + +// Check checks a given host +func Check(host string) (*Response, error) { + u, err := url.Parse(host) + if err != nil { + return nil, fmt.Errorf("could not parse host %q: %w", host, err) + } + + var h string + switch { + case u.Scheme != "": + h = u.Hostname() + case u.Path != "": + p := strings.TrimPrefix(u.Path, "/") + h = strings.Split(p, "/")[0] + default: + return nil, fmt.Errorf("could not derive host from %q", host) + } + + res, err := http.Get(fmt.Sprintf("https://isitup.org/%s.json", h)) + if err != nil { + return nil, fmt.Errorf("could not get from API: %w", err) + } + + var r Response + if err := json.NewDecoder(res.Body).Decode(&r); err != nil { + return nil, fmt.Errorf("could not decode response body: %w", err) + } + + return &r, nil +}