@@ -3,11 +3,8 @@ package adj
33import (
44 "encoding/json"
55 "os"
6- "path"
76
87 "github.com/HyperloopUPV-H8/h9-backend/internal/utils"
9- "github.com/go-git/go-git/v5"
10- "github.com/go-git/go-git/v5/plumbing"
118)
129
1310const (
@@ -90,245 +87,3 @@ func downloadADJ(AdjBranch string) (json.RawMessage, json.RawMessage, error) {
9087
9188 return info , boardsList , nil
9289}
93-
94- // WARNING: Doing tricks on it
95- func updateRepo (AdjBranch string ) error {
96- var repo * git.Repository
97- var err error
98-
99- if AdjBranch == "" {
100- // Makes use of submodule
101- return nil
102- } else {
103- if _ , err = os .Stat (RepoPath ); os .IsNotExist (err ) {
104- repo , err = git .PlainClone (RepoPath , false , & git.CloneOptions {
105- URL : RepoUrl ,
106- ReferenceName : plumbing .NewBranchReferenceName (AdjBranch ),
107- SingleBranch : true ,
108- Depth : 1 ,
109- })
110- if err != nil {
111- return err
112- }
113- } else {
114- repo , err = git .PlainOpen (RepoPath )
115- if err != nil {
116- return err
117- }
118- }
119-
120- err = repo .Fetch (& git.FetchOptions {
121- RemoteName : "origin" ,
122- Force : true ,
123- })
124- if err != nil && err != git .NoErrAlreadyUpToDate {
125- return err
126- }
127-
128- head , err := repo .Head ()
129- if err != nil {
130- return err
131- }
132-
133- branch := head .Name ().Short ()
134-
135- worktree , err := repo .Worktree ()
136- if err != nil {
137- return err
138- }
139-
140- if branch != AdjBranch {
141- localBranchRef := plumbing .NewBranchReferenceName (AdjBranch )
142- _ , err = repo .Reference (localBranchRef , false )
143- if err != nil {
144- remoteBranchRef := plumbing .NewRemoteReferenceName ("origin" , AdjBranch )
145- remoteRef , err2 := repo .Reference (remoteBranchRef , true )
146- if err2 != nil {
147- return err2
148- }
149-
150- err = worktree .Checkout (& git.CheckoutOptions {
151- Branch : localBranchRef ,
152- Create : true ,
153- Force : true ,
154- Hash : remoteRef .Hash (),
155- })
156- if err != nil {
157- println (err .Error ())
158- return err
159- }
160- } else {
161- err = worktree .Checkout (& git.CheckoutOptions {
162- Branch : localBranchRef ,
163- Force : true ,
164- })
165- }
166- }
167-
168- err = worktree .Pull (& git.PullOptions {
169- RemoteName : "origin" ,
170- SingleBranch : true ,
171- })
172- if err != nil {
173- if err == git .NoErrAlreadyUpToDate {
174- return nil
175- } else {
176- return err
177- }
178- }
179- }
180-
181- return nil
182- }
183-
184- func getBoards (boardsList map [string ]string ) (map [string ]Board , error ) {
185- boards := make (map [string ]Board , len (boardsList ))
186- for boardName , boardPath := range boardsList {
187- fullPath := path .Join (RepoPath , boardPath )
188- boardRaw , err := os .ReadFile (fullPath )
189- if err != nil {
190- return nil , err
191- }
192-
193- var boardJSON BoardJSON
194- if err = json .Unmarshal (boardRaw , & boardJSON ); err != nil {
195- return nil , err
196- }
197-
198- measPathsFr := make ([]string , 0 )
199- for _ , measPath := range boardJSON .MeasurementsPaths {
200- measPathsFr = append (measPathsFr , path .Join (RepoPath , "boards" , boardName , measPath ))
201- }
202- boardJSON .MeasurementsPaths = measPathsFr
203-
204- packetPathsFr := make ([]string , 0 )
205- for _ , packetPath := range boardJSON .PacketsPaths {
206- packetPathsFr = append (packetPathsFr , path .Join (RepoPath , "boards" , boardName , packetPath ))
207- }
208- boardJSON .PacketsPaths = packetPathsFr
209-
210- board := Board {
211- Name : boardName ,
212- IP : boardJSON .IP ,
213- }
214- board .Packets , err = getBoardPackets (boardJSON .PacketsPaths )
215- if err != nil {
216- return nil , err
217- }
218-
219- board .Measurements , err = getBoardMeasurements (boardJSON .MeasurementsPaths )
220- if err != nil {
221- return nil , err
222- }
223- board .LookUpMeasurements = make (map [string ]Measurement , len (board .Measurements ))
224-
225- for _ , measurement := range board .Measurements {
226- board .LookUpMeasurements [measurement .Id ] = measurement
227- }
228- board .Structures = getBoardStructures (board )
229-
230- boards [boardName ] = board
231- }
232-
233- return boards , nil
234- }
235-
236- func getBoardPackets (packetsPaths []string ) ([]Packet , error ) {
237- packets := make ([]Packet , 0 )
238- for _ , packetPath := range packetsPaths {
239- if _ , err := os .Stat (packetPath ); os .IsNotExist (err ) {
240- continue
241- }
242-
243- packetRaw , err := os .ReadFile (packetPath )
244- if err != nil {
245- return nil , err
246- }
247-
248- // Magic happens here
249- type PacketJSON struct {
250- Packet []Packet `json:"packets"`
251- }
252-
253- packetsJSON := PacketJSON {}
254- if err = json .Unmarshal (packetRaw , & packetsJSON ); err != nil {
255- return nil , err
256- }
257- for _ , packetTMP := range packetsJSON .Packet {
258- packets = append (packets , packetTMP )
259- }
260- }
261-
262- return packets , nil
263- }
264-
265- func getBoardMeasurements (measurementsPaths []string ) ([]Measurement , error ) {
266- measurements := make ([]Measurement , 0 )
267-
268- for _ , measurementPath := range measurementsPaths {
269- if _ , err := os .Stat (measurementPath ); os .IsNotExist (err ) {
270- continue
271- }
272-
273- measurementRaw , err := os .ReadFile (measurementPath )
274- if err != nil {
275- return nil , err
276- }
277-
278- // Absolutely doing tricks on it AGAIN - @msanlli
279- type MeasurementJSON struct {
280- Measurements []Measurement `json:"measurements"`
281- }
282-
283- measurementsJSON := MeasurementJSON {}
284- if err = json .Unmarshal (measurementRaw , & measurementsJSON ); err != nil {
285- return nil , err
286- }
287- for _ , measurementTMP := range measurementsJSON .Measurements {
288- measurements = append (measurements , measurementTMP )
289- }
290- }
291-
292- return measurements , nil
293- }
294-
295- func getBoardIds (boards map [string ]string ) (map [string ]uint16 , error ) {
296- boardIds := make (map [string ]uint16 , len (boards ))
297- for boardName , boardPath := range boards {
298- fullPath := path .Join (RepoPath , boardPath )
299- boardRaw , err := os .ReadFile (fullPath )
300- if err != nil {
301- return nil , err
302- }
303-
304- var boardJSON BoardJSON
305- if err = json .Unmarshal (boardRaw , & boardJSON ); err != nil {
306- return nil , err
307- }
308-
309- boardIds [boardName ] = boardJSON .ID
310- }
311-
312- return boardIds , nil
313- }
314-
315- func getBoardStructures (board Board ) []Structure {
316- structures := make ([]Structure , len (board .Packets ))
317- for i , packet := range board .Packets {
318- structures [i ] = Structure {
319- Packet : packet ,
320- Measurements : board .Measurements ,
321- }
322- }
323-
324- return structures
325- }
326-
327- func getAddresses (boards map [string ]Board ) (map [string ]string , error ) {
328- addresses := make (map [string ]string , len (boards ))
329- for boardName , board := range boards {
330- addresses [boardName ] = board .IP
331- }
332-
333- return addresses , nil
334- }
0 commit comments