Skip to content

Commit b54224e

Browse files
committed
Add --attr to supply additional attributes to create-fw-bundle
CL: mos: Add --attr to supply additional attributes to create-fw-bundle
1 parent 1cc0ea5 commit b54224e

File tree

6 files changed

+108
-30
lines changed

6 files changed

+108
-30
lines changed

common/fwbundle/fw_bundle.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,21 @@ type FirmwareBundle struct {
4040
tempDir string
4141
}
4242

43-
type FirmwareManifest struct {
43+
type firmwareManifest struct {
4444
Name string `json:"name,omitempty"`
4545
Platform string `json:"platform,omitempty"`
4646
Description string `json:"description,omitempty"`
4747
Version string `json:"version,omitempty"`
4848
BuildID string `json:"build_id,omitempty"`
4949
BuildTimestamp *time.Time `json:"build_timestamp,omitempty"`
5050
Parts map[string]*FirmwarePart `json:"parts"`
51+
52+
// Extra attributes.
53+
attrs map[string]interface{}
5154
}
5255

56+
type FirmwareManifest firmwareManifest
57+
5358
func NewBundle() *FirmwareBundle {
5459
return &FirmwareBundle{}
5560
}
@@ -146,3 +151,49 @@ func (fw *FirmwareBundle) Cleanup() {
146151
os.RemoveAll(fw.tempDir)
147152
}
148153
}
154+
155+
func (fwb *FirmwareBundle) SetAttr(attr string, value interface{}) {
156+
if fwb.FirmwareManifest.attrs == nil {
157+
fwb.FirmwareManifest.attrs = make(map[string]interface{})
158+
}
159+
fwb.FirmwareManifest.attrs[attr] = value
160+
}
161+
162+
func (fwm FirmwareManifest) MarshalJSON() ([]byte, error) {
163+
b, err := json.Marshal(firmwareManifest(fwm))
164+
if err != nil {
165+
return nil, err
166+
}
167+
if len(fwm.attrs) == 0 {
168+
return b, nil
169+
}
170+
eb, err := json.Marshal(fwm.attrs)
171+
if err != nil {
172+
return nil, err
173+
}
174+
eb[0] = ','
175+
rb := append(b[:len(b)-1], eb...)
176+
return rb, nil
177+
}
178+
179+
func (fwm FirmwareManifest) UnmarshalJSON(b []byte) error {
180+
// Start by filling in the struct fields.
181+
var fwm1 firmwareManifest
182+
if err := json.Unmarshal(b, &fwm1); err != nil {
183+
return err
184+
}
185+
*(&fwm) = FirmwareManifest(fwm1)
186+
// Re-parse as a generic map.
187+
var mp map[string]interface{}
188+
json.Unmarshal(b, &mp)
189+
// Find keys that are not fields in the struct and add them as attrs.
190+
for k, v := range mp {
191+
if !isJSONField(fwm, k) {
192+
if fwm.attrs == nil {
193+
fwm.attrs = make(map[string]interface{})
194+
}
195+
fwm.attrs[k] = v
196+
}
197+
}
198+
return nil
199+
}

common/fwbundle/fw_part.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ type firmwarePart struct {
5656
// Deprecated, not being used since 2018/12/03.
5757
CC3200FileSignature string `json:"sign,omitempty"`
5858

59-
// Other user-specified properties are preserved here.
60-
properties map[string]interface{}
59+
// Other user-specified attributes are preserved here.
60+
attrs map[string]interface{}
61+
6162
data []byte
6263
dataProvider DataProvider
6364
}
@@ -69,7 +70,7 @@ func PartFromString(ps string) (*FirmwarePart, error) {
6970
if len(np) < 2 {
7071
return nil, errors.Errorf("invalid part spec '%s', must be 'name:prop=value,...'", ps)
7172
}
72-
// Create properties JSON and re-parse it.
73+
// Create attrs JSON and re-parse it.
7374
m := make(map[string]interface{})
7475
for _, prop := range strings.Split(np[1], ",") {
7576
if len(prop) == 0 {
@@ -184,10 +185,10 @@ func (p *FirmwarePart) MarshalJSON() ([]byte, error) {
184185
if err != nil {
185186
return nil, err
186187
}
187-
if len(p.properties) == 0 {
188+
if len(p.attrs) == 0 {
188189
return b, nil
189190
}
190-
eb, err := json.Marshal(p.properties)
191+
eb, err := json.Marshal(p.attrs)
191192
if err != nil {
192193
return nil, err
193194
}
@@ -218,13 +219,13 @@ func (p *FirmwarePart) UnmarshalJSON(b []byte) error {
218219
// Re-parse as a generic map.
219220
var mp map[string]interface{}
220221
json.Unmarshal(b, &mp)
221-
// Find keys that are not fields in the struct and add them as properties.
222+
// Find keys that are not fields in the struct and add them as attrs.
222223
for k, v := range mp {
223224
if !isJSONField(p, k) {
224-
if p.properties == nil {
225-
p.properties = make(map[string]interface{})
225+
if p.attrs == nil {
226+
p.attrs = make(map[string]interface{})
226227
}
227-
p.properties[k] = v
228+
p.attrs[k] = v
228229
}
229230
}
230231
return nil

mos/common/common.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package moscommon
55

66
import (
77
"strings"
8+
9+
"github.com/cesanta/errors"
810
)
911

1012
const (
@@ -29,3 +31,17 @@ func ExpandPlaceholders(s, ps, ss string) string {
2931
}
3032
return res
3133
}
34+
35+
func ParseParamValues(args []string) (map[string]string, error) {
36+
ret := map[string]string{}
37+
for _, a := range args {
38+
// Split arg into two substring by "=" (so, param name name cannot contain
39+
// "=", but value can)
40+
subs := strings.SplitN(a, "=", 2)
41+
if len(subs) < 2 {
42+
return nil, errors.Errorf("missing value for %q", a)
43+
}
44+
ret[subs[0]] = subs[1]
45+
}
46+
return ret, nil
47+
}

mos/config/config.go

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ import (
2020
"context"
2121
"fmt"
2222
"sort"
23-
"strings"
2423
"time"
2524

26-
"github.com/mongoose-os/mos/mos/ourutil"
27-
"github.com/mongoose-os/mos/mos/dev"
28-
"github.com/mongoose-os/mos/mos/flags"
2925
"github.com/cesanta/errors"
3026
"github.com/golang/glog"
27+
moscommon "github.com/mongoose-os/mos/mos/common"
28+
"github.com/mongoose-os/mos/mos/dev"
29+
"github.com/mongoose-os/mos/mos/flags"
30+
"github.com/mongoose-os/mos/mos/ourutil"
3131
flag "github.com/spf13/pflag"
3232
)
3333

@@ -86,7 +86,7 @@ func SetWithArgs(
8686
return errors.Trace(err)
8787
}
8888

89-
paramValues, err := parseParamValues(args)
89+
paramValues, err := moscommon.ParseParamValues(args)
9090
if err != nil {
9191
return errors.Trace(err)
9292
}
@@ -168,20 +168,6 @@ func SetAndSave(ctx context.Context, devConn dev.DevConn, devConf *dev.DevConf)
168168
return SetAndSaveLevel(ctx, devConn, devConf, *flags.Level)
169169
}
170170

171-
func parseParamValues(args []string) (map[string]string, error) {
172-
ret := map[string]string{}
173-
for _, a := range args {
174-
// Split arg into two substring by "=" (so, param name name cannot contain
175-
// "=", but value can)
176-
subs := strings.SplitN(a, "=", 2)
177-
if len(subs) < 2 {
178-
return nil, errors.Errorf("missing value for %q", a)
179-
}
180-
ret[subs[0]] = subs[1]
181-
}
182-
return ret, nil
183-
}
184-
185171
func ApplyDiff(devConf *dev.DevConf, newConf map[string]string) error {
186172
ourutil.Reportf("\nUpdating config:")
187173
keys := []string{}

mos/create_fw_bundle/create_fw_bundle.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ import (
2222
"fmt"
2323
"io/ioutil"
2424
"path/filepath"
25+
"strconv"
2526
"strings"
2627

28+
moscommon "github.com/mongoose-os/mos/mos/common"
2729
"github.com/mongoose-os/mos/mos/dev"
2830
"github.com/mongoose-os/mos/mos/flags"
2931
"github.com/mongoose-os/mos/mos/version"
3032

33+
"github.com/cesanta/errors"
3134
"github.com/mongoose-os/mos/common/fwbundle"
3235
"github.com/mongoose-os/mos/mos/ourutil"
33-
"github.com/cesanta/errors"
3436
flag "github.com/spf13/pflag"
3537
)
3638

@@ -112,6 +114,26 @@ func CreateFWBundle(ctx context.Context, devConn dev.DevConn) error {
112114
}
113115
}
114116
}
117+
attrs, err := moscommon.ParseParamValues(*flags.Attr)
118+
if err != nil {
119+
return errors.Annotatef(err, "failed to parse --attr")
120+
}
121+
for attr, valueStr := range attrs {
122+
var value interface{}
123+
switch valueStr {
124+
case "true":
125+
value = true
126+
case "false":
127+
value = false
128+
default:
129+
if i, err := strconv.ParseInt(valueStr, 0, 64); err == nil {
130+
value = i
131+
} else {
132+
value = valueStr
133+
}
134+
}
135+
fwb.SetAttr(attr, value)
136+
}
115137
ourutil.Reportf("Writing %s", *flags.Output)
116138
return fwbundle.WriteZipFirmwareBundle(fwb, *flags.Output, *flags.Compress)
117139
}

mos/flags/flags.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ var (
8989

9090
KeepTempFiles = flag.Bool("keep-temp-files", false, "keep temp files after the build is done (by default they are in ~/.mos/tmp)")
9191
KeepFS = flag.Bool("keep-fs", false, "When flashing, skip the filesystem parts")
92+
93+
Attr = flag.StringArray("attr", []string{}, "manifest attribute, can be used multiple times")
9294
)
9395

9496
func Platform() string {

0 commit comments

Comments
 (0)