@@ -9,13 +9,14 @@ import (
99 "path/filepath"
1010 "slices"
1111 "strings"
12+ "text/tabwriter"
1213 "time"
1314
1415 "github.com/signadot/cli/internal/config"
1516 "github.com/signadot/cli/internal/local"
1617 "github.com/signadot/cli/internal/locald/sandboxmanager"
1718 "github.com/signadot/cli/internal/print"
18- "github.com/signadot/cli/internal/utils"
19+ "github.com/signadot/cli/internal/utils/system "
1920 "github.com/signadot/go-sdk/client/sandboxes"
2021 "github.com/signadot/go-sdk/models"
2122 "github.com/signadot/libconnect/common/k8senv"
@@ -76,12 +77,27 @@ func getFiles(cfg *config.SandboxGetFiles, out, errOut io.Writer, name string) e
7677 if err != nil {
7778 return err
7879 }
79- absOut , err := filepath .Abs (cfg .OutputDir )
80- if err != nil {
81- return err
80+ if cfg .OutputDir == "" {
81+ baseDir , err := system .GetSandboxLocalFilesBaseDir (name )
82+ if err != nil {
83+ return err
84+ }
85+ _ , err = os .Stat (baseDir )
86+ if err == nil {
87+ err := os .RemoveAll (baseDir )
88+ if err != nil {
89+ return err
90+ }
91+ } else {
92+ if ! os .IsNotExist (err ) {
93+ return err
94+ }
95+ }
96+ cfg .OutputDir = baseDir
8297 }
83- err = writeGCFiles (k8sEnv .Files , absOut )
84- if err != nil {
98+ // either no err from stat and no such baseDir
99+ // or error is that baseDir doesn't exist
100+ if err := os .MkdirAll (cfg .OutputDir , 0755 ); err != nil {
85101 return err
86102 }
87103 // no-clobber
@@ -101,7 +117,11 @@ func getFiles(cfg *config.SandboxGetFiles, out, errOut io.Writer, name string) e
101117 k8sEnv .Files .Name = cfg .OutputDir
102118 switch cfg .OutputFormat {
103119 case config .OutputFormatDefault :
104- return printTree (out , k8sEnv .Files , []bool {})
120+ w := tabwriter .NewWriter (os .Stdout , 0 , 4 , 1 , ' ' , tabwriter .TabIndent )
121+ if err := printTree (w , k8sEnv .Files , cfg .OutputDir , []bool {}); err != nil {
122+ return err
123+ }
124+ return w .Flush ()
105125 case config .OutputFormatJSON :
106126 return print .RawJSON (out , k8sEnv .Files )
107127 case config .OutputFormatYAML :
@@ -119,6 +139,7 @@ func calculateFileOverrides(ctx context.Context, kubeClient client.Client, ns st
119139 child := files .Path (fileOp .Path )
120140 if fileOp .ValueFrom == nil {
121141 child .Content = []byte (fileOp .Value )
142+ child .Source = & k8senv.Source {Override : true , Constant : & fileOp .Value }
122143 continue
123144 }
124145 if err := overrideFileValueFrom (ctx , kubeClient , child , fileOp , ns , resOuts , cmMap , secMap ); err != nil {
@@ -156,34 +177,50 @@ func overrideFileValueFrom(ctx context.Context, kubeClient client.Client, child
156177 return nil
157178 }
158179 child .Content = []byte (val )
180+ child .Source = & k8senv.Source {
181+ Override : true ,
182+ ConfigMap : & k8senv.MapKey {
183+ Namespace : cm .Namespace ,
184+ Name : cm .Name ,
185+ Key : cmSource .Key ,
186+ },
187+ }
159188 }
160189 return nil
161190
162191 case vf .Secret != nil :
163- cmSource := vf .Secret
164- cm := secMap [cmSource .Name ]
165- if cm == nil {
166- cm = & corev1.Secret {}
167- err := kubeClient .Get (ctx , client.ObjectKey {Namespace : ns , Name : cmSource .Name }, cm )
168- if cmSource .Optional && k8serrors .IsNotFound (err ) {
192+ secSource := vf .Secret
193+ sec := secMap [secSource .Name ]
194+ if sec == nil {
195+ sec = & corev1.Secret {}
196+ err := kubeClient .Get (ctx , client.ObjectKey {Namespace : ns , Name : secSource .Name }, sec )
197+ if secSource .Optional && k8serrors .IsNotFound (err ) {
169198 return nil
170199 }
171200 if err != nil {
172201 return err
173202 }
174- secMap [cmSource .Name ] = cm
203+ secMap [secSource .Name ] = sec
175204 }
176- if cmSource .Key == "" {
177- child .MountSecret (cm )
205+ if secSource .Key == "" {
206+ child .MountSecret (sec )
178207 } else {
179- val , ok := cm .Data [cmSource .Key ]
180- if ! cmSource .Optional && ! ok {
181- return fmt .Errorf ("key %q not found in secret %q" , cmSource .Key , cmSource .Name )
208+ val , ok := sec .Data [secSource .Key ]
209+ if ! secSource .Optional && ! ok {
210+ return fmt .Errorf ("key %q not found in secret %q" , secSource .Key , secSource .Name )
182211 }
183212 if ! ok {
184213 return nil
185214 }
186215 child .Content = val
216+ child .Source = & k8senv.Source {
217+ Override : true ,
218+ Secret : & k8senv.MapKey {
219+ Namespace : sec .Namespace ,
220+ Name : sec .Name ,
221+ Key : secSource .Key ,
222+ },
223+ }
187224 }
188225 return nil
189226 case vf .Resource != nil :
@@ -195,6 +232,13 @@ func overrideFileValueFrom(ctx context.Context, kubeClient client.Client, child
195232 }
196233 if vfr .OutputKey == resOut .Output {
197234 child .Content = []byte (resOut .Value )
235+ child .Source = & k8senv.Source {
236+ Override : true ,
237+ SandboxResource : & k8senv.SandboxResource {
238+ Name : vfr .Name ,
239+ OutputKey : vfr .OutputKey ,
240+ },
241+ }
198242 return nil
199243 }
200244 if vfr .OutputKey != "" {
@@ -203,6 +247,13 @@ func overrideFileValueFrom(ctx context.Context, kubeClient client.Client, child
203247 // all resource outputs mounted.
204248 keyChild := child .Path (resOut .Output )
205249 keyChild .Content = []byte (resOut .Value )
250+ keyChild .Source = & k8senv.Source {
251+ Override : true ,
252+ SandboxResource : & k8senv.SandboxResource {
253+ Name : vfr .Name ,
254+ OutputKey : resOut .Output ,
255+ },
256+ }
206257 }
207258 return nil
208259
@@ -232,30 +283,18 @@ func noClobber(errOut io.Writer, files *k8senv.Files, base string) (bool, error)
232283 if err != nil {
233284 return false , err
234285 }
235- fmt .Fprintf (errOut , "%s already exists, skipping\n " , base )
286+ fmt .Fprintf (errOut , "WARNING: %s already exists, skipping\n " , base )
236287 return true , nil
237288}
238289
239- func writeGCFiles (files * k8senv.Files , base string ) error {
240- if files .IsDir () {
241- for k , child := range files .Children {
242- if err := writeGCFiles (child , filepath .Join (base , k )); err != nil {
243- return err
244- }
245- }
246- return nil
247- }
248- return utils .RegisterPathForGC (base )
249- }
250-
251290var (
252291 vBar []byte = []byte ("│ " )
253292 highL []byte = []byte ("└── " )
254293 turnStyle []byte = []byte ("├── " )
255294 space []byte = []byte (strings .Repeat (" " , 4 ))
256295)
257296
258- func printTree (out io.Writer , files * k8senv.Files , ended []bool ) error {
297+ func printTree (out io.Writer , files * k8senv.Files , base string , ended []bool ) error {
259298 var err error
260299 for i , b := range ended {
261300 if i < len (ended )- 1 {
@@ -273,7 +312,12 @@ func printTree(out io.Writer, files *k8senv.Files, ended []bool) error {
273312 return err
274313 }
275314 }
276- _ , err = out .Write ([]byte (files .Name + "\n " ))
315+ src := "\t #"
316+ if files .Source != nil {
317+ src = fmt .Sprintf ("\t # %s" , files .Source )
318+ }
319+
320+ _ , err = fmt .Fprintf (out , "%s%s\n " , files .Name , src )
277321 if err != nil {
278322 return err
279323 }
@@ -283,7 +327,7 @@ func printTree(out io.Writer, files *k8senv.Files, ended []bool) error {
283327 keys := slices .Sorted (maps .Keys (files .Children ))
284328 for _ , k := range keys {
285329 n ++
286- if err := printTree (out , files .Children [k ], append (ended , n == N )); err != nil {
330+ if err := printTree (out , files .Children [k ], filepath . Join ( base , k ), append (ended , n == N )); err != nil {
287331 return err
288332 }
289333 }
0 commit comments