Skip to content

Commit

Permalink
This is where it all begins...
Browse files Browse the repository at this point in the history
  • Loading branch information
shopnilsazal committed Oct 3, 2018
0 parents commit f65262b
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.DS_Store
.vscode
22 changes: 22 additions & 0 deletions .goreleaser.yml
@@ -0,0 +1,22 @@
# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
builds:
- env:
- CGO_ENABLED=0
archive:
replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 Rafiqul Hasan

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.
5 changes: 5 additions & 0 deletions README.md
@@ -0,0 +1,5 @@
# checkr

### A cli tool to check whether a package name is available on different package managers.

![Screen Recording](https://raw.githubusercontent.com/shopnilsazal/checkr/master/recording.gif)
63 changes: 63 additions & 0 deletions cmd/checkr/helpers.go
@@ -0,0 +1,63 @@
package main

import (
"fmt"
"net/http"
"runtime"
)

// PackageResponse Type
type PackageResponse struct {
packageManager string
statusCode int
}

// Get status code from package manager
func getPackageStatus(name, url string, ch chan<- PackageResponse) {
resp, err := http.Get(url)
if err != nil {
ch <- PackageResponse{name, 500}
return
}
ch <- PackageResponse{name, resp.StatusCode}
resp.Body.Close()
}

// Get full url of the package manager based on name
func getFullURL(url, manager, name string) string {
fullURL := url + name
switch manager {
case "pip":
fullURL = fullURL + "/json"
case "gem":
fullURL = fullURL + ".json"
}
return fullURL
}

// Get success and error symbols
func getSymbols() (successSym, errorSym string) {
if runtime.GOOS == "windows" {
successSym = "\u001b[32m" + "√" + "\u001b[39m"
errorSym = "\u001b[31m" + "×" + "\u001b[39m"
} else {
successSym = "\u001b[32m" + "✔" + "\u001b[39m"
errorSym = "\u001b[31m" + "✖" + "\u001b[39m"
}
return
}

// Get the message to print on console
func getMessage(resp PackageResponse, name string) string {
var msg string
ss, es := getSymbols()
switch resp.statusCode {
case 404:
msg = fmt.Sprintf("%s %s is available on '%s' \n", ss, name, resp.packageManager)
case 200:
msg = fmt.Sprintf("%s %s is unavailable on '%s' \n", es, name, resp.packageManager)
default:
msg = fmt.Sprintf("%s oops! something bad happened in '%s' \n", es, resp.packageManager)
}
return msg
}
87 changes: 87 additions & 0 deletions cmd/checkr/main.go
@@ -0,0 +1,87 @@
package main

import (
"flag"
"fmt"
"net/http"
"strings"

"github.com/shopnilsazal/checkr/spinner"
)

var urls = map[string]string{
"npm": "https://registry.npmjs.org/",
"pip": "https://pypi.org/pypi/",
"crates": "https://crates.io/api/v1/crates/",
"gem": "https://rubygems.org/api/v1/gems/",
"hex": "https://hex.pm/api/packages/",
}

func main() {

name := flag.String("n", "react", "Provide a package name you want to search.")
managers := flag.String("m", "npm", "Provide package managers where you want to search.")
flag.Parse()

spin := spinner.New("%s Loading...")

ch := make(chan PackageResponse)
_, errorSym := getSymbols()

allManagers := strings.Split(*managers, ",")
if len(allManagers) == 1 {
manager := allManagers[0]
if manager == "all" {
spin.Start()
defer spin.Stop()

for key, url := range urls {
fullURL := getFullURL(url, key, *name)
go getPackageStatus(key, fullURL, ch)
}

for range urls {
res := <-ch
spin.Stop()
fmt.Printf(getMessage(res, *name))
spin.Start()
}

} else if u, ok := urls[manager]; ok {
spin.Start()
fullURL := getFullURL(u, manager, *name)
resp, err := http.Get(fullURL)
if err != nil {
fmt.Printf("%s oops! something bad happened in '%s' \n", errorSym, manager)
}
spin.Stop()
defer resp.Body.Close()
fmt.Printf(getMessage(PackageResponse{manager, resp.StatusCode}, *name))

} else {
fmt.Printf("%s '%s' is not a valid package manager.\n", errorSym, manager)
}

} else {
var validManagers []string
spin.Start()
defer spin.Stop()

for _, m := range allManagers {
if u, ok := urls[m]; ok {
fullURL := getFullURL(u, m, *name)
go getPackageStatus(m, fullURL, ch)
validManagers = append(validManagers, m)
} else {
fmt.Printf("%s '%s' is not a valid package manager.\n", errorSym, m)
}
}

for range validManagers {
res := <-ch
spin.Stop()
fmt.Printf(getMessage(res, *name))
spin.Start()
}
}
}
Binary file added recording.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions spinner/spinner.go
@@ -0,0 +1,65 @@
package spinner

import (
"fmt"
"sync/atomic"
"time"
)

// ClearLine go to the beggining of the line and clear it
const ClearLine = "\r\033[K"

// Spin type
var Spin = `⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏`

// Spinner main type
type Spinner struct {
frames []rune
pos int
active uint64
text string
}

// New Spinner with args
func New(text string) *Spinner {
s := &Spinner{
text: ClearLine + text,
}
s.Set(Spin)
return s
}

// Set frames to the given string which must not use spaces.
func (s *Spinner) Set(frames string) {
s.frames = []rune(frames)
}

// Start shows the spinner.
func (s *Spinner) Start() *Spinner {
if atomic.LoadUint64(&s.active) > 0 {
return s
}
atomic.StoreUint64(&s.active, 1)
go func() {
for atomic.LoadUint64(&s.active) > 0 {
fmt.Printf(s.text, s.next())
time.Sleep(100 * time.Millisecond)
}
}()
return s
}

// Stop hides the spinner.
func (s *Spinner) Stop() bool {
if x := atomic.SwapUint64(&s.active, 0); x > 0 {
fmt.Printf(ClearLine)
return true
}
return false
}

func (s *Spinner) next() string {
r := s.frames[s.pos%len(s.frames)]
s.pos++
return string(r)
}

0 comments on commit f65262b

Please sign in to comment.