Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit f3d55c1

Browse files
committed
move image cleanup into pkg/util
1 parent ba8ee97 commit f3d55c1

File tree

4 files changed

+31
-32
lines changed

4 files changed

+31
-32
lines changed

cmd/analyze.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func analyzeImage(imageArg string, analyzerArgs []string) error {
6969
image, err := ip.GetImage()
7070

7171
if !save {
72-
defer cleanupImage(image)
72+
defer pkgutil.CleanupImage(image)
7373
}
7474
if err != nil {
7575
glog.Error(err.Error())

cmd/diff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ func diffImages(image1Arg, image2Arg string, diffArgs []string) error {
8989
wg.Wait()
9090

9191
if !save {
92-
defer cleanupImage(*imageMap[image1Arg])
93-
defer cleanupImage(*imageMap[image2Arg])
92+
defer pkgutil.CleanupImage(*imageMap[image1Arg])
93+
defer pkgutil.CleanupImage(*imageMap[image2Arg])
9494
}
9595

9696
diffTypes, err := differs.GetAnalyzers(diffArgs)

cmd/root.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
goflag "flag"
2222
"fmt"
23-
"os"
2423
"sort"
2524
"strings"
2625

@@ -84,16 +83,6 @@ func outputResults(resultMap map[string]util.Result) {
8483
}
8584
}
8685

87-
func cleanupImage(image pkgutil.Image) {
88-
if image.FSPath != "" {
89-
glog.Infof("Removing image filesystem directory %s from system", image.FSPath)
90-
errMsg := remove(image.FSPath, true)
91-
if errMsg != "" {
92-
glog.Error(errMsg)
93-
}
94-
}
95-
}
96-
9786
func validateArgs(args []string, validatefxns ...validatefxn) error {
9887
for _, validatefxn := range validatefxns {
9988
if err := validatefxn(args); err != nil {
@@ -133,24 +122,6 @@ func checkIfValidAnalyzer(flagtypes string) error {
133122
return nil
134123
}
135124

136-
func remove(path string, dir bool) string {
137-
var errStr string
138-
if path == "" {
139-
return ""
140-
}
141-
142-
var err error
143-
if dir {
144-
err = os.RemoveAll(path)
145-
} else {
146-
err = os.Remove(path)
147-
}
148-
if err != nil {
149-
errStr = "\nUnable to remove " + path
150-
}
151-
return errStr
152-
}
153-
154125
func init() {
155126
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
156127
}

pkg/util/image_prep_utils.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,31 @@ func (p TarPrepper) getConfig() (ConfigSchema, error) {
287287

288288
return config, nil
289289
}
290+
291+
func CleanupImage(image Image) {
292+
if image.FSPath != "" {
293+
glog.Infof("Removing image filesystem directory %s from system", image.FSPath)
294+
errMsg := remove(image.FSPath, true)
295+
if errMsg != "" {
296+
glog.Error(errMsg)
297+
}
298+
}
299+
}
300+
301+
func remove(path string, dir bool) string {
302+
var errStr string
303+
if path == "" {
304+
return ""
305+
}
306+
307+
var err error
308+
if dir {
309+
err = os.RemoveAll(path)
310+
} else {
311+
err = os.Remove(path)
312+
}
313+
if err != nil {
314+
errStr = "\nUnable to remove " + path
315+
}
316+
return errStr
317+
}

0 commit comments

Comments
 (0)