Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

schema_cache/
dist/

.DS_Store
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/blues/note-go v1.7.2
github.com/fatih/color v1.17.0
github.com/peterh/liner v1.2.2
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
golang.org/x/term v0.20.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:Om
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
github.com/shirou/gopsutil/v3 v3.21.6/go.mod h1:JfVbDpIBLVzT8oKbvMg9P3wEIMDDpVn+LwHTKj0ST88=
github.com/shirou/gopsutil/v3 v3.24.4 h1:dEHgzZXt4LMNm+oYELpzl9YCqV65Yr/6SfrvgRBtXeU=
github.com/shirou/gopsutil/v3 v3.24.4/go.mod h1:lTd2mdiOspcqLgAnr9/nGi71NkeMpWKdmhuxm9GusH8=
Expand Down
18 changes: 18 additions & 0 deletions lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type ConfigSettings struct {
HubCreds map[string]ConfigCreds `json:"creds,omitempty"`
Interface string `json:"interface,omitempty"`
IPort map[string]ConfigPort `json:"iport,omitempty"`
SchemaUrl string `json:"json-schema-url,omitempty"`
}

// Config are the master config settings
Expand All @@ -46,6 +47,7 @@ var configFlagHub string
var configFlagInterface string
var configFlagPort string
var configFlagPortConfig int
var configFlagJsonSchemaUrl string

// ConfigRead reads the current info from config file
func ConfigRead() error {
Expand Down Expand Up @@ -104,6 +106,7 @@ func ConfigReset() {
configResetInterface()
ConfigSetHub("-")
Config.When = time.Now().UTC().Format("2006-01-02T15:04:05Z")
Config.SchemaUrl = ""
}

// ConfigShow displays all current config parameters
Expand Down Expand Up @@ -136,6 +139,9 @@ func ConfigShow() error {
fmt.Printf(" -portconfig %d\n", Config.IPort[Config.Interface].PortConfig)
}
}
if Config.SchemaUrl != "" {
fmt.Printf(" -json-schema-url %s\n", Config.SchemaUrl)
}

return nil

Expand Down Expand Up @@ -169,6 +175,11 @@ func ConfigFlagsProcess() (err error) {
} else if configFlagInterface != "" {
Config.Interface = configFlagInterface
}
if configFlagJsonSchemaUrl == "-" {
Config.SchemaUrl = ""
} else if configFlagJsonSchemaUrl != "" {
Config.SchemaUrl = configFlagJsonSchemaUrl
}
if configFlagPort == "-" {
temp := Config.IPort[Config.Interface]
temp.Port = ""
Expand Down Expand Up @@ -203,6 +214,7 @@ func ConfigFlagsRegister(notecardFlags bool, notehubFlags bool) {
// Process the commands
if notecardFlags {
flag.StringVar(&configFlagInterface, "interface", "", "select 'serial' or 'i2c' interface for notecard")
flag.StringVar(&configFlagJsonSchemaUrl, "json-schema-url", "", "set the schema URL for the notecard")
flag.StringVar(&configFlagPort, "port", "", "select serial or i2c port for notecard")
flag.IntVar(&configFlagPortConfig, "portconfig", 0, "set serial device speed or i2c address for notecard")
}
Expand Down Expand Up @@ -239,6 +251,7 @@ func FlagParse(notecardFlags bool, notehubFlags bool) (err error) {
case "-interface":
case "-port":
case "-portconfig":
case "-json-schema-url":
case "-hub":
// any odd argument that isn't one of our switches
default:
Expand All @@ -259,6 +272,11 @@ func FlagParse(notecardFlags bool, notehubFlags bool) (err error) {
Config.Interface = str
}

str = os.Getenv("NOTE_JSON_SCHEMA_URL")
if str != "" {
Config.SchemaUrl = str
}

// Override via env vars if specified
str = os.Getenv("NOTE_PORT")
if str != "" {
Expand Down
53 changes: 42 additions & 11 deletions notecard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
// Exit codes
const exitFail = 1

// The open notecard
// The open Notecard
var card *notecard.Context

// CLI Version - Set by ldflags during build/release
Expand Down Expand Up @@ -74,6 +74,7 @@ func getFlagGroups() []FlagGroup {
getFlagByName("output"),
getFlagByName("fast"),
getFlagByName("trace"),
getFlagByName("force"),
},
},
{
Expand Down Expand Up @@ -115,6 +116,7 @@ func getFlagGroups() []FlagGroup {
getFlagByName("interface"),
getFlagByName("port"),
getFlagByName("portconfig"),
getFlagByName("json-schema-url"),
},
},
{
Expand Down Expand Up @@ -167,23 +169,25 @@ func main() {
var actionWhenDisarmed bool
flag.BoolVar(&actionWhenDisarmed, "when-disarmed", false, "wait until ATTN is disarmed")
var actionVerbose bool
flag.BoolVar(&actionVerbose, "verbose", false, "display notecard requests and responses")
flag.BoolVar(&actionVerbose, "verbose", false, "display Notecard requests and responses")
var actionForce bool
flag.BoolVar(&actionForce, "force", false, "bypass JSON request validation against the Notecard schema (when used with -req)")
var actionWhenSynced bool
flag.BoolVar(&actionWhenSynced, "when-synced", false, "sync if needed and wait until sync completed")
var actionReserved bool
flag.BoolVar(&actionReserved, "reserved", false, "when exploring, include reserved notefiles")
var actionExplore bool
flag.BoolVar(&actionExplore, "explore", false, "explore the contents of the device")
var actionFactory bool
flag.BoolVar(&actionFactory, "factory", false, "reset notecard to factory defaults")
flag.BoolVar(&actionFactory, "factory", false, "reset Notecard to factory defaults")
var actionFormat bool
flag.BoolVar(&actionFormat, "format", false, "reset notecard's notefile storage but retain configuration")
flag.BoolVar(&actionFormat, "format", false, "reset Notecard's Notefile storage but retain configuration")
var actionInput string
flag.StringVar(&actionInput, "input", "", "add the contents of this file as a payload to the request")
var actionOutput string
flag.StringVar(&actionOutput, "output", "", "output file")
var actionLog string
flag.StringVar(&actionLog, "log", "", "add a text string to the _log.qo notefile")
flag.StringVar(&actionLog, "log", "", "add a text string to the _log.qo Notefile")
var actionTrace bool
flag.BoolVar(&actionTrace, "trace", false, "watch Notecard's trace output")
var actionPlayground bool
Expand All @@ -207,19 +211,19 @@ func main() {
var actionSetup string
flag.StringVar(&actionSetup, "setup", "", "issue requests sequentially as stored in the specified .json file")
var actionSetupSKU string
flag.StringVar(&actionSetupSKU, "setup-sku", "", "configure a notecard for self-setup even after factory restore, with requests in the specified .json file")
flag.StringVar(&actionSetupSKU, "setup-sku", "", "configure a Notecard for self-setup even after factory restore, with requests in the specified .json file")
var actionScan string
flag.StringVar(&actionScan, "scan", "", "scan a batch of notecards to collect info or to set them up")
flag.StringVar(&actionScan, "scan", "", "scan a batch of Notecards to collect info or to set them up")
var actionProvision string
flag.StringVar(&actionProvision, "provision", "", "provision into carrier account using AccountSID:AuthTOKEN")
var actionDFUPackage string
flag.StringVar(&actionDFUPackage, "binpack", "", "package multiple .bin's for DFU into a single .bins package")
var actionFast bool
flag.BoolVar(&actionFast, "fast", false, "use low timeouts and big buffers when sending to notecard knowing that {io} errors are to be expected")
flag.BoolVar(&actionFast, "fast", false, "use low timeouts and big buffers when sending to Notecard knowing that {io} errors are to be expected")
var actionSideload string
flag.StringVar(&actionSideload, "sideload", "", "side-load a .bin or .bins into the notecard's storage")
flag.StringVar(&actionSideload, "sideload", "", "side-load a .bin or .bins into the Notecard's storage")
var actionEcho int
flag.IntVar(&actionEcho, "echo", 0, "perform <N> iterations of a communications reliability test to the notecard")
flag.IntVar(&actionEcho, "echo", 0, "perform <N> iterations of a communications reliability test to the Notecard")
var actionVersion bool
flag.BoolVar(&actionVersion, "version", false, "print the current version of the CLI")

Expand Down Expand Up @@ -266,7 +270,7 @@ func main() {
if argsLeft == 1 {
actionRequest = flag.Args()[0]
} else if argsLeft > 0 {
fmt.Printf("to send a JSON request to the notecard, please place it in quotes")
fmt.Printf("to send a JSON request to the Notecard, please place it in quotes")
os.Exit(exitFail)
}
}
Expand Down Expand Up @@ -682,12 +686,38 @@ func main() {
actionRequest = ""
}

// If the user has provided a JSON schema URL, we need to clear the cache
// and re-initialize the schema. This is because the schema URL may have
// changed, and we need to make sure that the schema is up to date.
json_provided := false
for _, arg := range os.Args {
if arg == "-json-schema-url" {
json_provided = true
break
}
}
if err == nil && json_provided {
clearCache()
url := lib.Config.SchemaUrl
if url == "" {
url = defaultJsonSchemaUrl
}
err = initSchema(url)
}

if err == nil && actionRequest != "" {
if err == nil {
var rspJSON []byte
var req, rsp notecard.Request
note.JSONUnmarshal([]byte(actionRequest), &req)

if !actionForce {
err = validateRequest([]byte(actionRequest), lib.Config.SchemaUrl)
if err != nil {
goto done
}
}

// If we want to read the payload from a file, do so
if actionInput != "" {
var contents []byte
Expand Down Expand Up @@ -834,6 +864,7 @@ func main() {
err = explore(actionReserved, actionPretty)
}

done:
// Process errors
if err != nil {
if actionRequest != "" && !actionVerbose {
Expand Down
Loading