GitRoot

craft your forge, build your project, grow your community freely
 1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
 2//
 3// SPDX-License-Identifier: MIT
 4
 5package model
 6
 7import "github.com/tidwall/gjson"
 8
 9type Exec struct {
10	Build       string   `json:"build"` //image docker, path to Dockerfile
11	ReportStats bool     `json:"reportStats"`
12	Cmds        []Cmd    `json:"cmds"`
13	Env         []string `json:"env"`
14	Artifacts   []string `json:"artifacts"`
15	Cache       []Cache  `json:"cache"`
16}
17
18type Cmd struct {
19	Cmd  string   `json:"cmd"`
20	Args []string `json:"args"`
21}
22
23type Cache struct {
24	Key      string `json:"key"`
25	Path     string `json:"path"`
26	ReadOnly bool   `json:"readOnly"`
27}
28
29type ExecStatus struct {
30	CmdsExec   []string   `json:"cmdsExec"`
31	CmdsStatus []int      `json:"cmdsStatus"`
32	CmdsLogs   []string   `json:"cmdsLogs"`
33	CmdsStats  []CmdStats `json:"cmdsStats"`
34	Artifacts  []string   `json:"artifacts"`
35}
36
37type CmdStats struct {
38	MaxMemoryBytes  uint64 `json:"maxMemoryBytes"`
39	MaxThreads      int32  `json:"maxThreads"`
40	ReadBytesTotal  uint64 `json:"readBytesTotal"`
41	WriteBytesTotal uint64 `json:"writeBytesTotal"`
42	TotalCPUTimeMs  uint64 `json:"totalCpuTimeMs"`
43}
44
45func ExecStatusFromString(execStatus string) ExecStatus {
46	jsonStatus := gjson.Parse(execStatus)
47	res := ExecStatus{
48		CmdsExec:   []string{},
49		CmdsStatus: []int{},
50		CmdsLogs:   []string{},
51		CmdsStats:  []CmdStats{},
52	}
53	for _, j := range jsonStatus.Get("cmdsExec").Array() {
54		res.CmdsExec = append(res.CmdsExec, j.String())
55	}
56	for _, j := range jsonStatus.Get("cmdsStatus").Array() {
57		res.CmdsStatus = append(res.CmdsStatus, int(j.Int()))
58	}
59	for _, j := range jsonStatus.Get("cmdsLogs").Array() {
60		res.CmdsLogs = append(res.CmdsLogs, j.String())
61	}
62	for _, j := range jsonStatus.Get("cmdsStats").Array() {
63		res.CmdsStats = append(res.CmdsStats, CmdStats{
64			MaxMemoryBytes:  j.Get("maxMemoryBytes").Uint(),
65			MaxThreads:      int32(j.Get("maxThreads").Int()),
66			ReadBytesTotal:  j.Get("readBytesTotal").Uint(),
67			WriteBytesTotal: j.Get("writeBytesTotal").Uint(),
68			TotalCPUTimeMs:  j.Get("totalCpuTimeMs").Uint(),
69		})
70	}
71	return res
72}