-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/swupdater' of https://github.com/graugans/go-ov…
…p8xx into feature/swupdater
- Loading branch information
Showing
24 changed files
with
483 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,25 @@ | ||
#!/bin/bash | ||
|
||
# install missing packages | ||
sudo apt-get update && | ||
sudo apt-get install -q -y git-lfs \ | ||
python3-pip \ | ||
python3-venv \ | ||
shfmt \ | ||
shellcheck && | ||
sudo apt-get clean && | ||
sudo rm -rf /var/lib/apt/lists/* | ||
|
||
# Fetch git large files | ||
git lfs fetch --all | ||
|
||
# Install Python packages | ||
export PATH=~/.local/bin:$PATH | ||
pip3 install --break-system-packages --upgrade pip | ||
pip3 install --break-system-packages -r requirements.txt | ||
|
||
# Go CI Lint | ||
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.54.0 | ||
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "$(go env GOPATH)/bin" v1.54.0 | ||
|
||
# Go tools | ||
go install golang.org/x/tools/cmd/goimports@latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ Copyright © 2023 Christian Ege <[email protected]> | |
package cmd | ||
|
||
import ( | ||
"github.com/graugans/go-ovp8xx/pkg/ovp8xx" | ||
"github.com/graugans/go-ovp8xx/v2/pkg/ovp8xx" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
|
@@ -14,7 +14,7 @@ var factoryResetCmd = &cobra.Command{ | |
Short: "Performs a factory reset of the device", | ||
Long: `Sometime one wants a fresh start. | ||
The command factoryReset resets all settings to their defaults and erases any addtional data like Docker containers.`, | ||
The command factoryReset resets all settings to their defaults and erases any additional data like Docker containers.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
keepNetworkSettings, err := cmd.Flags().GetBool("keepnetworksettings") | ||
if err != nil { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,40 @@ Copyright © 2023 Christian Ege <[email protected]> | |
package cmd | ||
|
||
import ( | ||
"github.com/graugans/go-ovp8xx/pkg/ovp8xx" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"text/template" | ||
|
||
"github.com/graugans/go-ovp8xx/v2/pkg/ovp8xx" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// toJSON converts the given object to a JSON string representation. | ||
// If an error occurs during marshaling, an empty string is returned. | ||
// This is taken from https://github.com/intel/tfortools | ||
func toJSON(obj interface{}) string { | ||
b, err := json.MarshalIndent(obj, "", "\t") | ||
if err != nil { | ||
return "" | ||
} | ||
return string(b) | ||
} | ||
|
||
// prefix can be used to create a list separated by s and the very first | ||
// element is not prefixed. | ||
func prefix(s string) func() string { | ||
i := -1 | ||
return func() string { | ||
i++ | ||
if i == 0 { | ||
return "" | ||
} | ||
return s | ||
} | ||
} | ||
|
||
func getCommand(cmd *cobra.Command, args []string) error { | ||
var result ovp8xx.Config | ||
var err error | ||
|
@@ -23,6 +53,35 @@ func getCommand(cmd *cobra.Command, args []string) error { | |
if result, err = o3r.Get(helper.jsonPointers()); err != nil { | ||
return err | ||
} | ||
|
||
if cmd.Flags().Changed("format") { | ||
if helper.prettyPrint() { | ||
return errors.New("you can't use --pretty and --format at the same time") | ||
} | ||
|
||
format, err := cmd.Flags().GetString("format") | ||
if err != nil { | ||
return fmt.Errorf("unable to get the format string from the command line: %w", err) | ||
} | ||
var inputData interface{} | ||
if err = json.Unmarshal([]byte(result.String()), &inputData); err != nil { | ||
return fmt.Errorf("unable to unmarshal the JSON data from the 'get' call: %w", err) | ||
} | ||
templateFunctions := template.FuncMap{ | ||
"toJSON": toJSON, | ||
"prefix": prefix, | ||
} | ||
tmpl, err := template.New("output").Funcs(templateFunctions).Parse(format) | ||
if err != nil { | ||
return fmt.Errorf("unable to parse the template: %w", err) | ||
} | ||
|
||
if err := tmpl.Execute(os.Stdout, inputData); err != nil { | ||
return fmt.Errorf("unable to execute the template: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
if err := helper.printJSONResult(result.String()); err != nil { | ||
return err | ||
} | ||
|
@@ -40,11 +99,11 @@ Valid queries are for example: | |
- To query all ports including all sub elements the query "/ports" can be used. | ||
In contrast to the concept of a JSON pointer the OVP8xx does not response with the data | ||
the pointer is pointing to, it returns the full object hirachie with the encapsulating | ||
the pointer is pointing to, it returns the full object hierarchy with the encapsulating | ||
object paths. | ||
A query of the name of the "port6" (/ports/port6/info/name) not just returns the object of that port, | ||
it also keeps the hirachy intact: | ||
it also keeps the hierarchy intact: | ||
{ | ||
"ports": | ||
|
@@ -66,4 +125,5 @@ func init() { | |
rootCmd.AddCommand(getCmd) | ||
getCmd.Flags().StringSliceP("pointer", "p", []string{""}, "A JSON pointer to be queried") | ||
getCmd.Flags().Bool("pretty", false, "Pretty print the JSON received from the device") | ||
getCmd.Flags().String("format", "", "Specify an alternative format for the JSON output") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ Copyright © 2023 Christian Ege <[email protected]> | |
package cmd | ||
|
||
import ( | ||
"github.com/graugans/go-ovp8xx/pkg/ovp8xx" | ||
"github.com/graugans/go-ovp8xx/v2/pkg/ovp8xx" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
|
@@ -34,7 +34,7 @@ var getSchemaCmd = &cobra.Command{ | |
Use: "getSchema", | ||
Short: "Retrieve the currently used JSON schema from the device", | ||
Long: `The OVP8xx getSchema command accepts a list of JSON pointers. | ||
The JSON schema provides details about multiple aspects of a paramter. It | ||
The JSON schema provides details about multiple aspects of a parameter. It | ||
contains information like the type of a parameter and its defaults. It also | ||
provides information weather a parameter is readOnly or not. | ||
|
@@ -76,7 +76,7 @@ The pointer '/device/swVersion/diagnostics' for example provides this informatio | |
"type": "object" | ||
} | ||
When no query is provided the complete schema is returend. | ||
When no query is provided the complete schema is returned. | ||
`, | ||
RunE: getSchemaCommand, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ Copyright © 2023 Christian Ege <[email protected]> | |
package cmd | ||
|
||
import ( | ||
"github.com/graugans/go-ovp8xx/pkg/ovp8xx" | ||
"github.com/graugans/go-ovp8xx/v2/pkg/ovp8xx" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
|
@@ -36,7 +36,7 @@ var getInitCmd = &cobra.Command{ | |
Long: `The OVP8xx provides a way to store a configuration on the device | ||
NOTE: This shall be used with care, because it may lead to an system which is no | ||
longer useable when the expectation from the safed configuration is no longer met.`, | ||
longer useable when the expectation from the saved configuration is no longer met.`, | ||
RunE: getInitCommand, | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ Copyright © 2023 Christian Ege <[email protected]> | |
package cmd | ||
|
||
import ( | ||
"github.com/graugans/go-ovp8xx/pkg/ovp8xx" | ||
"github.com/graugans/go-ovp8xx/v2/pkg/ovp8xx" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,19 +4,19 @@ Copyright © 2023 Christian Ege <[email protected]> | |
package cmd | ||
|
||
import ( | ||
"github.com/graugans/go-ovp8xx/pkg/ovp8xx" | ||
"github.com/graugans/go-ovp8xx/v2/pkg/ovp8xx" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// saveInitCmd represents the reboot command | ||
var saveInitCmd = &cobra.Command{ | ||
Use: "saveInit", | ||
Short: "Saves the init configuration on the device", | ||
Long: `To store the configuration persistant on the device the command saveInit can be used. | ||
Long: `To store the configuration persistent on the device the command saveInit can be used. | ||
A safed configuration persists a reboot. This is best used in combination with the "set" command. | ||
A saved configuration persists a reboot. This is best used in combination with the "set" command. | ||
Please use this with care. The scope should be as narrow as posible, to prevent any conflicts. | ||
Please use this with care. The scope should be as narrow as possible, to prevent any conflicts. | ||
In case no JSON Pointer is provided the complete configuration is saved`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
pointers, err := cmd.Flags().GetStringSlice("pointer") | ||
|
Oops, something went wrong.