-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstackpack_package.go
More file actions
300 lines (249 loc) · 8.63 KB
/
stackpack_package.go
File metadata and controls
300 lines (249 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package stackpack
import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/gurkankaymak/hocon"
"github.com/spf13/cobra"
"github.com/stackvista/stackstate-cli/internal/common"
"github.com/stackvista/stackstate-cli/internal/di"
)
const (
defaultDirMode = 0755 // Default directory permissions
)
// PackageArgs contains arguments for stackpack package command
type PackageArgs struct {
StackpackDir string
ArchiveFile string
Force bool
}
// StackpackInfo contains parsed stackpack metadata
type StackpackInfo struct {
Name string
Version string
}
// StackpackConfigParser interface for parsing stackpack configuration
type StackpackConfigParser interface {
Parse(filePath string) (*StackpackInfo, error)
}
// HoconParser implements StackpackConfigParser for HOCON format
type HoconParser struct{}
func (h *HoconParser) Parse(filePath string) (*StackpackInfo, error) {
// Read the file content
content, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
// Parse stackpack.conf content
conf, err := hocon.ParseString(string(content))
if err != nil {
return nil, fmt.Errorf("failed to parse stackpack.conf file: %w", err)
}
name := strings.Trim(conf.GetString("name"), `"`)
version := strings.Trim(conf.GetString("version"), `"`)
if name == "" {
return nil, fmt.Errorf("name not found in stackpack.conf")
}
if version == "" {
return nil, fmt.Errorf("version not found in stackpack.conf")
}
return &StackpackInfo{
Name: name,
Version: version,
}, nil
}
// YamlParser implements StackpackConfigParser for YAML format (future)
type YamlParser struct{}
func (y *YamlParser) Parse(filePath string) (*StackpackInfo, error) {
// TODO: Implement YAML parsing when format changes
return nil, fmt.Errorf("YAML format not yet implemented")
}
// Required files and directories for a valid stackpack
var requiredStackpackItems = []string{
"provisioning",
"README.md",
"resources",
"stackpack.conf",
}
// StackpackPackageCommand creates the package subcommand
func StackpackPackageCommand(cli *di.Deps) *cobra.Command {
args := &PackageArgs{}
cmd := &cobra.Command{
Use: "package",
Short: "Package a stackpack into a zip file",
Long: `Package a stackpack into a zip file.
Creates a zip file containing all required stackpack files and directories:
- provisioning/ (directory)
- README.md (file)
- resources/ (directory)
- stackpack.conf (file)
The zip file is named <stackpack_name>-<version>.zip where the name and
version are extracted from stackpack.conf and created in the current directory.`,
Example: `# Package stackpack in current directory
sts stackpack package
# Package specific stackpack directory
sts stackpack package -d ./my-stackpack
# Package with custom archive filename
sts stackpack package -f my-custom-archive.zip
# Force overwrite existing zip file
sts stackpack package --force`,
RunE: cli.CmdRunE(RunStackpackPackageCommand(args)),
}
cmd.Flags().StringVarP(&args.StackpackDir, "stackpack-directory", "d", "", "Path to stackpack directory (defaults to current directory)")
cmd.Flags().StringVarP(&args.ArchiveFile, "archive-file", "f", "", "Path to the zip file to create (defaults to <stackpack_name>-<version>.zip in current directory)")
cmd.Flags().BoolVar(&args.Force, "force", false, "Overwrite existing zip file without prompting")
return cmd
}
// RunStackpackPackageCommand executes the package command
func RunStackpackPackageCommand(args *PackageArgs) func(cli *di.Deps, cmd *cobra.Command) common.CLIError {
return func(cli *di.Deps, cmd *cobra.Command) common.CLIError {
// Set default stackpack directory
if args.StackpackDir == "" {
currentDir, err := os.Getwd()
if err != nil {
return common.NewRuntimeError(fmt.Errorf("failed to get current working directory: %w", err))
}
args.StackpackDir = currentDir
}
// Convert to absolute path
absStackpackDir, err := filepath.Abs(args.StackpackDir)
if err != nil {
return common.NewRuntimeError(fmt.Errorf("failed to get absolute path for stackpack directory: %w", err))
}
args.StackpackDir = absStackpackDir
// Parse stackpack.conf using HOCON parser to get name and version
parser := &HoconParser{}
stackpackInfo, err := parser.Parse(filepath.Join(args.StackpackDir, "stackpack.conf"))
if err != nil {
return common.NewRuntimeError(fmt.Errorf("failed to parse stackpack.conf: %w", err))
}
// Set default archive file path if not specified
if args.ArchiveFile == "" {
currentDir, err := os.Getwd()
if err != nil {
return common.NewRuntimeError(fmt.Errorf("failed to get current working directory: %w", err))
}
zipFileName := fmt.Sprintf("%s-%s.zip", stackpackInfo.Name, stackpackInfo.Version)
args.ArchiveFile = filepath.Join(currentDir, zipFileName)
} else {
// Convert to absolute path
absArchiveFile, err := filepath.Abs(args.ArchiveFile)
if err != nil {
return common.NewRuntimeError(fmt.Errorf("failed to get absolute path for archive file: %w", err))
}
args.ArchiveFile = absArchiveFile
}
// Validate stackpack directory
if err := validateStackpackDirectory(args.StackpackDir); err != nil {
return common.NewCLIArgParseError(err)
}
// Check if zip file exists and handle force flag
if _, err := os.Stat(args.ArchiveFile); err == nil && !args.Force {
return common.NewRuntimeError(fmt.Errorf("zip file already exists: %s (use --force to overwrite)", args.ArchiveFile))
}
// Create output directory if it doesn't exist
outputDir := filepath.Dir(args.ArchiveFile)
if err := os.MkdirAll(outputDir, os.FileMode(defaultDirMode)); err != nil {
return common.NewRuntimeError(fmt.Errorf("failed to create output directory: %w", err))
}
// Create zip file
if err := createStackpackZip(args.StackpackDir, args.ArchiveFile); err != nil {
return common.NewRuntimeError(fmt.Errorf("failed to create zip file: %w", err))
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"success": true,
"stackpack_name": stackpackInfo.Name,
"stackpack_version": stackpackInfo.Version,
"zip_file": args.ArchiveFile,
"source_dir": args.StackpackDir,
})
} else {
cli.Printer.Successf("Stackpack packaged successfully!")
cli.Printer.PrintLn("")
cli.Printer.PrintLn(fmt.Sprintf("Stackpack: %s (v%s)", stackpackInfo.Name, stackpackInfo.Version))
cli.Printer.PrintLn(fmt.Sprintf("Zip file: %s", args.ArchiveFile))
}
return nil
}
}
func validateStackpackDirectory(dir string) error {
for _, item := range requiredStackpackItems {
itemPath := filepath.Join(dir, item)
if _, err := os.Stat(itemPath); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("required stackpack item not found: %s", item)
}
return fmt.Errorf("failed to check stackpack item %s: %w", item, err)
}
}
return nil
}
func createStackpackZip(sourceDir, zipPath string) error {
zipFile, err := os.Create(zipPath)
if err != nil {
return fmt.Errorf("failed to create zip file: %w", err)
}
defer zipFile.Close()
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
// Add each required item to the zip
for _, item := range requiredStackpackItems {
itemPath := filepath.Join(sourceDir, item)
if err := addToZip(zipWriter, itemPath, item); err != nil {
return fmt.Errorf("failed to add %s to zip: %w", item, err)
}
}
return nil
}
func addToZip(zipWriter *zip.Writer, sourcePath, zipPath string) error {
fileInfo, err := os.Stat(sourcePath)
if err != nil {
return err
}
if fileInfo.IsDir() {
return addDirToZip(zipWriter, sourcePath, zipPath)
}
return addFileToZip(zipWriter, sourcePath, zipPath)
}
func addFileToZip(zipWriter *zip.Writer, sourcePath, zipPath string) error {
file, err := os.Open(sourcePath)
if err != nil {
return err
}
defer file.Close()
zipFileWriter, err := zipWriter.Create(zipPath)
if err != nil {
return err
}
_, err = io.Copy(zipFileWriter, file)
return err
}
func addDirToZip(zipWriter *zip.Writer, sourceDir, zipDir string) error {
return filepath.Walk(sourceDir, func(filePath string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}
// Get relative path from source directory
relPath, err := filepath.Rel(sourceDir, filePath)
if err != nil {
return err
}
// Skip the root directory itself
if relPath == "." {
return nil
}
// Create zip path with forward slashes for cross-platform compatibility
zipPath := filepath.ToSlash(filepath.Join(zipDir, relPath))
if fileInfo.IsDir() {
// Create directory entry in zip
_, err := zipWriter.Create(zipPath + "/")
return err
}
// Add file to zip
return addFileToZip(zipWriter, filePath, zipPath)
})
}