Skip to content

Commit

Permalink
Compute the edit distance and complement the URL
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Nov 24, 2017
1 parent af5c1da commit 533f171
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
29 changes: 23 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"math"
neturl "net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/agext/levenshtein"
)

type Config struct {
Expand Down Expand Up @@ -83,15 +85,14 @@ func (cfg *Config) LoadFile(file string) error {
}

func (cfg *Config) GetEnv(url string) (env Env, err error) {
u, err := neturl.Parse(url)
if err != nil {
return
}
u1, _ := neturl.Parse(url)
for _, service := range cfg.Services {
if strings.Contains(service.URL, u.Host) {
u2, _ := neturl.Parse(service.URL)
if u1.Host == u2.Host {
return service.Env, nil
}
}
err = fmt.Errorf("%s: no such host in config file", u1.Host)
return
}

Expand Down Expand Up @@ -121,3 +122,19 @@ func (cfg *Config) Edit() error {
cmd.Stdin = os.Stdin
return cmd.Run()
}

func (cfg *Config) SimilarURLs(url string) (urls []string) {
u1, _ := neturl.Parse(url)
for _, service := range cfg.Services {
u2, _ := neturl.Parse(service.URL)
degree := round(levenshtein.Similarity(u1.Host, u2.Host, nil) * 100)
if degree > 50 {
urls = append(urls, service.URL)
}
}
return
}

func round(f float64) float64 {
return math.Floor(f + .5)
}
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func main() {
}

func run(args []string) int {
var url string
if len(args) > 0 {
switch args[0] {
case "-h", "--help":
Expand All @@ -63,11 +64,16 @@ func run(args []string) int {
}
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
return 1
default:
url = args[0]
}

env, err := cfg.GetEnv(args[0])
env, err := cfg.GetEnv(url)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
for _, url := range cfg.SimilarURLs(url) {
}
fmt.Fprintf(os.Stderr, " similar urls found %q\n")
return 1
}
if credentials == "" {
Expand Down

0 comments on commit 533f171

Please sign in to comment.