-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (136 loc) · 4.13 KB
/
main.go
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
// SPDX-License-Identifier: MPL-2.0
// Copyright (C) 2025 Arduino SA
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/codeclysm/extract/v4"
"github.com/go-git/go-git/v5"
)
func downloadFile(filepath string, url string) (err error) {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Check server response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}
// Writer the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func main() {
// Create a temporary folder, download a URL based on a git tag in it
// and extract the zip file to the temporary folder
tmpDir, err := os.MkdirTemp("", "sync_zephyr_artifacts")
if err != nil {
fmt.Println("Error creating temp dir:", err)
return
}
defer os.RemoveAll(tmpDir) // Clean up
// Download the zip file from http://downloads.arduino.cc/cores/zephyr/zephyr-core-llext-{git_tag}.zip
// and save it to zipFilePath
// Replace {git_tag} with the actual git tag
gitCorePath := os.Args[1]
// Force an hash, for debug only
forceHash := ""
if len(os.Args) > 2 {
forceHash = os.Args[2]
}
r, err := git.PlainOpen(gitCorePath)
if err != nil {
fmt.Println("Error opening git repository:", err)
return
}
ref, err := r.Head()
if err != nil {
fmt.Println("Error getting git reference:", err)
return
}
w, err := r.Worktree()
if err != nil {
fmt.Println("Error getting git worktree:", err)
return
}
// Check if the repo contains modifications in either variants or firmwares folders
status, err := w.StatusWithOptions(git.StatusOptions{Strategy: git.Preload})
if err != nil {
fmt.Println("Error getting git status:", err)
return
}
if !status.IsClean() {
// Check if there are untracked/modified files in the firmwares or variants folders
for path, s := range status {
if strings.HasPrefix(path, "firmwares/") || strings.HasPrefix(path, "variants/") {
fmt.Println("The git repository contains uncommitted changes in", path)
if s.Worktree != git.Untracked {
fmt.Println("Please stash them before running this script.")
}
return
}
}
}
fmt.Println("Git tag:", ref.Hash())
// Replace {git_tag} with the actual git tag in the URL
hash := ref.Hash().String()[0:7]
if forceHash != "" {
hash = forceHash
}
filename := fmt.Sprintf("arduino-core-zephyr-llext-%s.tar.bz2", hash)
url := fmt.Sprintf("http://downloads.arduino.cc/cores/zephyr/%s", filename)
fmt.Println("Download URL:", url)
// Download the zip file from the URL
zipFilePath := filepath.Join(tmpDir, filename)
err = downloadFile(zipFilePath, url)
if err != nil {
fmt.Println("Error downloading zip file:", err)
return
}
fmt.Println("Downloaded zip file to:", zipFilePath)
// Extract the tar.bz2 file to the temporary folder
// Use packer/tar and compress/bzip2 to extract the file
file, err := os.Open(filepath.Join(tmpDir, filename))
if err != nil {
fmt.Println("Error opening zip file:", err)
return
}
defer file.Close()
extract.Bz2(context.Background(), file, filepath.Join(tmpDir, "extract"), nil)
// Copy the content of firmware folder to gitCorePath/firmware
err = os.CopyFS(filepath.Join(gitCorePath, "firmwares"), os.DirFS(filepath.Join(tmpDir, "extract", "ArduinoCore-zephyr", "firmwares")))
if err != nil {
fmt.Println("Error copying firmware folder:", err)
return
}
// Copy the content of variants folder to gitCorePath/variants
// Since CopyFS does not overwrite, before doing so remove gitCorePath/variants
// TODO: make sure there's no outstanding change in gitCorePath/variants
// Bail out in that case with a clear error message
err = os.RemoveAll(filepath.Join(gitCorePath, "variants"))
if err != nil {
fmt.Println("Error renaming variants folder:", err)
return
}
err = os.CopyFS(filepath.Join(gitCorePath, "variants"), os.DirFS(filepath.Join(tmpDir, "extract", "ArduinoCore-zephyr", "variants")))
if err != nil {
fmt.Println("Error copying variants folder:", err)
return
}
}