aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/cat.go24
-rw-r--r--cmd/diff.go61
-rw-r--r--cmd/list.go2
-rw-r--r--cmd/overwrite.go21
4 files changed, 69 insertions, 39 deletions
diff --git a/cmd/cat.go b/cmd/cat.go
index 186f7da..f3ea2bf 100644
--- a/cmd/cat.go
+++ b/cmd/cat.go
@@ -2,7 +2,7 @@ package cmd
import (
"fmt"
- "golang.flu0r1ne.net/zfdiff/snap"
+ "golang.flu0r1ne.net/zsu/snap"
"os"
"io"
)
@@ -14,34 +14,32 @@ func Cat(params []string) {
die.Fatal("Reference file is required")
}
- if(n_params > 3) {
+ if(n_params > 2) {
die.Fatal("Too many arguments provided")
}
- var reference string
- snapish := ""
+ reference := params[0]
- if(n_params == 1) {
- reference = params[0]
- } else {
- snapish, reference = params[0], params[1]
+ snapish := "@"
+ if(n_params > 1) {
+ snapish = params[1]
}
snapRef := snap.ToRelative(snapish)
oracle := snap.GetOracle(reference)
- path := oracle.ResolveRelative(snapRef)
+ path := oracle.PathTo(snapRef)
file, err := os.Open(path)
if err != nil {
- die.Fatal("Could not open snapshot %s\nError: %v", path, err)
+ die.Fatalf("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)
+ die.Fatalf("Could not close file %s\nError: %v", path, err)
}
}()
@@ -59,7 +57,7 @@ func Cat(params []string) {
}
if err != nil {
- die.Fatal("Encountered error while reading file: %v", err)
+ die.Fatalf("Encountered error while reading file: %v", err)
}
}
-} \ No newline at end of file
+}
diff --git a/cmd/diff.go b/cmd/diff.go
index 46327b3..8d6a85f 100644
--- a/cmd/diff.go
+++ b/cmd/diff.go
@@ -1,8 +1,13 @@
package cmd
import (
+ "golang.flu0r1ne.net/zsu/snap"
+ "os/exec"
+ "os"
+ "io"
+ "log"
"fmt"
- "golang.flu0r1ne.net/zfdiff/snap"
+ "errors"
)
func Diff(params []string) {
@@ -15,9 +20,57 @@ func Diff(params []string) {
die.Fatal("Too many arguments provided")
}
- from, to := snap.ParseDiff(params[0])
+ reference := params[0]
- reference := params[1]
+ from, to := snap.ToRelative("@-"), snap.ToRelative("@")
+ if len(params) > 1 {
+ from, to = snap.ParseDiff(params[1])
+ }
+
+ oracle := snap.GetOracle(reference)
+
+ fromPath := oracle.PathTo(from)
+ toPath := oracle.PathTo(to)
+
+ _, noColor := os.LookupEnv("NO_COLOR")
+
+ colorOpt := "--color=always"
+ if noColor {
+ colorOpt = "--color=never"
+ }
+
+ diff := exec.Command("diff", colorOpt, fromPath, toPath)
+ diff.Env = os.Environ()
+
+ stderr, err := diff.StderrPipe()
+ if err != nil {
+ log.Fatal(err);
+ }
- fmt.Println(from, to, reference);
+ stdout, err := diff.StdoutPipe()
+ if err != nil {
+ log.Fatal(err);
+ }
+
+ if err := diff.Start(); err != nil {
+ log.Fatal(err)
+ }
+
+ outtext, _ := io.ReadAll(stdout)
+ errtext, _ := io.ReadAll(stderr)
+
+ fmt.Fprintf(os.Stderr, "%s", errtext)
+ fmt.Fprintf(os.Stdout, "%s", outtext)
+
+ if err := diff.Wait(); err != nil {
+ var exiterr *exec.ExitError
+
+ // GNU Diff exits 1 if the files differ
+ if errors.As(err, &exiterr) && exiterr.ExitCode() == 1 {
+ os.Exit(exiterr.ExitCode())
+ }
+
+ die.Fatalf("Error encountered while executing diff.")
+ os.Exit(2)
+ }
}
diff --git a/cmd/list.go b/cmd/list.go
index 5849975..94c18b8 100644
--- a/cmd/list.go
+++ b/cmd/list.go
@@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"flag"
- "golang.flu0r1ne.net/zfdiff/snap"
+ "golang.flu0r1ne.net/zsu/snap"
)
func List(params []string) {
diff --git a/cmd/overwrite.go b/cmd/overwrite.go
deleted file mode 100644
index 324dfb4..0000000
--- a/cmd/overwrite.go
+++ /dev/null
@@ -1,21 +0,0 @@
-package cmd
-
-import (
- "fmt"
- "golang.flu0r1ne.net/zfdiff/snap"
-)
-
-func Overwrite(params [] string) {
- if(len(params) == 0) {
- die.Fatal("Reference file is required")
- }
-
- if(len(params) > 3) {
- die.Fatal("Too many arguments provided")
- }
-
- snapRef := snap.ToRelative(params[0])
- reference := params[1]
-
- fmt.Print(snapRef, reference)
-} \ No newline at end of file