This repository has been archived by the owner on Apr 21, 2024. It is now read-only.
-
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
36 changed files
with
826 additions
and
192 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
image: golang:1.12.7 | ||
|
||
before_script: | ||
- go get -u -v gitlab.com/NatoBoram/git-to-ipns | ||
- go get -u -v github.com/GeertJohan/go.rice/rice | ||
- go get -u -v gitlab.com/NatoBoram/git-to-ipfs | ||
- rice embed-go | ||
- go clean | ||
|
||
build: | ||
stage: build | ||
script: | ||
- rice embed-go | ||
- go build ./... | ||
- go build | ||
|
||
test: | ||
stage: test | ||
script: | ||
- go test ./... | ||
- go test -v -cover -race ./... | ||
|
||
after_script: | ||
- go clean |
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 |
---|---|---|
@@ -1,121 +1,82 @@ | ||
// Low-level operations on the database. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
badger "github.com/dgraph-io/badger" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
func getFromURL(db *badger.DB, link string, ch chan Repo, oldRepo func(*badger.DB, Repo) (Repo, error), newRepo func(*badger.DB, string) (Repo, error)) (err error) { | ||
err = db.View(func(txn *badger.Txn) (err error) { | ||
|
||
// Select from URL | ||
item, err := txn.Get([]byte(link)) | ||
if err == badger.ErrKeyNotFound { | ||
repo, err := newRepo(db, link) | ||
if err != nil { | ||
return xerrors.Errorf("Couldn't add a new repo : %w", err) | ||
} | ||
ch <- repo | ||
} else if err != nil { | ||
return xerrors.Errorf("Couldn't select an URL : %w", err) | ||
} | ||
|
||
// Get value | ||
err = item.Value(func(bytes []byte) (err error) { | ||
|
||
// Decode | ||
repo, err := decodeRepo(bytes) | ||
if err != nil { | ||
return xerrors.Errorf("Couldn't decode a repo : %w", err) | ||
} | ||
|
||
// Execute the callback only if there's no error. | ||
repo, err = oldRepo(db, repo) | ||
if err != nil { | ||
return xerrors.Errorf("Couldn't refresh an old repo : %w", err) | ||
} | ||
ch <- repo | ||
|
||
// Unreachable code | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return xerrors.Errorf("Couldn't get an URL's value : %w", err) | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return xerrors.Errorf("Couldn't create a view from an URL : %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (repo Repo) save(db *badger.DB) (err error) { | ||
err = db.Update(func(txn *badger.Txn) error { | ||
func badgerSet(db *badger.DB, repo Repo) error { | ||
return db.Update(func(txn *badger.Txn) error { | ||
|
||
// Encode | ||
bytes, err := repo.encode() | ||
if err != nil { | ||
return xerrors.Errorf("Couldn't encode a repo : %w", err) | ||
return err | ||
} | ||
|
||
// Save | ||
err = txn.Set([]byte(repo.URL), bytes) | ||
if err != nil { | ||
return xerrors.Errorf("Couldn't save a repo : %w", err) | ||
} | ||
|
||
return nil | ||
return err | ||
}) | ||
|
||
if err != nil { | ||
return xerrors.Errorf("Couldn't create an update transaction : %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getRepos(db *badger.DB, ch chan Repo) error { | ||
|
||
// Create a view | ||
// badgerList populates a channel with every single keys inside the Badger database. | ||
func badgerList(db *badger.DB, ch chan repoerr) error { | ||
err := db.View(func(txn *badger.Txn) error { | ||
|
||
it := txn.NewIterator(badger.DefaultIteratorOptions) | ||
defer it.Close() | ||
defer close(ch) | ||
|
||
for it.Rewind(); it.Valid(); it.Next() { | ||
item := it.Item() | ||
|
||
err := item.Value(func(v []byte) error { | ||
|
||
repo, err := decodeRepo(v) | ||
if err != nil { | ||
return xerrors.Errorf("Couldn't decode a repo : %w", err) | ||
ch <- repoerr{ | ||
repo: repo, | ||
err: err, | ||
} | ||
|
||
ch <- repo | ||
|
||
return nil | ||
return err | ||
}) | ||
|
||
// Continue even if there's an error | ||
if err != nil { | ||
fmt.Println("Couldn't get a value.") | ||
fmt.Println("Couldn't get the value of an item.") | ||
fmt.Println(err.Error()) | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("Couldn't create a view : %w", err) | ||
} | ||
|
||
return nil | ||
close(ch) | ||
return err | ||
} | ||
|
||
// Get a repository from its URL. | ||
// | ||
// Can return `badger.ErrKeyNotFound`. | ||
func badgerGet(db *badger.DB, link string, ch chan Repo) error { | ||
err := db.View(func(txn *badger.Txn) error { | ||
item, err := txn.Get([]byte(link)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return item.Value(func(bytes []byte) error { | ||
repo, err := decodeRepo(bytes) | ||
ch <- repo | ||
return err | ||
}) | ||
}) | ||
|
||
close(ch) | ||
return err | ||
} | ||
|
||
func badgerDelete(db *badger.DB, link string) error { | ||
return db.Update(func(txn *badger.Txn) error { | ||
return txn.Delete([]byte(link)) | ||
}) | ||
} |
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,20 @@ | ||
package main | ||
|
||
import ( | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/logrusorgru/aurora" | ||
) | ||
|
||
func TestRun(t *testing.T) { | ||
_, err := run( | ||
exec.Command("go", "help"), | ||
".", | ||
"Couldn't run the Go command.", | ||
aurora.Bold("Command :"), "go", "help", | ||
) | ||
if err != nil { | ||
t.Errorf("Failed to run a command: %s.", err.Error()) | ||
} | ||
} |
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
Oops, something went wrong.