From 34d7d612dd376258248ee6cae07b839951fccf32 Mon Sep 17 00:00:00 2001 From: flu0r1ne Date: Thu, 22 Jul 2021 17:18:21 -0500 Subject: List command, diff command, and snapshot search infrastructure --- cmd/cat.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++----- cmd/list.go | 20 +++++++++++++++++--- 2 files changed, 66 insertions(+), 8 deletions(-) (limited to 'cmd') diff --git a/cmd/cat.go b/cmd/cat.go index d5a275d..186f7da 100644 --- a/cmd/cat.go +++ b/cmd/cat.go @@ -3,19 +3,63 @@ package cmd import ( "fmt" "golang.flu0r1ne.net/zfdiff/snap" + "os" + "io" ) func Cat(params []string) { - if(len(params) == 0) { + n_params := len(params) + + if(n_params == 0) { die.Fatal("Reference file is required") } - if(len(params) > 3) { + if(n_params > 3) { die.Fatal("Too many arguments provided") } - snapRef := snap.ToRelative(params[0]) - reference := params[1] + var reference string + snapish := "" + + if(n_params == 1) { + reference = params[0] + } else { + snapish, reference = params[0], params[1] + } + + snapRef := snap.ToRelative(snapish) + + oracle := snap.GetOracle(reference) + + path := oracle.ResolveRelative(snapRef) + + file, err := os.Open(path) + + if err != nil { + die.Fatal("Could not open snapshot %s\nError: %v", path, err) + } + + defer func() { + if err = file.Close(); err != nil { + die.Fatal("Could not close file %s\nError: %v", path, err) + } + }() + + buf := make([]byte, 10*1024*1024) //10Ki + + for { + n, err := file.Read(buf) + + if n > 0 { + fmt.Printf("%s", buf[:n]) + } + + if err == io.EOF { + break + } - fmt.Println(snapRef, reference); + if err != nil { + die.Fatal("Encountered error while reading file: %v", err) + } + } } \ No newline at end of file diff --git a/cmd/list.go b/cmd/list.go index 99d5f24..5849975 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -3,13 +3,14 @@ package cmd import ( "fmt" "flag" + "golang.flu0r1ne.net/zfdiff/snap" ) func List(params []string) { flags := flag.NewFlagSet("list", flag.ExitOnError) var withPaths bool - + withName := aliasedBoolVar( flags, &withPaths, @@ -32,6 +33,19 @@ func List(params []string) { reference := flags.Arg(0) - fmt.Printf(reference) - fmt.Printf("Your flag is: %t", withPaths); + snaps := snap.GetTimeseries(reference) + + for _, s := range snaps { + if s.Reference == "" { + continue + } + + fmt.Printf("%s", s.Name) + + if withPaths { + fmt.Printf(", %s", s.Reference) + } + + fmt.Println("") + } } -- cgit v1.2.3