Skip to content

Commit

Permalink
add script subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
jcodybaker committed Feb 20, 2024
1 parent 47d48de commit 8b25096
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ Response to Shelly.Update command for http://192.168.1.29/rpc: success
- `schedule`
- `delete` ([Schedule.Delete](https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/Schedule#scheduledelete))
- `delete-all` ([Schedule.DeleteAll](https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/Schedule#scheduledeleteall))
- `script`
- `create` ([Script.Create](https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/Script#scriptcreate))
- `get-config` ([Script.GetConfig](https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/Script#scriptgetconfig))
- `get-status` ([Script.GetStatus](https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/Script#scriptgetstatus))
- `put-code` ([Script.PutCode](https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/Script#scriptputcode))
- `set-config` ([Script.SetConfig](https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/Script#scriptsetconfig))
- `shelly`
- `check-for-update` ([Shelly.CheckForUpdate](https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/Shelly#shellycheckforupdate))
- `detect-location` ([Shelly.DetectLocation](https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/Shelly#shellydetectlocation))
Expand Down
15 changes: 15 additions & 0 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ var (
},
}

scriptComponent = &gencobra.Component{
Parent: &cobra.Command{
GroupID: "Component RPCs",
Use: "script",
Short: "RPCs related to managing scripts",
},
Requests: []shelly.RPCRequestBody{
&shelly.ScriptCreateRequest{},
&shelly.ScriptGetConfigRequest{},
&shelly.ScriptSetConfigRequest{},
&shelly.ScriptGetStatusRequest{},
},
}

shellyComponent = &gencobra.Component{
Parent: &cobra.Command{
GroupID: "Component RPCs",
Expand Down Expand Up @@ -185,6 +199,7 @@ var (
lightComponent,
mqttComponent,
scheduleComponent,
scriptComponent,
shellyComponent,
switchComponent,
sysComponent,
Expand Down
11 changes: 8 additions & 3 deletions cmd/gen_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ func newDataCommand(reqBuilder reqBuilder, strParam, fileParam, nullParam string
var b *bytes.Buffer
data := viper.GetString(strParam)
dataFile := viper.GetString(fileParam)
remove := viper.GetBool(nullParam)
remove := false
fields := fmt.Sprintf("--%s and --%s", strParam, fileParam)
if nullParam != "" {
remove = viper.GetBool(nullParam)
fields = fmt.Sprintf("--%s, --%s, and --%s", strParam, fileParam, nullParam)
}
if (data != "" && dataFile != "") || (data != "" && remove) || (dataFile != "" && remove) {
return fmt.Errorf("--%s, --%s, and --%s options are mutually exclusive", strParam, fileParam, nullParam)
return fmt.Errorf("%s options are mutually exclusive", fields)
}
if data == "" && dataFile == "" && !remove {
return fmt.Errorf("exactly one of --%s, --%s, and --%s options are required", strParam, fileParam, nullParam)
return fmt.Errorf("exactly one of %s options are required", fields)
}
if data != "" {
b = bytes.NewBufferString(data)
Expand Down
34 changes: 34 additions & 0 deletions cmd/script_put_code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"github.com/jcodybaker/go-shelly"
"github.com/spf13/cobra"
)

var (
scriptPutCodeCmd = &cobra.Command{
Use: "put-code",
}
)

func init() {
scriptPutCodeCmd.Flags().String(
"code", "", "Code. (one of --code or --code-file is required)",
)
scriptPutCodeCmd.Flags().String(
"code-file", "", "path to a file containing code.",
)

scriptComponent.Parent.AddCommand(scriptPutCodeCmd)
discoveryFlags(scriptPutCodeCmd.Flags(), discoveryFlagsOptions{interactive: true})
scriptPutCodeCmd.RunE = newDataCommand(
func(code *string, append bool) shelly.RPCRequestBody {
r := &shelly.ScriptPutCodeRequest{
Append: append,
}
if code != nil {
r.Code = *code
}
return r
}, "code", "code-file", "")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21.3
require (
github.com/go-logr/zerologr v1.2.3
github.com/hashicorp/mdns v1.0.5
github.com/jcodybaker/go-shelly v0.0.0-20240219095728-be65a682916b
github.com/jcodybaker/go-shelly v0.0.0-20240219132253-eb811e71b087
github.com/mongoose-os/mos v0.0.0-20230313140341-b44964e63a92
github.com/prometheus/client_golang v1.18.0
github.com/rs/zerolog v1.31.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,10 @@ github.com/jcodybaker/go-shelly v0.0.0-20240219095047-15d8021c50d7 h1:cWn6PcVFIq
github.com/jcodybaker/go-shelly v0.0.0-20240219095047-15d8021c50d7/go.mod h1:EfKnkqHSomR+wV7AoVgv6wU+kz1Xm4RSaEKaWMKWgWg=
github.com/jcodybaker/go-shelly v0.0.0-20240219095728-be65a682916b h1:Qsp0H62AbXVZ8b2MXfy5vmnw4TDDCr388TAa2ShMGBA=
github.com/jcodybaker/go-shelly v0.0.0-20240219095728-be65a682916b/go.mod h1:EfKnkqHSomR+wV7AoVgv6wU+kz1Xm4RSaEKaWMKWgWg=
github.com/jcodybaker/go-shelly v0.0.0-20240219125818-e8cdd5fed7d8 h1:Yrm3Ddb/IXgGkwu2hflAMKpfSyljp+JpaeJJDfC9+e0=
github.com/jcodybaker/go-shelly v0.0.0-20240219125818-e8cdd5fed7d8/go.mod h1:EfKnkqHSomR+wV7AoVgv6wU+kz1Xm4RSaEKaWMKWgWg=
github.com/jcodybaker/go-shelly v0.0.0-20240219132253-eb811e71b087 h1:zJBnKH/hGZ7mfWbApAPsxy6TxQZjkwQ5qKZVK7+0OmY=
github.com/jcodybaker/go-shelly v0.0.0-20240219132253-eb811e71b087/go.mod h1:EfKnkqHSomR+wV7AoVgv6wU+kz1Xm4RSaEKaWMKWgWg=
github.com/jcodybaker/mdns v0.0.0-20240218225721-3b8606993b85 h1:/Ls0Q1POaNRFf9uopWlac5skKmWCkEwn/EPmueN5Mco=
github.com/jcodybaker/mdns v0.0.0-20240218225721-3b8606993b85/go.mod h1:RBWesCDwuRmyqepO+WxO8JSfrh3Tj3LkSzXPQlFq2nM=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
Expand Down

0 comments on commit 8b25096

Please sign in to comment.