-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
218 lines (185 loc) · 5.23 KB
/
Copy pathmain.go
File metadata and controls
218 lines (185 loc) · 5.23 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
package main
import (
"context"
"flag"
"fmt"
"io"
"net/url"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strings"
"time"
"bootstrap/internal/s3client"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func parseS3URL(s3URL string) (bucket, key string, err error) {
u, err := url.Parse(s3URL)
if err != nil {
return "", "", fmt.Errorf("invalid S3 URL: %w", err)
}
if u.Scheme != "s3" {
return "", "", fmt.Errorf("not an S3 URL (should start with s3://)")
}
return u.Host, strings.TrimPrefix(u.Path, "/"), nil
}
func executeFile(path string) error {
cmd := &exec.Cmd{}
if runtime.GOOS == "windows" {
// On Windows, try to detect if it's a script that needs an interpreter
ext := strings.ToLower(filepath.Ext(path))
switch ext {
case ".bat", ".cmd":
cmd = exec.Command("cmd", "/C", path)
case ".ps1":
cmd = exec.Command("powershell", "-File", path)
case ".py":
cmd = exec.Command("python", path)
default:
// For .exe and other executables
cmd = exec.Command(path)
}
} else {
// On Unix systems, execute directly
cmd = exec.Command(path)
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func shutdownSystem(duration time.Duration, debug bool) error {
var cmd *exec.Cmd
time.Sleep(duration)
switch runtime.GOOS {
case "windows":
cmd = exec.Command("shutdown", "/s", "/t", "0")
default: // Linux and others
cmd = exec.Command("sudo", "shutdown", "-h", "now")
}
if debug {
fmt.Printf("Debug: Would execute command: %v\n", cmd.Args)
return nil
}
return cmd.Run()
}
func main() {
ctx := context.Background()
// Create cancellable context for signal handling
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Setup signal handling
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)
// Create temp file path holder
var tmpPath string
// Goroutine for signal handling
go func() {
<-sigChan
if tmpPath != "" {
os.Remove(tmpPath)
}
cancel()
os.Exit(1)
}()
saveFlag := flag.String("save", "", "Save the downloaded file to the specified path instead of a temporary location")
execFlag := flag.Bool("exec", false, "Execute the downloaded file")
postExecFlag := flag.String("post-exec", "", "Action to take after execution (only used with --exec). Valid values: shutdown")
debugFlag := flag.Bool("debug", false, "Debug mode - skips post-exec actions")
flag.Parse()
if *postExecFlag != "" && !*execFlag {
fmt.Fprintf(os.Stderr, "Error: --post-exec can only be used with --exec\n")
os.Exit(1)
}
if *postExecFlag != "" && *postExecFlag != "shutdown" {
fmt.Fprintf(os.Stderr, "Error: invalid --post-exec value. Valid values: shutdown\n")
os.Exit(1)
}
args := flag.Args()
if len(args) != 1 {
fmt.Fprintf(os.Stderr, "Usage: %s [--exec] [--save path] s3://bucket/path/to/file\n", os.Args[0])
os.Exit(1)
}
bucket, key, err := parseS3URL(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing S3 URL: %v\n", err)
os.Exit(1)
}
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to load AWS config: %v\n", err)
os.Exit(1)
}
client := s3.NewFromConfig(cfg)
ctxDownload, cancelDownload := context.WithTimeout(ctx, 30*time.Second)
defer cancelDownload()
result, err := s3client.Download(ctxDownload, client, bucket, key)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
defer result.Close()
var targetPath string
var targetFile *os.File
if *saveFlag != "" {
// Use the specified save path
targetPath = *saveFlag
// Create parent directories if they don't exist
dir := filepath.Dir(targetPath)
if err := os.MkdirAll(dir, 0755); err != nil {
fmt.Fprintf(os.Stderr, "Error creating directories: %v\n", err)
os.Exit(1)
}
// Create the target file
targetFile, err = os.Create(targetPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating file: %v\n", err)
os.Exit(1)
}
} else {
// Create temp file with original extension if possible
targetFile, err = os.CreateTemp("", "bootstrap-*-"+filepath.Base(key))
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating temporary file: %v\n", err)
os.Exit(1)
}
targetPath = targetFile.Name()
tmpPath = targetPath // Set tmpPath for cleanup handling
// Ensure cleanup of file if exec flag is set
if *execFlag {
defer os.Remove(targetPath)
}
}
if _, err := io.Copy(targetFile, result); err != nil {
fmt.Fprintf(os.Stderr, "Error copying S3 object to file: %v\n", err)
os.Exit(1)
}
// Make file executable on Unix systems
if runtime.GOOS != "windows" {
if err := targetFile.Chmod(0755); err != nil {
fmt.Fprintf(os.Stderr, "Error making file executable: %v\n", err)
os.Exit(1)
}
}
targetFile.Close()
if *execFlag {
var exitStatus int
if err := executeFile(targetPath); err != nil {
fmt.Fprintf(os.Stderr, "Error executing file: %v\n", err)
exitStatus = 1
}
if *postExecFlag == "shutdown" {
fmt.Println("System will shutdown in 20 seconds...")
if err := shutdownSystem(time.Duration(20)*time.Second, *debugFlag); err != nil {
fmt.Fprintf(os.Stderr, "Error initiating shutdown: %v\n", err)
exitStatus = 1
}
}
os.Exit(exitStatus)
} else {
fmt.Println(targetPath)
}
}