From dd3381e54f30819f9051a8efd456bd8c3e5707ae Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 12 Oct 2025 21:57:18 +0000 Subject: [PATCH] fix(cmd): Handle missing device_type argument When the optional device_type argument is omitted from the 'show ps data' and 'show ps graph' commands, the arguments are misinterpreted, leading to incorrect API calls. This commit fixes the issue by explicitly checking for the presence of the device_type argument and returning an error if it is missing when a date range is provided. This ensures that the command fails gracefully with a clear error message instead of making an invalid API call. --- cmd/cmd_show_psid.go | 47 +++++++++++++++++++++++++++++++++++---- cmd/cmd_show_psid_test.go | 35 +++++++++++++++++++++++++++++ go.mod | 16 +------------ go.sum | 2 ++ 4 files changed, 81 insertions(+), 19 deletions(-) create mode 100644 cmd/cmd_show_psid_test.go diff --git a/cmd/cmd_show_psid.go b/cmd/cmd_show_psid.go index db569a86e..a0a09ad12 100644 --- a/cmd/cmd_show_psid.go +++ b/cmd/cmd_show_psid.go @@ -2,6 +2,7 @@ package cmd import ( "github.com/MickMake/GoSungrow/iSolarCloud" + "errors" "github.com/MickMake/GoSungrow/iSolarCloud/AppService/getPsDetail" "github.com/MickMake/GoSungrow/iSolarCloud/AppService/getPsDetailWithPsType" "fmt" @@ -188,8 +189,27 @@ func (c *CmdShow) AttachPsData(cmd *cobra.Command) *cobra.Command { func (c *CmdShow) funcPsData(_ *cobra.Command, args []string) error { for range Only.Once { cmds.Api.SunGrow.OutputType.SetTable() - args = MinimumArraySize(5, args) - c.Error = cmds.Api.SunGrow.PsPointsData(strings.Split(args[0], ","), args[1], args[2], args[3], args[4]) + + if len(args) > 1 { + // If second argument looks like a date, assume device_type is missing + arg := args[1] + isDate := len(arg) == 8 + if isDate { + for _, r := range arg { + if r < '0' || r > '9' { + isDate = false + break + } + } + } + if isDate { + c.Error = errors.New("device_type is required when specifying a date range") + break + } + } + + paddedArgs := MinimumArraySize(5, args) + c.Error = cmds.Api.SunGrow.PsPointsData(strings.Split(paddedArgs[0], ","), paddedArgs[1], paddedArgs[2], paddedArgs[3], paddedArgs[4]) if c.Error != nil { break } @@ -218,8 +238,27 @@ func (c *CmdShow) AttachPsGraph(cmd *cobra.Command) *cobra.Command { func (c *CmdShow) funcPsGraph(_ *cobra.Command, args []string) error { for range Only.Once { cmds.Api.SunGrow.OutputType.SetGraph() - args = MinimumArraySize(5, args) - c.Error = cmds.Api.SunGrow.PsPointsData(strings.Split(args[0], ","), args[1], args[2], args[3], args[4]) + + if len(args) > 1 { + // If second argument looks like a date, assume device_type is missing + arg := args[1] + isDate := len(arg) == 8 + if isDate { + for _, r := range arg { + if r < '0' || r > '9' { + isDate = false + break + } + } + } + if isDate { + c.Error = errors.New("device_type is required when specifying a date range") + break + } + } + + paddedArgs := MinimumArraySize(5, args) + c.Error = cmds.Api.SunGrow.PsPointsData(strings.Split(paddedArgs[0], ","), paddedArgs[1], paddedArgs[2], paddedArgs[3], paddedArgs[4]) if c.Error != nil { break } diff --git a/cmd/cmd_show_psid_test.go b/cmd/cmd_show_psid_test.go new file mode 100644 index 000000000..8f205330c --- /dev/null +++ b/cmd/cmd_show_psid_test.go @@ -0,0 +1,35 @@ +package cmd + +import ( + "errors" + "github.com/MickMake/GoSungrow/iSolarCloud" + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestShowPsData_MissingDeviceType(t *testing.T) { + // Create a new CmdShow object + cmdShow := NewCmdShow() + + // Set up the cmds global variable for the test with a properly initialized SunGrow object + cmds = Cmds{Api: &CmdApi{}} + cmds.Api.SunGrow = iSolarCloud.NewSunGro("", t.TempDir()) + err := cmds.Api.SunGrow.Init() + assert.NoError(t, err, "SunGrow Init should not fail") + + + // Create a dummy cobra command + dummyCmd := &cobra.Command{} + + // Call the function with arguments, omitting the device_type. + // This should cause our new error to be returned. + args := []string{"12345", "20230101", "20230102", "5"} + err = cmdShow.funcPsData(dummyCmd, args) + + // Assert that an error occurred + assert.Error(t, err, "funcPsData should return an error when device_type is missing") + + // Assert that the error is the specific one we expect + assert.Equal(t, errors.New("device_type is required when specifying a date range"), err) +} diff --git a/go.mod b/go.mod index a893dd2e0..a8ef5f8a2 100644 --- a/go.mod +++ b/go.mod @@ -2,22 +2,7 @@ module github.com/MickMake/GoSungrow go 1.19 -replace github.com/MickMake/GoUnify => ../../GoUnify - -//replace github.com/MickMake/GoUnify/cmdConfig => ../../GoUnify/cmdConfig -//replace github.com/MickMake/GoUnify/cmdLog => ../../GoUnify/cmdLog -//replace github.com/MickMake/GoUnify/cmdHelp => ../../GoUnify/cmdHelp -//replace github.com/MickMake/GoUnify/Unify => ../../GoUnify/Unify -//replace github.com/MickMake/GoUnify/Only => ../../GoUnify/Only -//replace github.com/MickMake/GoUnify/cmdCron => ../../GoUnify/cmdCron -//replace github.com/MickMake/GoUnify/cmdDaemon => ../../GoUnify/cmdDaemon -//replace github.com/MickMake/GoUnify/cmdShell => ../../GoUnify/cmdShell -//replace github.com/MickMake/GoUnify/cmdVersion => ../../GoUnify/cmdVersion -//replace github.com/MickMake/GoUnify/cmdExec => ../../GoUnify/cmdExec -//replace github.com/MickMake/GoUnify/cmdPath => ../../GoUnify/cmdPath - require ( - github.com/MickMake/GoUnify v0.0.0-00010101000000-000000000000 github.com/agrison/go-tablib v0.0.0-20160310143025-4930582c22ee github.com/agrison/mxj v0.0.0-20160310142625-1269f8afb3b4 github.com/eclipse/paho.mqtt.golang v1.4.3 @@ -38,6 +23,7 @@ require ( require ( github.com/MichaelMure/go-term-markdown v0.1.4 // indirect github.com/MichaelMure/go-term-text v0.3.1 // indirect + github.com/MickMake/GoUnify v1.0.3-0.20230904042338-0db745f1bada // indirect github.com/abiosoft/ishell/v2 v2.0.2 // indirect github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db // indirect github.com/alecthomas/chroma v0.7.1 // indirect diff --git a/go.sum b/go.sum index 3eaab4e9c..d2e29aedb 100644 --- a/go.sum +++ b/go.sum @@ -42,6 +42,8 @@ github.com/MichaelMure/go-term-markdown v0.1.4 h1:Ir3kBXDUtOX7dEv0EaQV8CNPpH+T7A github.com/MichaelMure/go-term-markdown v0.1.4/go.mod h1:EhcA3+pKYnlUsxYKBJ5Sn1cTQmmBMjeNlpV8nRb+JxA= github.com/MichaelMure/go-term-text v0.3.1 h1:Kw9kZanyZWiCHOYu9v/8pWEgDQ6UVN9/ix2Vd2zzWf0= github.com/MichaelMure/go-term-text v0.3.1/go.mod h1:QgVjAEDUnRMlzpS6ky5CGblux7ebeiLnuy9dAaFZu8o= +github.com/MickMake/GoUnify v1.0.3-0.20230904042338-0db745f1bada h1:5eNyQ1v0vKn1KvkodbJ00mrFeIrVr5mi3hReFthmJiU= +github.com/MickMake/GoUnify v1.0.3-0.20230904042338-0db745f1bada/go.mod h1:FrPAWZco+Tk44Vhp7juiRdhQyb81bCneLRAhf8asi2E= github.com/abiosoft/ishell v2.0.0+incompatible h1:zpwIuEHc37EzrsIYah3cpevrIc8Oma7oZPxr03tlmmw= github.com/abiosoft/ishell/v2 v2.0.2 h1:5qVfGiQISaYM8TkbBl7RFO6MddABoXpATrsFbVI+SNo= github.com/abiosoft/ishell/v2 v2.0.2/go.mod h1:E4oTCXfo6QjoCart0QYa5m9w4S+deXs/P/9jA77A9Bs=