Skip to content

Commit

Permalink
Merge pull request #154 from askgitdev/go-mod-fn
Browse files Browse the repository at this point in the history
moving and renaming some packages 😬 - quite a big diff
  • Loading branch information
patrickdevivo authored Aug 12, 2021
2 parents f21a89a + 9ef9ebd commit f776ddf
Show file tree
Hide file tree
Showing 93 changed files with 552 additions and 312 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ SELECT yaml_to_json('hello: world')
-- +------------------------------+
```

##### `go_mod_to_json`

Scalar function that parses a `go.mod` file and returns a JSON representation of it.

```SQL
SELECT go_mod_to_json('<contents-of-go.mod-file>')
```

##### `str_split`

Helper for splitting strings on some separator.
Expand Down
19 changes: 10 additions & 9 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package cmd
import (
"os"

"github.com/askgitdev/askgit/extensions"
"github.com/askgitdev/askgit/extensions/options"
"github.com/askgitdev/askgit/pkg/locator"
"github.com/askgitdev/askgit/tables"
"go.riyazali.net/sqlite"

// bring in sqlite 🙌
Expand All @@ -14,14 +15,14 @@ import (

func registerExt() {
sqlite.Register(
tables.RegisterFn(
tables.WithExtraFunctions(),
tables.WithRepoLocator(locator.CachedLocator(locator.MultiLocator())),
tables.WithContextValue("defaultRepoPath", repo),
tables.WithGitHub(),
tables.WithContextValue("githubToken", githubToken),
tables.WithContextValue("githubPerPage", os.Getenv("GITHUB_PER_PAGE")),
tables.WithContextValue("githubRateLimit", os.Getenv("GITHUB_RATE_LIMIT")),
extensions.RegisterFn(
options.WithExtraFunctions(),
options.WithRepoLocator(locator.CachedLocator(locator.MultiLocator())),
options.WithContextValue("defaultRepoPath", repo),
options.WithGitHub(),
options.WithContextValue("githubToken", githubToken),
options.WithContextValue("githubPerPage", os.Getenv("GITHUB_PER_PAGE")),
options.WithContextValue("githubRateLimit", os.Getenv("GITHUB_RATE_LIMIT")),
),
)
}
53 changes: 53 additions & 0 deletions extensions/extensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Package extensions provide implementation of the various underlying sqlite3 virtual tables [https://www.sqlite.org/vtab.html] and user defined functions
// that askgit uses under-the-hood. This module can be side-effect-imported in other modules to include the functionality
// of the sqlite3 extensions there.
package extensions

import (
"github.com/askgitdev/askgit/extensions/internal/enry"
"github.com/askgitdev/askgit/extensions/internal/git"
"github.com/askgitdev/askgit/extensions/internal/github"
"github.com/askgitdev/askgit/extensions/internal/golang"
"github.com/askgitdev/askgit/extensions/internal/helpers"
"github.com/askgitdev/askgit/extensions/options"
"go.riyazali.net/sqlite"
)

func RegisterFn(fns ...options.OptionFn) func(ext *sqlite.ExtensionApi) (_ sqlite.ErrorCode, err error) {
var opt = &options.Options{}
for _, fn := range fns {
fn(opt)
}

// return an extension function that register modules with sqlite when this package is loaded
return func(ext *sqlite.ExtensionApi) (_ sqlite.ErrorCode, err error) {
// register the git tables
if sqliteErr, err := git.Register(ext, opt); err != nil {
return sqliteErr, err
}

// only conditionally register the utility functions
if opt.ExtraFunctions {
if sqliteErr, err := helpers.Register(ext, opt); err != nil {
return sqliteErr, err
}

if sqliteErr, err := enry.Register(ext, opt); err != nil {
return sqliteErr, err
}

if sqliteErr, err := golang.Register(ext, opt); err != nil {
return sqliteErr, err
}
}

// conditionally register the GitHub functionality
if opt.GitHub {
if sqliteErr, err := github.Register(ext, opt); err != nil {
return sqliteErr, err
}
}

return sqlite.SQLITE_OK, nil
}
}
30 changes: 30 additions & 0 deletions extensions/internal/enry/enry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package enry

import (
"github.com/askgitdev/askgit/extensions/options"
"github.com/pkg/errors"
"go.riyazali.net/sqlite"
)

// Register registers enry related functionality as a SQLite extension
func Register(ext *sqlite.ExtensionApi, _ *options.Options) (_ sqlite.ErrorCode, err error) {
var fns = map[string]sqlite.Function{
"enry_detect_language": &EnryDetectLanguage{},
"enry_is_binary": &EnryIsBinary{},
"enry_is_configuration": &EnryIsConfiguration{},
"enry_is_documentation": &EnryIsDocumentation{},
"enry_is_dot_file": &EnryIsDotFile{},
"enry_is_generated": &EnryIsGenerated{},
"enry_is_image": &EnryIsImage{},
"enry_is_test": &EnryIsTest{},
"enry_is_vendor": &EnryIsVendor{},
}

for name, fn := range fns {
if err = ext.CreateFunction(name, fn); err != nil {
return sqlite.SQLITE_ERROR, errors.Wrapf(err, "failed to register %q function", name)
}
}

return sqlite.SQLITE_OK, nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package funcs
package enry

import (
"github.com/go-enry/go-enry/v2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package funcs
package enry

import (
"io/ioutil"
"testing"

"github.com/askgitdev/askgit/tables/internal/tools"
"github.com/askgitdev/askgit/extensions/internal/tools"
)

func TestEnryDetectLanguage(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package funcs
package enry

import (
"github.com/go-enry/go-enry/v2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package funcs
package enry

import (
"io/ioutil"
"testing"

"github.com/askgitdev/askgit/tables/internal/tools"
"github.com/askgitdev/askgit/extensions/internal/tools"
)

func TestEnryIsBinary(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package funcs
package enry

import (
"github.com/go-enry/go-enry/v2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package funcs
package enry

import (
"testing"

"github.com/askgitdev/askgit/tables/internal/tools"
"github.com/askgitdev/askgit/extensions/internal/tools"
)

func TestEnryIsConfiguration(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package funcs
package enry

import (
"github.com/go-enry/go-enry/v2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package funcs
package enry

import (
"testing"

"github.com/askgitdev/askgit/tables/internal/tools"
"github.com/askgitdev/askgit/extensions/internal/tools"
)

func TestEnryIsDocumentation(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package funcs
package enry

import (
"github.com/go-enry/go-enry/v2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package funcs
package enry

import (
"testing"

"github.com/askgitdev/askgit/tables/internal/tools"
"github.com/askgitdev/askgit/extensions/internal/tools"
)

func TestEnryIsDotFile(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package funcs
package enry

import (
"github.com/go-enry/go-enry/v2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package funcs
package enry

import (
"testing"

"github.com/askgitdev/askgit/tables/internal/tools"
"github.com/askgitdev/askgit/extensions/internal/tools"
)

func TestEnryIsGenerated(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package funcs
package enry

import (
"github.com/go-enry/go-enry/v2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package funcs
package enry

import (
"testing"

"github.com/askgitdev/askgit/tables/internal/tools"
"github.com/askgitdev/askgit/extensions/internal/tools"
)

func TestEnryIsImage(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package funcs
package enry

import (
"github.com/go-enry/go-enry/v2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package funcs
package enry

import (
"testing"

"github.com/askgitdev/askgit/tables/internal/tools"
"github.com/askgitdev/askgit/extensions/internal/tools"
)

func TestEnryIsTest(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package funcs
package enry

import (
"github.com/go-enry/go-enry/v2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package funcs
package enry

import (
"testing"

"github.com/askgitdev/askgit/tables/internal/tools"
"github.com/askgitdev/askgit/extensions/internal/tools"
)

func TestEnryIsVendor(t *testing.T) {
Expand Down
30 changes: 30 additions & 0 deletions extensions/internal/enry/enry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package enry

import (
"database/sql"
"log"
"os"
"testing"

_ "github.com/askgitdev/askgit/pkg/sqlite"
"go.riyazali.net/sqlite"
)

// FixtureDatabase represents the database connection to run the test against
var FixtureDatabase *sql.DB

func init() {
// register sqlite extension when this package is loaded
sqlite.Register(func(ext *sqlite.ExtensionApi) (_ sqlite.ErrorCode, err error) {
return Register(ext, nil)
})
}

func TestMain(m *testing.M) {
var err error
if FixtureDatabase, err = sql.Open("sqlite3", "file:testing.db?mode=memory"); err != nil {
log.Fatalf("failed to open database connection: %v", err)
}

os.Exit(m.Run())
}
File renamed without changes.
File renamed without changes.
38 changes: 38 additions & 0 deletions extensions/internal/git/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package git

import (
"github.com/askgitdev/askgit/extensions/internal/git/native"
"github.com/askgitdev/askgit/extensions/options"
"github.com/pkg/errors"
"go.riyazali.net/sqlite"
)

// Register registers git related functionality as a SQLite extension
func Register(ext *sqlite.ExtensionApi, opt *options.Options) (_ sqlite.ErrorCode, err error) {
// register virtual table modules
var modules = map[string]sqlite.Module{
"commits": &LogModule{Locator: opt.Locator, Context: opt.Context},
"refs": &RefModule{Locator: opt.Locator, Context: opt.Context},
"stats": native.NewStatsModule(opt.Locator, opt.Context),
"files": native.NewFilesModule(opt.Locator, opt.Context),
"blame": native.NewBlameModule(opt.Locator, opt.Context),
}

for name, mod := range modules {
if err = ext.CreateModule(name, mod); err != nil {
return sqlite.SQLITE_ERROR, errors.Wrapf(err, "failed to register %q module", name)
}
}

var fns = map[string]sqlite.Function{
"commit_from_tag": &CommitFromTagFn{},
}

for name, fn := range fns {
if err = ext.CreateFunction(name, fn); err != nil {
return sqlite.SQLITE_ERROR, errors.Wrapf(err, "failed to register %q function", name)
}
}

return sqlite.SQLITE_OK, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ package git_test

import (
"database/sql"
"os"
"testing"

"github.com/askgitdev/askgit/extensions"
"github.com/askgitdev/askgit/extensions/options"
"github.com/askgitdev/askgit/pkg/locator"
_ "github.com/askgitdev/askgit/pkg/sqlite"
"github.com/askgitdev/askgit/tables"
_ "github.com/mattn/go-sqlite3"
"go.riyazali.net/sqlite"
"os"
"testing"
)

func init() {
// register sqlite extension when this package is loaded
sqlite.Register(tables.RegisterFn(
tables.WithExtraFunctions(), tables.WithRepoLocator(locator.CachedLocator(locator.MultiLocator())),
sqlite.Register(extensions.RegisterFn(
options.WithExtraFunctions(), options.WithRepoLocator(locator.CachedLocator(locator.MultiLocator())),
))
}

Expand Down
Loading

0 comments on commit f776ddf

Please sign in to comment.