@@ -10,61 +10,112 @@ import (
10
10
"os"
11
11
"os/exec"
12
12
"path/filepath"
13
+ "strings"
13
14
14
15
"go.githedgehog.com/fabric/pkg/util/logutil"
15
16
fabapi "go.githedgehog.com/fabricator/api/fabricator/v1beta1"
16
17
"go.githedgehog.com/fabricator/api/meta"
17
18
)
18
19
19
20
const (
20
- BashCompletionRef = "bash-completion"
21
- TarballName = "bash-completion.tar.xz"
22
- InstallDir = "/opt/bash-completion"
23
- BashCompletionVersion = "2.11"
24
- TempExtractDir = "/tmp/bash-completion-2.11"
25
- CompletionsDir = InstallDir + "/completions"
26
- HookFilename = "kubectl-fabric-hook.sh"
27
- ProfileDir = "/etc/profile.d"
28
- ProfileFilename = "bash-completion.sh"
21
+ BashCompletionRef = "bash-completion"
22
+ TarballName = "bash-completion.tar.xz"
23
+ InstallDir = "/opt/bash-completion"
24
+ CompletionsDir = InstallDir + "/completions"
25
+ HookFilename = "kubectl-fabric-hook.sh"
26
+ ProfileDir = "/etc/profile.d"
27
+ ProfileFilename = "bash-completion.sh"
29
28
)
30
29
31
30
func Version (f fabapi.Fabricator ) meta.Version {
32
31
return f .Status .Versions .Platform .BashCompletion
33
32
}
34
33
35
- func Install (ctx context.Context , workDir string ) error {
34
+ func Install (ctx context.Context , workDir string , fab fabapi. Fabricator ) error {
36
35
slog .Info ("Installing bash-completion" )
37
36
37
+ versionStr := strings .TrimPrefix (string (Version (fab )), "v" )
38
+
39
+ tempExtractDir := fmt .Sprintf ("/tmp/bash-completion-%s" , versionStr )
40
+
41
+ slog .Info ("Using bash-completion version" , "version" , versionStr , "extractDir" , tempExtractDir )
42
+
38
43
if err := os .MkdirAll (InstallDir , 0o755 ); err != nil {
39
44
return fmt .Errorf ("creating bash-completion dir %q: %w" , InstallDir , err )
40
45
}
41
46
42
47
tarballPath := filepath .Join (workDir , TarballName )
43
- cmd := exec .CommandContext (ctx , "tar" , "-xf" , tarballPath , "-C" , "/tmp" )
48
+
49
+ if _ , err := os .Stat (tarballPath ); os .IsNotExist (err ) {
50
+ return fmt .Errorf ("tarball not found at %s" , tarballPath ) //nolint:goerr113
51
+ }
52
+
53
+ listCmd := exec .CommandContext (ctx , "tar" , "-tvf" , tarballPath )
54
+ listCmd .Dir = workDir
55
+ listCmd .Stdout = logutil .NewSink (ctx , slog .Debug , "tarball contents: " )
56
+ listCmd .Stderr = logutil .NewSink (ctx , slog .Debug , "tarball error: " )
57
+ if err := listCmd .Run (); err != nil {
58
+ return fmt .Errorf ("listing tarball contents: %w" , err )
59
+ }
60
+
61
+ slog .Info ("Extracting bash-completion tarball" , "path" , tarballPath )
62
+ cmd := exec .CommandContext (ctx , "tar" , "-xvf" , tarballPath , "-C" , "/tmp" )
44
63
cmd .Dir = workDir
45
- cmd .Stdout = logutil .NewSink (ctx , slog .Debug , "bash-completion: " )
46
- cmd .Stderr = logutil .NewSink (ctx , slog .Debug , "bash-completion: " )
64
+ cmd .Stdout = logutil .NewSink (ctx , slog .Debug , "bash-completion extract : " )
65
+ cmd .Stderr = logutil .NewSink (ctx , slog .Debug , "bash-completion extract error : " )
47
66
if err := cmd .Run (); err != nil {
48
67
return fmt .Errorf ("extracting bash-completion: %w" , err )
49
68
}
50
69
70
+ if _ , err := os .Stat (tempExtractDir ); os .IsNotExist (err ) {
71
+ lsCmd := exec .CommandContext (ctx , "ls" , "-la" , "/tmp" )
72
+ lsCmd .Stdout = logutil .NewSink (ctx , slog .Info , "tmp contents: " )
73
+ if err := lsCmd .Run (); err != nil {
74
+ slog .Debug ("Failed to list /tmp directory" , "error" , err )
75
+ }
76
+
77
+ return fmt .Errorf ("extracted directory %s does not exist" , tempExtractDir ) //nolint:goerr113
78
+ }
79
+
80
+ lsCmd := exec .CommandContext (ctx , "ls" , "-la" , tempExtractDir )
81
+ lsCmd .Stdout = logutil .NewSink (ctx , slog .Info , "extracted contents: " )
82
+ if err := lsCmd .Run (); err != nil {
83
+ slog .Debug ("Failed to list extracted directory" , "error" , err )
84
+ }
85
+
51
86
if err := os .MkdirAll (CompletionsDir , 0o755 ); err != nil {
52
87
return fmt .Errorf ("creating completions dir: %w" , err )
53
88
}
54
89
55
- srcCompletions := filepath .Join (TempExtractDir , "completions" )
56
- cmd = exec .CommandContext (ctx , "cp" , "-r" , srcCompletions , InstallDir )
90
+ srcCompletions := filepath .Join (tempExtractDir , "completions" )
91
+
92
+ if _ , err := os .Stat (srcCompletions ); os .IsNotExist (err ) {
93
+ return fmt .Errorf ("source completions directory %s does not exist" , srcCompletions ) //nolint:goerr113
94
+ }
95
+
96
+ slog .Info ("Copying bash completions" , "from" , srcCompletions , "to" , InstallDir )
97
+ cmd = exec .CommandContext (ctx , "cp" , "-rv" , srcCompletions , InstallDir )
98
+ cmd .Stdout = logutil .NewSink (ctx , slog .Debug , "cp: " )
99
+ cmd .Stderr = logutil .NewSink (ctx , slog .Debug , "cp error: " )
57
100
if err := cmd .Run (); err != nil {
58
- return fmt .Errorf ("copying bash-completion files: %w" , err )
101
+ return fmt .Errorf ("copying bash-completion files: %w" , err ) //nolint:goerr113
102
+ }
103
+
104
+ srcBashCompletion := filepath .Join (tempExtractDir , "bash_completion" )
105
+
106
+ if _ , err := os .Stat (srcBashCompletion ); os .IsNotExist (err ) {
107
+ return fmt .Errorf ("source bash_completion file %s does not exist" , srcBashCompletion ) //nolint:goerr113
59
108
}
60
109
61
- srcBashCompletion := filepath .Join (TempExtractDir , "bash_completion" )
62
- cmd = exec .CommandContext (ctx , "cp" , srcBashCompletion , InstallDir )
110
+ slog .Info ("Copying bash_completion file" , "from" , srcBashCompletion , "to" , InstallDir )
111
+ cmd = exec .CommandContext (ctx , "cp" , "-v" , srcBashCompletion , InstallDir )
112
+ cmd .Stdout = logutil .NewSink (ctx , slog .Debug , "cp: " )
113
+ cmd .Stderr = logutil .NewSink (ctx , slog .Debug , "cp error: " )
63
114
if err := cmd .Run (); err != nil {
64
115
return fmt .Errorf ("copying bash_completion file: %w" , err )
65
116
}
66
117
67
- if err := os .RemoveAll (TempExtractDir ); err != nil {
118
+ if err := os .RemoveAll (tempExtractDir ); err != nil {
68
119
slog .Warn ("Failed to clean up bash-completion tmp files" , "error" , err )
69
120
}
70
121
0 commit comments