forked from GoogleCloudPlatform/runtimes-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iDiff refactor with interfaces (GoogleCloudPlatform#243)
* Differ + image refactor * Output and DiffResult refactor * Breaking up JSON and text outputs * Refactored if branch chains to use reflection for image source determination * Extracted out eng
- Loading branch information
Showing
19 changed files
with
456 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,48 @@ | ||
package differs | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"os" | ||
"reflect" | ||
|
||
"github.com/GoogleCloudPlatform/runtimes-common/iDiff/utils" | ||
) | ||
|
||
var diffs = map[string]func(string, string, bool, bool) (string, error){ | ||
"hist": HistoryDiff, | ||
"history": HistoryDiff, | ||
"file": FileDiff, | ||
"apt": AptDiff, | ||
"linux": AptDiff, | ||
"pip": PipDiff, | ||
"node": NodeDiff, | ||
type DiffRequest struct { | ||
Image1 utils.Image | ||
Image2 utils.Image | ||
DiffType Differ | ||
} | ||
|
||
func Diff(arg1, arg2, differ string, json bool, eng bool) (string, error) { | ||
if f, exists := diffs[differ]; exists { | ||
fValue := reflect.ValueOf(f) | ||
histValue := reflect.ValueOf(HistoryDiff) | ||
fileValue := reflect.ValueOf(FileDiff) | ||
if fValue.Pointer() == histValue.Pointer() || fValue.Pointer() == fileValue.Pointer() { | ||
return f(arg1, arg2, json, eng) | ||
} | ||
return specificDiffer(f, arg1, arg2, json, eng) | ||
} | ||
return "", errors.New("Unknown differ") | ||
type DiffResult interface { | ||
OutputJSON() error | ||
OutputText() error | ||
} | ||
|
||
func specificDiffer(f func(string, string, bool, bool) (string, error), img1, img2 string, json bool, eng bool) (string, error) { | ||
var buffer bytes.Buffer | ||
validDiff := true | ||
imgPath1, err := utils.ImageToFS(img1, eng) | ||
if err != nil { | ||
buffer.WriteString(err.Error()) | ||
validDiff = false | ||
} | ||
imgPath2, err := utils.ImageToFS(img2, eng) | ||
if err != nil { | ||
buffer.WriteString(err.Error()) | ||
validDiff = false | ||
} | ||
|
||
var diff string | ||
if validDiff { | ||
output, err := f(imgPath1, imgPath2, json, eng) | ||
if err != nil { | ||
buffer.WriteString(err.Error()) | ||
} | ||
diff = output | ||
} | ||
|
||
errStr := remove(imgPath1, true) | ||
errStr += remove(imgPath2, true) | ||
if errStr != "" { | ||
buffer.WriteString(errStr) | ||
} | ||
type Differ interface { | ||
Diff(image1, image2 utils.Image) (DiffResult, error) | ||
} | ||
|
||
if buffer.String() != "" { | ||
return diff, errors.New(buffer.String()) | ||
} | ||
return diff, nil | ||
var diffs = map[string]Differ{ | ||
"hist": HistoryDiffer{}, | ||
"history": HistoryDiffer{}, | ||
"file": FileDiffer{}, | ||
"apt": AptDiffer{}, | ||
"linux": AptDiffer{}, | ||
"pip": PipDiffer{}, | ||
"node": NodeDiffer{}, | ||
} | ||
|
||
func remove(path string, dir bool) string { | ||
var errStr string | ||
if path == "" { | ||
return "" | ||
} | ||
func (diff DiffRequest) GetDiff() (DiffResult, error) { | ||
img1 := diff.Image1 | ||
img2 := diff.Image2 | ||
differ := diff.DiffType | ||
return differ.Diff(img1, img2) | ||
} | ||
|
||
var err error | ||
if dir { | ||
err = os.RemoveAll(path) | ||
func GetDiffer(diffName string) (differ Differ, err error) { | ||
if d, exists := diffs[diffName]; exists { | ||
differ = d | ||
} else { | ||
err = os.Remove(path) | ||
} | ||
if err != nil { | ||
errStr = "\nUnable to remove " + path | ||
errors.New("Unknown differ") | ||
} | ||
return errStr | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.