9
9
"os"
10
10
"os/exec"
11
11
"os/signal"
12
- "path/filepath"
13
12
"runtime"
14
13
"strconv"
15
14
"strings"
@@ -710,34 +709,31 @@ func runSingleScan(ctx context.Context, cmd string, cfg engine.Config) (metrics,
710
709
}
711
710
eng .Start (ctx )
712
711
713
- tmpDir := filepath .Join (os .TempDir (), "trufflehog_" + strconv .Itoa (os .Getpid ()))
714
712
persistRepo := * gitNoCleanup || * githubNoCleanup || * gitlabNoCleanup
713
+ clonePath := ""
715
714
716
715
defer func () {
717
716
// Clean up temporary artifacts.
718
717
if err := cleantemp .CleanTempArtifacts (ctx ); err != nil {
719
718
ctx .Logger ().Error (err , "error cleaning temp artifacts" )
720
719
}
721
720
722
- if ! persistRepo {
723
- if err := os .RemoveAll (tmpDir ); err != nil {
724
- ctx .Logger ().Error (err , "error removing temporary directory" )
721
+ if * jsonLegacy {
722
+ // If JSON legacy is enabled, that means the cloned repos are not deleted yet
723
+ // because they were needed for outputting legacy JSON.
724
+ // We only clean them up here if the user did not request to persist them.
725
+ if ! persistRepo {
726
+ if err := cleantemp .CleanTempDirsForLegacyJSON (clonePath ); err != nil {
727
+ ctx .Logger ().Error (err , "error cleaning temp artifacts for legacy JSON" )
728
+ }
725
729
}
726
730
}
727
731
}()
728
732
729
733
var refs []sources.JobProgressRef
730
734
switch cmd {
731
735
case gitScan .FullCommand ():
732
- if * jsonLegacy && ! * gitNoCleanup {
733
- if * gitClonePath == "" {
734
- if err := os .MkdirAll (tmpDir , os .ModePerm ); err != nil {
735
- return scanMetrics , fmt .Errorf ("failed to create temporary directory: %v" , err )
736
- }
737
- * gitClonePath = tmpDir
738
- }
739
- * gitNoCleanup = true
740
- }
736
+ clonePath = * gitClonePath
741
737
// validate the commit for local repository only
742
738
if * gitScanSinceCommit != "" && strings .HasPrefix (* gitScanURI , "file" ) {
743
739
if ! isValidCommit (* gitScanURI , * gitScanSinceCommit ) {
@@ -765,22 +761,15 @@ func runSingleScan(ctx context.Context, cmd string, cfg engine.Config) (metrics,
765
761
ExcludeGlobs : * gitScanExcludeGlobs ,
766
762
ClonePath : * gitClonePath ,
767
763
NoCleanup : * gitNoCleanup ,
764
+ PrintLegacyJSON : * jsonLegacy ,
768
765
}
769
766
if ref , err := eng .ScanGit (ctx , gitCfg ); err != nil {
770
767
return scanMetrics , fmt .Errorf ("failed to scan Git: %v" , err )
771
768
} else {
772
769
refs = []sources.JobProgressRef {ref }
773
770
}
774
771
case githubScan .FullCommand ():
775
- if * jsonLegacy && ! * githubNoCleanup {
776
- if * githubClonePath == "" {
777
- if err := os .MkdirAll (tmpDir , os .ModePerm ); err != nil {
778
- return scanMetrics , fmt .Errorf ("failed to create temporary directory: %v" , err )
779
- }
780
- * githubClonePath = tmpDir
781
- }
782
- * githubNoCleanup = true
783
- }
772
+ clonePath = * githubClonePath
784
773
filter , err := common .FilterFromFiles (* githubScanIncludePaths , * githubScanExcludePaths )
785
774
if err != nil {
786
775
return scanMetrics , fmt .Errorf ("could not create filter: %v" , err )
@@ -816,6 +805,7 @@ func runSingleScan(ctx context.Context, cmd string, cfg engine.Config) (metrics,
816
805
ClonePath : * githubClonePath ,
817
806
NoCleanup : * githubNoCleanup ,
818
807
IgnoreGists : * githubIgnoreGists ,
808
+ PrintLegacyJSON : * jsonLegacy ,
819
809
}
820
810
821
811
if ref , err := eng .ScanGitHub (ctx , cfg ); err != nil {
@@ -837,15 +827,7 @@ func runSingleScan(ctx context.Context, cmd string, cfg engine.Config) (metrics,
837
827
refs = []sources.JobProgressRef {ref }
838
828
}
839
829
case gitlabScan .FullCommand ():
840
- if * jsonLegacy && ! * gitlabNoCleanup {
841
- if * gitlabClonePath == "" {
842
- if err := os .MkdirAll (tmpDir , os .ModePerm ); err != nil {
843
- return scanMetrics , fmt .Errorf ("failed to create temporary directory: %v" , err )
844
- }
845
- * gitlabClonePath = tmpDir
846
- }
847
- * gitlabNoCleanup = true
848
- }
830
+ clonePath = * gitlabClonePath
849
831
filter , err := common .FilterFromFiles (* gitlabScanIncludePaths , * gitlabScanExcludePaths )
850
832
if err != nil {
851
833
return scanMetrics , fmt .Errorf ("could not create filter: %v" , err )
@@ -860,16 +842,17 @@ func runSingleScan(ctx context.Context, cmd string, cfg engine.Config) (metrics,
860
842
}
861
843
862
844
cfg := sources.GitlabConfig {
863
- Endpoint : * gitlabScanEndpoint ,
864
- Token : * gitlabScanToken ,
865
- Repos : * gitlabScanRepos ,
866
- GroupIds : * gitlabScanGroupIds ,
867
- IncludeRepos : * gitlabScanIncludeRepos ,
868
- ExcludeRepos : * gitlabScanExcludeRepos ,
869
- Filter : filter ,
870
- AuthInUrl : * gitlabAuthInUrl ,
871
- ClonePath : * gitlabClonePath ,
872
- NoCleanup : * gitlabNoCleanup ,
845
+ Endpoint : * gitlabScanEndpoint ,
846
+ Token : * gitlabScanToken ,
847
+ Repos : * gitlabScanRepos ,
848
+ GroupIds : * gitlabScanGroupIds ,
849
+ IncludeRepos : * gitlabScanIncludeRepos ,
850
+ ExcludeRepos : * gitlabScanExcludeRepos ,
851
+ Filter : filter ,
852
+ AuthInUrl : * gitlabAuthInUrl ,
853
+ ClonePath : * gitlabClonePath ,
854
+ NoCleanup : * gitlabNoCleanup ,
855
+ PrintLegacyJSON : * jsonLegacy ,
873
856
}
874
857
875
858
if ref , err := eng .ScanGitLab (ctx , cfg ); err != nil {
0 commit comments