Skip to content

Commit 57f3626

Browse files
committed
o/h/ctlcmd, daemon: improve structure comments
1 parent 3b6a0d0 commit 57f3626

3 files changed

Lines changed: 19 additions & 12 deletions

File tree

daemon/api_general_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,8 +1026,7 @@ func (s *generalSuite) TestStateChange(c *check.C) {
10261026
// and doesn't cause the whole task to be missing from the output
10271027
t2.Set("snap-setup", "some-snap")
10281028

1029-
chg := st.Change(chg1.ID())
1030-
chg.Set("api-data", map[string]int{"n": 42})
1029+
chg1.Set("api-data", map[string]int{"n": 42})
10311030
st.Unlock()
10321031

10331032
// Execute

overlord/hookstate/ctlcmd/helpers.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,31 @@ const snapctlDebounceWindow = 200 * time.Millisecond
7171
// properly organize the hook tasks in the chain of tasks in the change.
7272
const finalSeedTask = "mark-seeded"
7373

74+
// ChangeInfo represents the information about a change that is needed for JSON marshaling.
7475
// To unmarshal the data needed for `snap tasks`, we need access
7576
// to all struct fields that client.Change has. However, Change.data is an
7677
// unexported field, so we cannot unmarshal into it directly. There is a
7778
// changeAndData struct in the client package with all exported fields, but the
7879
// struct itself is not exported.
7980
type ChangeInfo struct {
80-
ID string `json:"id"`
81-
Kind string `json:"kind"`
82-
Summary string `json:"summary"`
83-
Status string `json:"status"`
81+
ID string `json:"id"`
82+
Kind string `json:"kind"`
83+
Summary string `json:"summary"`
84+
Status string `json:"status"`
8485
Tasks []TaskInfo `json:"tasks,omitempty"`
85-
Ready bool `json:"ready"`
86-
Err string `json:"err,omitempty"`
86+
Ready bool `json:"ready"`
87+
Err string `json:"err,omitempty"`
8788

8889
SpawnTime time.Time `json:"spawn-time,omitzero"`
8990
ReadyTime *time.Time `json:"ready-time,omitempty"`
9091

9192
Data map[string]*json.RawMessage `json:"data,omitempty"`
9293
}
9394

95+
// TaskInfo represents the information about a task that is needed for JSON marshaling
96+
// for `snap tasks` output. It includes fields such as ID, kind, summary, status, and
97+
// any additional data associated with the task. The Data field is a map of string keys
98+
// to raw JSON messages, allowing for flexible inclusion of task-specific data in the output.
9499
type TaskInfo struct {
95100
ID string `json:"id"`
96101
Kind string `json:"kind"`
@@ -105,15 +110,18 @@ type TaskInfo struct {
105110
Data map[string]*json.RawMessage `json:"data,omitempty"`
106111
}
107112

113+
// TaskInfoProgress represents the progress of a task, including a label and the
114+
// amount of work done out of the total. Used in the TaskInfo struct to provide
115+
// progress information for tasks in the `snap tasks` output.
108116
type TaskInfoProgress struct {
109117
Label string `json:"label"`
110118
Done int `json:"done"`
111119
Total int `json:"total"`
112120
}
113121

114-
// To unmarshal the data needed for `snap tasks`, we need access
115-
// to all struct fields that client.Change has. However, due to unexported
116-
// fields, we use ChangeInfo instead.
122+
// StateChangeToChangeInfo converts a state.Change to a ChangeInfo struct which has all
123+
// the exported fields needed for JSON marshaling for `snap tasks` output. It also extracts
124+
// the "api-data" from the change and includes it in the ChangeInfo.Data field.
117125
func StateChangeToChangeInfo(chg *state.Change) *ChangeInfo {
118126
status := chg.Status()
119127
chgInfo := &ChangeInfo{

overlord/hookstate/ctlcmd/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ package ctlcmd_test
2222
import (
2323
"strings"
2424

25-
. "gopkg.in/check.v1"
2625
"github.com/snapcore/snapd/overlord/hookstate/ctlcmd"
2726
"github.com/snapcore/snapd/overlord/state"
2827
"github.com/snapcore/snapd/testutil"
28+
. "gopkg.in/check.v1"
2929
)
3030

3131
type helperSuite struct {

0 commit comments

Comments
 (0)