@@ -7,11 +7,12 @@ package zip
7
7
import (
8
8
"archive/zip"
9
9
"context"
10
- "fmt "
10
+ "io "
11
11
"os"
12
- "os/exec"
13
12
"path/filepath"
14
13
14
+ "github.com/hashicorp/go-multierror"
15
+
15
16
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors"
16
17
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact"
17
18
)
@@ -47,7 +48,7 @@ func (i *Installer) Install(_ context.Context, programName, version, installDir
47
48
os .RemoveAll (installDir )
48
49
}
49
50
50
- if err := i .unzip (artifactPath , programName , version ); err != nil {
51
+ if err := i .unzip (artifactPath ); err != nil {
51
52
return err
52
53
}
53
54
@@ -67,14 +68,59 @@ func (i *Installer) Install(_ context.Context, programName, version, installDir
67
68
return nil
68
69
}
69
70
70
- func (i * Installer ) unzip (artifactPath , programName , version string ) error {
71
- if _ , err := os .Stat (artifactPath ); err != nil {
72
- return errors .New (fmt .Sprintf ("artifact for '%s' version '%s' could not be found at '%s'" , programName , version , artifactPath ), errors .TypeFilesystem , errors .M (errors .MetaKeyPath , artifactPath ))
71
+ func (i * Installer ) unzip (artifactPath string ) error {
72
+ r , err := zip .OpenReader (artifactPath )
73
+ if err != nil {
74
+ return err
75
+ }
76
+ defer r .Close ()
77
+
78
+ if err := os .MkdirAll (i .config .InstallPath , 0755 ); err != nil && ! os .IsExist (err ) {
79
+ // failed to create install dir
80
+ return err
81
+ }
82
+
83
+ unpackFile := func (f * zip.File ) (err error ) {
84
+ rc , err := f .Open ()
85
+ if err != nil {
86
+ return err
87
+ }
88
+ defer func () {
89
+ if cerr := rc .Close (); cerr != nil {
90
+ err = multierror .Append (err , cerr )
91
+ }
92
+ }()
93
+
94
+ path := filepath .Join (i .config .InstallPath , f .Name )
95
+
96
+ if f .FileInfo ().IsDir () {
97
+ os .MkdirAll (path , f .Mode ())
98
+ } else {
99
+ os .MkdirAll (filepath .Dir (path ), f .Mode ())
100
+ f , err := os .OpenFile (path , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , f .Mode ())
101
+ if err != nil {
102
+ return err
103
+ }
104
+ defer func () {
105
+ if cerr := f .Close (); cerr != nil {
106
+ err = multierror .Append (err , cerr )
107
+ }
108
+ }()
109
+
110
+ if _ , err = io .Copy (f , rc ); err != nil {
111
+ return err
112
+ }
113
+ }
114
+ return nil
73
115
}
74
116
75
- powershellArg := fmt .Sprintf ("Expand-Archive -LiteralPath \" %s\" -DestinationPath \" %s\" " , artifactPath , i .config .InstallPath )
76
- installCmd := exec .Command ("powershell" , "-command" , powershellArg )
77
- return installCmd .Run ()
117
+ for _ , f := range r .File {
118
+ if err := unpackFile (f ); err != nil {
119
+ return err
120
+ }
121
+ }
122
+
123
+ return nil
78
124
}
79
125
80
126
// retrieves root directory from zip archive
0 commit comments