Skip to content

Commit 8e81a50

Browse files
committed
post-build: refactor to use flag package
1 parent a27b134 commit 8e81a50

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

extra/post_build_tool/main.go

+18-20
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
package main
22

33
import (
4+
"flag"
45
"fmt"
56
"os"
67
)
78

89
func main() {
9-
if len(os.Args) < 2 {
10-
fmt.Println("Please provide a filename")
10+
var output = flag.String("output", "", "Output to a specific file (default: add .dfu suffix)")
11+
var debug = flag.Bool("debug", false, "Enable debugging mode")
12+
var linked = flag.Bool("prelinked", false, "Provided file has already been linked to Zephyr")
13+
14+
flag.Parse()
15+
if flag.NArg() != 1 {
16+
fmt.Printf("Usage: %s [flags] <filename>\n", os.Args[0])
17+
flag.PrintDefaults()
1118
return
1219
}
13-
14-
filename := os.Args[1]
15-
debug := 0
16-
linked := 0
17-
18-
if len(os.Args) >= 3 {
19-
if os.Args[2] == "debug" {
20-
debug = 1
21-
}
22-
if os.Args[2] == "linked" {
23-
linked = 1
24-
}
25-
}
20+
filename := flag.Arg(0)
2621

2722
// Read the file content
2823
content, err := os.ReadFile(filename)
@@ -34,17 +29,20 @@ func main() {
3429
// Get the length of the file content
3530
length := len(content)
3631

37-
// Create a new filename for the copy
38-
newFilename := filename + ".dfu"
39-
4032
// Create the new content with the length in front
4133
len_str := fmt.Sprintf("%d", length)
42-
newContent := append([]byte(len_str), 0, byte(debug), byte(linked))
34+
newContent := append([]byte(len_str), 0, byte(*debug), byte(*linked))
4335
// make newContent 16 bytes
4436
tmp := make([]byte, 16-len(newContent))
4537
newContent = append(newContent, tmp...)
4638
newContent = append(newContent, content...)
4739

40+
// Create a new filename for the copy
41+
newFilename := *output
42+
if newFilename == "" {
43+
newFilename = filename + ".dfu"
44+
}
45+
4846
// Write the new content to the new file
4947
err = os.WriteFile(newFilename, []byte(newContent), 0644)
5048
if err != nil {
@@ -58,5 +56,5 @@ func main() {
5856
return
5957
}
6058

61-
fmt.Printf("File copied and saved as %s\n", newFilename)
59+
fmt.Printf("File %s saved as %s\n", filename, newFilename)
6260
}

0 commit comments

Comments
 (0)