diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 6433d9d..9e041cb 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -57,22 +57,23 @@ func main() { func runReshCli(out *output.Output, config cfg.Config) (string, int) { args := opt.HandleVersionOpts(out, os.Args, version, commit) + const missing = "" flags := pflag.NewFlagSet("", pflag.ExitOnError) - sessionID := flags.String("session-id", "", "Resh generated session ID") - pwd := flags.String("pwd", "", "$PWD - present working directory") - gitOriginRemote := flags.String("git-origin-remote", "<<>>", "> git origin remote") + sessionID := flags.String("session-id", missing, "Resh generated session ID") + pwd := flags.String("pwd", missing, "$PWD - present working directory") + gitOriginRemote := flags.String("git-remote", missing, "> git remote get-url origin") query := flags.String("query", "", "Search query") flags.Parse(args) // TODO: These errors should tell the user that they should not be running the command directly errMsg := "Failed to get required command-line arguments" - if *sessionID == "" { + if *sessionID == missing { out.FatalE(errMsg, errors.New("missing required option --session-id")) } - if *pwd == "" { + if *pwd == missing { out.FatalE(errMsg, errors.New("missing required option --pwd")) } - if *gitOriginRemote == "<<>>" { + if *gitOriginRemote == missing { out.FatalE(errMsg, errors.New("missing required option --git-origin-remote")) } dataDir, err := datadir.GetPath() @@ -249,7 +250,7 @@ func (m manager) UpdateData(input string) { "recordCount", len(m.s.cliRecords), "itemCount", len(m.s.data), ) - query := searchapp.NewQueryFromString(input, m.host, m.pwd, m.gitOriginRemote, m.config.Debug) + query := searchapp.NewQueryFromString(sugar, input, m.host, m.pwd, m.gitOriginRemote, m.config.Debug) var data []searchapp.Item itemSet := make(map[string]int) m.s.lock.Lock() diff --git a/internal/normalize/normailze.go b/internal/normalize/normailze.go new file mode 100644 index 0000000..d6ed45d --- /dev/null +++ b/internal/normalize/normailze.go @@ -0,0 +1,28 @@ +package normalize + +import ( + "net/url" + "strings" + + giturls "github.com/whilp/git-urls" + "go.uber.org/zap" +) + +// GitRemote helper +// Returns normalized git remote - valid even on error +func GitRemote(sugar *zap.SugaredLogger, gitRemote string) string { + gitRemote = strings.TrimSuffix(gitRemote, ".git") + parsedURL, err := giturls.Parse(gitRemote) + if err != nil { + sugar.Errorw("Failed to parse git remote", zap.Error(err), + "gitRemote", gitRemote, + ) + return gitRemote + } + if parsedURL.User == nil || parsedURL.User.Username() == "" { + parsedURL.User = url.User("git") + } + // TODO: figure out what scheme we want + parsedURL.Scheme = "git+ssh" + return parsedURL.String() +} diff --git a/internal/normalize/normalize_test.go b/internal/normalize/normalize_test.go new file mode 100644 index 0000000..847928d --- /dev/null +++ b/internal/normalize/normalize_test.go @@ -0,0 +1,46 @@ +package normalize_test + +import ( + "testing" + + "github.com/curusarn/resh/internal/normalize" + "go.uber.org/zap" +) + +// TestLeftCutPadString +func TestGitRemote(t *testing.T) { + sugar := zap.NewNop().Sugar() + + data := [][]string{ + { + "git@github.com:curusarn/resh.git", // git + "git@github.com:curusarn/resh", // git no ".git" + "http://github.com/curusarn/resh.git", // http + "https://github.com/curusarn/resh.git", // https + "ssh://git@github.com/curusarn/resh.git", // ssh + "git+ssh://git@github.com/curusarn/resh.git", // git+ssh + }, + { + "git@host.example.com:org/user/repo.git", // git + "git@host.example.com:org/user/repo", // git no ".git" + "http://host.example.com/org/user/repo.git", // http + "https://host.example.com/org/user/repo.git", // https + "ssh://git@host.example.com/org/user/repo.git", // ssh + "git+ssh://git@host.example.com/org/user/repo.git", // git+ssh + }, + } + + for _, arr := range data { + n := len(arr) + for i := 0; i < n-1; i++ { + for j := i + 1; j < n; j++ { + one := normalize.GitRemote(sugar, arr[i]) + two := normalize.GitRemote(sugar, arr[j]) + if one != two { + t.Fatalf("Normalized git remotes should match for '%s' and '%s'\n -> got: '%s' != '%s'", + arr[i], arr[j], one, two) + } + } + } + } +} diff --git a/internal/recordint/searchapp.go b/internal/recordint/searchapp.go index da85e28..5ccc537 100644 --- a/internal/recordint/searchapp.go +++ b/internal/recordint/searchapp.go @@ -1,12 +1,10 @@ package recordint import ( - "net/url" "strconv" - "strings" + "github.com/curusarn/resh/internal/normalize" "github.com/curusarn/resh/record" - giturls "github.com/whilp/git-urls" "go.uber.org/zap" ) @@ -50,28 +48,9 @@ func NewSearchApp(sugar *zap.SugaredLogger, r *record.V1) SearchApp { Host: r.Device, Pwd: r.Pwd, Home: r.Home, - // TODO: is this the right place to normalize the git remote - GitOriginRemote: normalizeGitRemote(sugar, r.GitOriginRemote), + // TODO: is this the right place to normalize the git remote? + GitOriginRemote: normalize.GitRemote(sugar, r.GitOriginRemote), ExitCode: r.ExitCode, Time: time, } } - -// TODO: maybe move this to a more appropriate place -// normalizeGitRemote helper -func normalizeGitRemote(sugar *zap.SugaredLogger, gitRemote string) string { - gitRemote = strings.TrimSuffix(gitRemote, ".git") - parsedURL, err := giturls.Parse(gitRemote) - if err != nil { - sugar.Errorw("Failed to parse git remote", zap.Error(err), - "gitRemote", gitRemote, - ) - return gitRemote - } - if parsedURL.User == nil || parsedURL.User.Username() == "" { - parsedURL.User = url.User("git") - } - // TODO: figure out what scheme we want - parsedURL.Scheme = "git+ssh" - return parsedURL.String() -} diff --git a/internal/recutil/recutil.go b/internal/recutil/recutil.go index 53b1cff..1862741 100644 --- a/internal/recutil/recutil.go +++ b/internal/recutil/recutil.go @@ -26,11 +26,11 @@ import ( // } // TODO: maybe more to a more appropriate place -// TODO: cleanup the interface - stop modifying the part1 and returning a ew record at the same time +// TODO: cleanup the interface - stop modifying the part1 and returning a new record at the same time // Merge two records (part1 - collect + part2 - postcollect) func Merge(r1 *recordint.Collect, r2 *recordint.Collect) (record.V1, error) { if r1.SessionID != r2.SessionID { - return record.V1{}, errors.New("Records to merge are not from the same sesion - r1:" + r1.SessionID + " r2:" + r2.SessionID) + return record.V1{}, errors.New("Records to merge are not from the same session - r1:" + r1.SessionID + " r2:" + r2.SessionID) } if r1.Rec.RecordID != r2.Rec.RecordID { return record.V1{}, errors.New("Records to merge do not have the same ID - r1:" + r1.Rec.RecordID + " r2:" + r2.Rec.RecordID) diff --git a/internal/searchapp/query.go b/internal/searchapp/query.go index fc2870a..507af74 100644 --- a/internal/searchapp/query.go +++ b/internal/searchapp/query.go @@ -3,6 +3,9 @@ package searchapp import ( "sort" "strings" + + "github.com/curusarn/resh/internal/normalize" + "go.uber.org/zap" ) // Query holds information that is used for result scoring @@ -35,7 +38,7 @@ func filterTerms(terms []string) []string { } // NewQueryFromString . -func NewQueryFromString(queryInput string, host string, pwd string, gitOriginRemote string, debug bool) Query { +func NewQueryFromString(sugar *zap.SugaredLogger, queryInput string, host string, pwd string, gitOriginRemote string, debug bool) Query { terms := strings.Fields(queryInput) var logStr string for _, term := range terms { @@ -51,7 +54,7 @@ func NewQueryFromString(queryInput string, host string, pwd string, gitOriginRem terms: terms, host: host, pwd: pwd, - gitOriginRemote: gitOriginRemote, + gitOriginRemote: normalize.GitRemote(sugar, gitOriginRemote), } } diff --git a/scripts/hooks.sh b/scripts/hooks.sh index 0b5dfd8..c71468f 100644 --- a/scripts/hooks.sh +++ b/scripts/hooks.sh @@ -125,7 +125,7 @@ __resh_widget_control_R() { return $? fi BUFFER=$(resh-cli -requireVersion "$__RESH_VERSION" \ - --git-origin-remote "$git_remote" \ + --git-remote "$git_remote" \ --pwd "$PWD" \ --query "$BUFFER" \ --session-id "$__RESH_SESSION_ID" \