-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
256 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
text.dll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
text.dylib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
text.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"owner": "nalgeon", | ||
"name": "example", | ||
"version": "0.1.0", | ||
"homepage": "https://github.com/nalgeon/sqlite-example/blob/main/README.md", | ||
"repository": "https://github.com/nalgeon/sqlite-example", | ||
"specfile": "https://github.com/nalgeon/sqlite-example/raw/main/sqlpkg.json", | ||
"authors": ["Anton Zhiyanov"], | ||
"license": "MIT", | ||
"description": "Example extension.", | ||
"keywords": ["sqlite-example"], | ||
"assets": { | ||
"path": "https://github.com/nalgeon/sqlite-example/releases/download/0.1.0", | ||
"files": { | ||
"darwin-amd64": "example-macos-0.1.0-x86.zip", | ||
"darwin-arm64": "example-macos-0.1.0-arm64.zip", | ||
"linux-amd64": "example-linux-0.1.0-x86.zip", | ||
"windows-amd64": "example-win-0.1.0-x64.zip" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"owner": "sqlite", | ||
"name": "broken", | ||
"assets": { | ||
"path": "broken", | ||
"files": { | ||
"darwin-amd64": "broken.dylib", | ||
"darwin-arm64": "broken.dylib", | ||
"linux-amd64": "broken.so", | ||
"windows-amd64": "broken.dll" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"owner": "sqlite", | ||
"name": "stmt", | ||
"homepage": "https://www.sqlite.org/stmt.html", | ||
"repository": "https://sqlite.org/src/file/ext/misc/", | ||
"authors": ["Richard Hipp"], | ||
"license": "Public Domain", | ||
"description": "Natural string sorting and comparison.", | ||
"symbols": ["sqlite_stmt"], | ||
"assets": { | ||
"path": "https://github.com/nalgeon/sqlean/releases/download/incubator", | ||
"files": { | ||
"darwin-amd64": "stmt.dylib", | ||
"darwin-arm64": "stmt.dylib", | ||
"linux-amd64": "stmt.so", | ||
"windows-amd64": "stmt.dll" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
stmtvtab.dll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
stmtvtab.dylib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
stmtvtab.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"packages": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package which | ||
|
||
import ( | ||
"errors" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
|
||
"sqlpkg.org/cli/cmd" | ||
"sqlpkg.org/cli/fileio" | ||
"sqlpkg.org/cli/logx" | ||
"sqlpkg.org/cli/spec" | ||
) | ||
|
||
const help = "usage: sqlpkg which package" | ||
|
||
// maps the OS name to the file extension | ||
var fileExt = map[string]string{ | ||
"darwin": ".dylib", | ||
"linux": ".so", | ||
"windows": ".dll", | ||
} | ||
|
||
// Which prints a path to the extension file. | ||
func Which(args []string) error { | ||
if len(args) != 1 { | ||
return errors.New(help) | ||
} | ||
|
||
parts := strings.Split(args[0], "/") | ||
if len(parts) != 2 { | ||
return errors.New("invalid package name") | ||
} | ||
|
||
owner, name := parts[0], parts[1] | ||
pkgDir := spec.Dir(cmd.WorkDir, owner, name) | ||
if !fileio.Exists(pkgDir) { | ||
return errors.New("package is not installed") | ||
} | ||
|
||
path := findExact(pkgDir, name, runtime.GOOS) | ||
if path != "" { | ||
logx.Log(path) | ||
return nil | ||
} | ||
|
||
paths := findByExt(pkgDir, name, runtime.GOOS) | ||
if len(paths) == 0 { | ||
return errors.New("extension file is not found") | ||
} | ||
|
||
logx.Log("exact match not found") | ||
logx.Log("possible matches:") | ||
for _, path := range paths { | ||
logx.Log(path) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// findExact returns a path to the extension file | ||
// if the extension file has the same name as the package itself. | ||
func findExact(pkgDir, name, os string) string { | ||
{ | ||
// e.g., text.dylib | ||
pattern := filepath.Join(pkgDir, name+fileExt[os]) | ||
paths, _ := filepath.Glob(pattern) | ||
if len(paths) != 0 { | ||
return paths[0] | ||
} | ||
} | ||
{ | ||
// e.g., text0.dylib | ||
pattern := filepath.Join(pkgDir, name+"[0-9]"+fileExt[os]) | ||
paths, _ := filepath.Glob(pattern) | ||
if len(paths) != 0 { | ||
return paths[0] | ||
} | ||
} | ||
// no exact match | ||
return "" | ||
} | ||
|
||
// findByExt returns paths to files in the package dir | ||
// that have an expected extension (e.g. textext.dylib) | ||
func findByExt(pkgDir, name, os string) []string { | ||
file := "*" + fileExt[os] | ||
pattern := filepath.Join(pkgDir, file) | ||
paths, _ := filepath.Glob(pattern) | ||
return paths | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package which | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"sqlpkg.org/cli/cmd" | ||
) | ||
|
||
func TestExact(t *testing.T) { | ||
cmd.WorkDir = "." | ||
repoDir, lockPath := cmd.SetupTestRepo(t) | ||
cmd.CopyTestRepo(t, "") | ||
mem := cmd.SetupTestLogger() | ||
|
||
args := []string{"nalgeon/example"} | ||
err := Which(args) | ||
if err != nil { | ||
t.Fatalf("which error: %v", err) | ||
} | ||
|
||
mem.Print() | ||
mem.MustHave(t, ".sqlpkg/nalgeon/example/example.dylib") | ||
|
||
cmd.TeardownTestRepo(t, repoDir, lockPath) | ||
} | ||
|
||
func TestPossible(t *testing.T) { | ||
cmd.WorkDir = "." | ||
repoDir, lockPath := cmd.SetupTestRepo(t) | ||
cmd.CopyTestRepo(t, "") | ||
mem := cmd.SetupTestLogger() | ||
|
||
args := []string{"sqlite/stmt"} | ||
err := Which(args) | ||
if err != nil { | ||
t.Fatalf("which error: %v", err) | ||
} | ||
|
||
mem.Print() | ||
mem.MustHave(t, "exact match not found") | ||
mem.MustHave(t, ".sqlpkg/sqlite/stmt/stmtvtab.dylib") | ||
|
||
cmd.TeardownTestRepo(t, repoDir, lockPath) | ||
} | ||
|
||
func TestNotFound(t *testing.T) { | ||
cmd.WorkDir = "." | ||
repoDir, lockPath := cmd.SetupTestRepo(t) | ||
cmd.CopyTestRepo(t, "") | ||
cmd.SetupTestLogger() | ||
|
||
args := []string{"sqlite/broken"} | ||
err := Which(args) | ||
if err == nil { | ||
t.Fatalf("expected error, got nil") | ||
} | ||
if !strings.Contains(err.Error(), "extension file is not found") { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
cmd.TeardownTestRepo(t, repoDir, lockPath) | ||
} | ||
|
||
func TestUnknown(t *testing.T) { | ||
cmd.WorkDir = "." | ||
repoDir, lockPath := cmd.SetupTestRepo(t) | ||
cmd.SetupTestLogger() | ||
|
||
args := []string{"sqlite/unknown"} | ||
err := Which(args) | ||
if err == nil { | ||
t.Fatalf("expected error, got nil") | ||
} | ||
if !strings.Contains(err.Error(), "package is not installed") { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
cmd.TeardownTestRepo(t, repoDir, lockPath) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters