Documentation ¶
Overview ¶
Example ¶
package main import ( "bytes" "fmt" "io/ioutil" "log" "os" "strings" "time" "github.com/readpe/repodb" ) // FileRecord is a simple file record, satisfying the Record interface type FileRecord struct { Name string `json:"name"` SoftDeleted bool `json:"softdeleted"` CreateOn time.Time `json:"created_on"` UpdatedOn time.Time `json:"updated_on"` DeletedOn time.Time `json:"deleted_on"` body string // unexported so as not to add body to meta-data in addition to file } // FileName returns the file name for the record. Satisfies Record interface func (fr *FileRecord) FileName() string { return fr.Name } // Folder returns the folder name for the record within the repository. Satisfies Record interface func (fr *FileRecord) Folder() string { return "files" } func main() { // temp directory used for example dir, err := ioutil.TempDir(os.TempDir(), "repodb") if err != nil { log.Fatal(err) } // create database and repository db := repodb.NewDB(dir) // setup Repo details for CreateRepo repo := &repodb.Repo{ Name: "HelloRepo", DB: db, Description: "A hello world repository.", Protected: false, SoftDeleted: false, CreatedOn: time.Now(), UpdatedOn: time.Now(), } // create repository: adds a directory does git init, and writes meta-data for repo err = db.CreateRepo(repo) if err != nil { log.Fatal(err) } // creating example record fr := &FileRecord{ Name: "HelloWorld.txt", SoftDeleted: false, CreateOn: time.Now(), UpdatedOn: time.Now(), body: "This is the body of the text file, an io.Reader could be used instead.", } // re-open repository for each write/read repo, err = db.OpenRepo("HelloRepo") if err != nil { log.Fatal(err) } // writes file to repository and commits all changes repo.WriteFile(fr, strings.NewReader(fr.body), repodb.CommitOptions{ Msg: fmt.Sprintf("added file %s to %s", fr.FileName(), repo.Dir()), }) repo.WriteMeta(fr, repodb.CommitOptions{ Msg: fmt.Sprintf("added meta-data for file %s to %s", fr.FileName(), repo.Dir()), }) // Read File buf := bytes.NewBufferString("") _, err = repo.ReadFile(fr, buf) if err != nil { log.Fatal(err) } fmt.Println(buf) }
Output: This is the body of the text file, an io.Reader could be used instead.
Index ¶
- Variables
- type CommitOptions
- type Record
- type Repo
- func (repo *Repo) CommitAll(opts CommitOptions) error
- func (repo *Repo) Dir() string
- func (repo *Repo) FileExists(rec Record) bool
- func (repo *Repo) FileName() string
- func (repo *Repo) Folder() string
- func (repo *Repo) LoadMeta(rec Record) error
- func (repo *Repo) Protect() error
- func (repo *Repo) ReadFile(rec Record, w io.Writer) (written int64, err error)
- func (repo *Repo) RemoveFile(rec Record, opts CommitOptions) error
- func (repo *Repo) RemoveMeta(rec Record, opts CommitOptions) error
- func (repo *Repo) WriteFile(rec Record, r io.Reader, opts CommitOptions) error
- func (repo *Repo) WriteMeta(rec Record, opts CommitOptions) error
- type RepoDB
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( MetaDir = "meta-data" DBRepoName = "db-repo" // the database repo name DBRepoCommitOptions = CommitOptions{ Msg: DBRepoName, Opts: git.CommitOptions{ Author: &object.Signature{Name: "repodb", Email: ""}, Committer: &object.Signature{Name: "repodb", Email: ""}, }, } )
package variables
var ( ErrRepoAlreadyExists = errors.New("repo already exists") ErrRepoNotExists = errors.New("repo does not exist") )
errors
Functions ¶
This section is empty.
Types ¶
type CommitOptions ¶
type CommitOptions struct { Msg string Opts git.CommitOptions }
CommitOptions is a wrapper struct arount git.CommitOptions with the addition of the message
type Repo ¶
type Repo struct { sync.RWMutex Name string DB *RepoDB `json:"-"` Description string Protected bool SoftDeleted bool CreatedOn time.Time UpdatedOn time.Time DeletedOn time.Time }
Repo is a git repository as a subdirectory under the RepoDB
func (*Repo) CommitAll ¶
func (repo *Repo) CommitAll(opts CommitOptions) error
CommitAll does a git add . && git commit -m "msg"
func (*Repo) FileExists ¶
FileExists checks if file exists
func (*Repo) Folder ¶
Folder is the record folder, final path component under the Repo. Implements Record interface
func (*Repo) RemoveFile ¶
func (repo *Repo) RemoveFile(rec Record, opts CommitOptions) error
RemoveFile removes the record. If there is an error it will be of type *os.PathError. This function will not remove the coresponding meta-data file, use in conjunction with RemoveMeta.
func (*Repo) RemoveMeta ¶
func (repo *Repo) RemoveMeta(rec Record, opts CommitOptions) error
RemoveMeta removes the records meta-data file. If there is an error it will be of type *os.PathError. This function will not remove the referenced record file, use in conjunction with RemoveFIle.
type RepoDB ¶
RepoDB is a file based database of git repositories.
func (*RepoDB) CreateRepo ¶
CreateRepo will create a git repository as a subdirectory dir in the RepoDB. Will return ErrRepoAlreadyExists if it already exists
func (*RepoDB) OpenRepo ¶
OpenRepo will open the git repository at the specified directory Will return ErrRepoNotExists if no valid repository is found
func (*RepoDB) RemoveRepo ¶
RemoveRepo will remove the current database and all files/sub-directories. Use with caution.