package cmd import ( "golang.flu0r1ne.net/zsu/snap" "os/exec" "os" "io" "log" "fmt" "errors" ) func Diff(params []string) { if(len(params) == 0) { die.Fatal("Reference file is required") } if(len(params) > 2) { die.Fatal("Too many arguments provided") } reference := params[0] 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); } 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) } }