@@ -2,58 +2,45 @@ package utils
22
33import (
44 "context"
5- "fmt"
65 "io"
76 "log/slog"
87 "net/url"
98 "os"
109 "path"
1110 "path/filepath"
1211 "strings"
13- "time"
1412
1513 "github.com/Obmondo/kubeaid-bootstrap-script/pkg/config"
16- "github.com/Obmondo/kubeaid-bootstrap-script/pkg/globals "
14+ "github.com/Obmondo/kubeaid-bootstrap-script/pkg/constants "
1715 "github.com/Obmondo/kubeaid-bootstrap-script/pkg/utils/assert"
16+ "github.com/Obmondo/kubeaid-bootstrap-script/pkg/utils/logger"
1817)
1918
2019// Creates a temp dir inside /tmp, where KubeAid Bootstrap Script will clone repos.
2120// Then sets the value of constants.TempDir as the temp dir path.
2221// If the temp dir already exists, then that gets reused.
2322func InitTempDir (ctx context.Context ) {
24- namePrefix := "kubeaid-bootstrap-script-"
23+ ctx = logger .AppendSlogAttributesToCtx (ctx , []slog.Attr {
24+ slog .String ("path" , constants .TempDirectory ),
25+ })
2526
2627 // Check if a temp dir already exists for KubeAid Bootstrap Script.
2728 // If yes, then reuse that.
2829 filesAndFolders , err := os .ReadDir ("/tmp" )
2930 assert .AssertErrNil (ctx , err , "Failed listing files and folders in /tmp" )
3031 for _ , item := range filesAndFolders {
31- if item .IsDir () && strings .HasPrefix (item .Name (), namePrefix ) {
32- path := "/tmp/" + item .Name ()
33- slog .InfoContext (ctx ,
34- "Skipped creating temp dir, since it already exists" ,
35- slog .String ("path" , path ),
36- )
37-
38- globals .TempDir = path
39-
32+ if item .IsDir () && (item .Name () == constants .TempDirectory ) {
33+ slog .InfoContext (ctx , "Skipped creating temp dir, since it already exists" )
4034 return
4135 }
4236 }
4337
4438 // Otherwise, create it.
4539
46- dirName := fmt .Sprintf ("%s%d" , namePrefix , time .Now ().Unix ())
47-
48- path , err := os .MkdirTemp ("/tmp" , dirName )
49- assert .AssertErrNil (ctx , err ,
50- "Failed creating temp dir" ,
51- slog .String ("path" , path ),
52- )
40+ path , err := os .MkdirTemp ("/tmp" , constants .TempDirectoryName )
41+ assert .AssertErrNil (ctx , err , "Failed creating temp dir" )
5342
5443 slog .InfoContext (ctx , "Created temp dir" , slog .String ("path" , path ))
55-
56- globals .TempDir = path
5744}
5845
5946// Returns path to the parent dir of the given file.
@@ -76,27 +63,15 @@ func CreateIntermediateDirsForFile(ctx context.Context, filePath string) {
7663 )
7764}
7865
79- // Returns path to the directory (in temp directory), where the KubeAid repo is / will be cloned.
80- func GetKubeAidDir () string {
81- return path .Join (globals .TempDir , "KubeAid" )
82- }
83-
84- // Returns path to the directory (in temp directory), where the customer's KubeAid Config repo
85- // is / will be cloned.
86- func GetKubeAidConfigDir () string {
87- return path .Join (globals .TempDir , "kubeaid-config" )
88- }
89-
9066// Returns path to the directory containing cluster specific config, in the KubeAid Config dir.
9167func GetClusterDir () string {
92- clusterDir := path .Join (GetKubeAidConfigDir (), "k8s" , config .ParsedGeneralConfig .Cluster .Name )
93- return clusterDir
68+ return path .Join (constants .KubeAidConfigDirectory , "k8s" , config .ParsedGeneralConfig .Cluster .Name )
9469}
9570
9671// Returns the path to the local temp directory, where contents of the given blob storage bucket
9772// will be / is downloaded.
9873func GetDownloadedStorageBucketContentsDir (bucketName string ) string {
99- return path .Join (globals . TempDir , "buckets" , bucketName )
74+ return path .Join (constants . TempDirectory , "buckets" , bucketName )
10075}
10176
10277// Converts the given relative path to an absolute path.
0 commit comments