diff options
author | flu0r1ne <flu0r1ne@flu0r1ne.net> | 2021-07-29 13:09:23 -0500 |
---|---|---|
committer | flu0r1ne <flu0r1ne@flu0r1ne.net> | 2021-07-29 13:09:23 -0500 |
commit | 2c10158ce92f102280d4d87406ebeea3e05bf915 (patch) | |
tree | 61797fcdea06e128a04af05ce611f9babc0a8775 /cmd | |
parent | 34d7d612dd376258248ee6cae07b839951fccf32 (diff) | |
download | zsu-2c10158ce92f102280d4d87406ebeea3e05bf915.tar.xz zsu-2c10158ce92f102280d4d87406ebeea3e05bf915.zip |
Added README, Licence, and fixed issue with modules
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/cat.go | 24 | ||||
-rw-r--r-- | cmd/diff.go | 61 | ||||
-rw-r--r-- | cmd/list.go | 2 | ||||
-rw-r--r-- | cmd/overwrite.go | 21 |
4 files changed, 69 insertions, 39 deletions
@@ -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 |