feat: Implement stcache list command
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/pojntfx/stfs/pkg/formatting"
|
||||
"github.com/pojntfx/stfs/pkg/persisters"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var list = &cobra.Command{
|
||||
Use: "list",
|
||||
Aliases: []string{"l"},
|
||||
Short: "List contents of index",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
metadataPersister := persisters.NewMetadataPersister(viper.GetString(dbFlag))
|
||||
if err := metadataPersister.Open(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
headers, err := metadataPersister.GetHeaders(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i, hdr := range headers {
|
||||
if i == 0 {
|
||||
if err := formatting.PrintCSV(formatting.TARHeaderCSV); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
paxRecords := map[string]string{}
|
||||
if err := json.Unmarshal([]byte(hdr.Paxrecords), &paxRecords); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := formatting.PrintCSV(formatting.GetTARHeaderAsCSV(hdr.Record, hdr.Block, &tar.Header{
|
||||
Typeflag: byte(hdr.Typeflag),
|
||||
Name: hdr.Name,
|
||||
Linkname: hdr.Linkname,
|
||||
Size: hdr.Size,
|
||||
Mode: hdr.Mode,
|
||||
Uid: int(hdr.UID),
|
||||
Gid: int(hdr.Gid),
|
||||
Uname: hdr.Uname,
|
||||
Gname: hdr.Gname,
|
||||
ModTime: hdr.Modtime,
|
||||
AccessTime: hdr.Accesstime,
|
||||
ChangeTime: hdr.Changetime,
|
||||
Devmajor: hdr.Devmajor,
|
||||
Devminor: hdr.Devminor,
|
||||
PAXRecords: paxRecords,
|
||||
Format: tar.Format(hdr.Format),
|
||||
})); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Get default working dir
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
workingDirDefault := filepath.Join(home, ".local", "share", "stcache", "var", "lib", "stcache")
|
||||
|
||||
list.PersistentFlags().StringP(dbFlag, "d", filepath.Join(workingDirDefault, "index.sqlite"), "Database to use")
|
||||
|
||||
viper.AutomaticEnv()
|
||||
|
||||
rootCmd.AddCommand(list)
|
||||
}
|
||||
@@ -33,7 +33,7 @@ func NewMetadataPersister(dbPath string) *MetadataPersister {
|
||||
}
|
||||
|
||||
func (c *MetadataPersister) UpsertHeader(ctx context.Context, record, block int64, hdr *tar.Header) error {
|
||||
paxHeaders, err := json.Marshal(hdr.PAXRecords)
|
||||
paxRecords, err := json.Marshal(hdr.PAXRecords)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -55,7 +55,7 @@ func (c *MetadataPersister) UpsertHeader(ctx context.Context, record, block int6
|
||||
Changetime: hdr.ChangeTime,
|
||||
Devmajor: hdr.Devmajor,
|
||||
Devminor: hdr.Devminor,
|
||||
Paxrecords: string(paxHeaders),
|
||||
Paxrecords: string(paxRecords),
|
||||
Format: int64(hdr.Format),
|
||||
}
|
||||
|
||||
@@ -77,3 +77,7 @@ func (c *MetadataPersister) UpsertHeader(ctx context.Context, record, block int6
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *MetadataPersister) GetHeaders(ctx context.Context) (models.HeaderSlice, error) {
|
||||
return models.Headers().All(ctx, p.db)
|
||||
}
|
||||
|
||||
@@ -16,26 +16,26 @@ type SQLite struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func (d *SQLite) Open() error {
|
||||
func (s *SQLite) Open() error {
|
||||
// Create leading directories for database
|
||||
leadingDir, _ := filepath.Split(d.DBPath)
|
||||
leadingDir, _ := filepath.Split(s.DBPath)
|
||||
if err := os.MkdirAll(leadingDir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Open the DB
|
||||
db, err := sql.Open("sqlite3", d.DBPath)
|
||||
db, err := sql.Open("sqlite3", s.DBPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Configure the db
|
||||
db.SetMaxOpenConns(1) // Prevent "database locked" errors
|
||||
d.db = db
|
||||
s.db = db
|
||||
|
||||
// Run migrations if set
|
||||
if d.Migrations != nil {
|
||||
if _, err := migrate.Exec(d.db, "sqlite3", d.Migrations, migrate.Up); err != nil {
|
||||
if s.Migrations != nil {
|
||||
if _, err := migrate.Exec(s.db, "sqlite3", s.Migrations, migrate.Up); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user