From d23b6d557995118de825b19c4c269d5d4e7f39c9 Mon Sep 17 00:00:00 2001 From: Florian Loitsch Date: Fri, 20 Sep 2024 11:58:38 +0200 Subject: [PATCH] Don't pass the device-config all over the place. (#551) --- cmd/jag/commands/container.go | 22 +++------------------- cmd/jag/commands/device.go | 16 ++++++++++------ cmd/jag/commands/firmware.go | 22 ++++++++-------------- cmd/jag/commands/ping.go | 8 +------- cmd/jag/commands/run.go | 7 +------ cmd/jag/commands/watch.go | 8 +------- 6 files changed, 24 insertions(+), 59 deletions(-) diff --git a/cmd/jag/commands/container.go b/cmd/jag/commands/container.go index b8d0fa44..645a6883 100644 --- a/cmd/jag/commands/container.go +++ b/cmd/jag/commands/container.go @@ -10,7 +10,6 @@ import ( "strings" "github.com/spf13/cobra" - "github.com/toitlang/jaguar/cmd/jag/directory" ) func ContainerCmd() *cobra.Command { @@ -33,11 +32,6 @@ func ContainerListCmd() *cobra.Command { Use: "list", Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - cfg, err := directory.GetDeviceConfig() - if err != nil { - return err - } - deviceSelect, err := parseDeviceFlag(cmd) if err != nil { return err @@ -49,7 +43,7 @@ func ContainerListCmd() *cobra.Command { return err } - device, err := GetDevice(ctx, cfg, sdk, false, deviceSelect) + device, err := GetDevice(ctx, sdk, false, deviceSelect) if err != nil { return err } @@ -99,11 +93,6 @@ func ContainerInstallCmd() *cobra.Command { return err } - cfg, err := directory.GetDeviceConfig() - if err != nil { - return err - } - programAssetsPath, err := GetProgramAssetsPath(cmd.Flags(), "assets") if err != nil { return err @@ -132,7 +121,7 @@ func ContainerInstallCmd() *cobra.Command { return err } - device, err := GetDevice(ctx, cfg, sdk, true, deviceSelect) + device, err := GetDevice(ctx, sdk, true, deviceSelect) if err != nil { return err } @@ -166,17 +155,12 @@ func ContainerUninstallCmd() *cobra.Command { return err } - cfg, err := directory.GetDeviceConfig() - if err != nil { - return err - } - sdk, err := GetSDK(ctx) if err != nil { return err } - device, err := GetDevice(ctx, cfg, sdk, true, deviceSelect) + device, err := GetDevice(ctx, sdk, true, deviceSelect) if err != nil { return err } diff --git a/cmd/jag/commands/device.go b/cmd/jag/commands/device.go index 98bae149..3ac19037 100644 --- a/cmd/jag/commands/device.go +++ b/cmd/jag/commands/device.go @@ -9,7 +9,7 @@ import ( "fmt" "strings" - "github.com/spf13/viper" + "github.com/toitlang/jaguar/cmd/jag/directory" ) const ( @@ -143,11 +143,15 @@ func intOr(data map[string]interface{}, key string, def int) int { return def } -func GetDevice(ctx context.Context, cfg *viper.Viper, sdk *SDK, checkPing bool, deviceSelect deviceSelect) (Device, error) { +func GetDevice(ctx context.Context, sdk *SDK, checkPing bool, deviceSelect deviceSelect) (Device, error) { + deviceCfg, err := directory.GetDeviceConfig() + if err != nil { + return nil, err + } manualPick := deviceSelect != nil - if cfg.IsSet("device") && !manualPick { + if deviceCfg.IsSet("device") && !manualPick { var decoded map[string]interface{} - if err := cfg.UnmarshalKey("device", &decoded); err != nil { + if err := deviceCfg.UnmarshalKey("device", &decoded); err != nil { return nil, err } d, err := NewDeviceFromJson(decoded) @@ -173,8 +177,8 @@ func GetDevice(ctx context.Context, cfg *viper.Viper, sdk *SDK, checkPing bool, if autoSelected { fmt.Printf("Found device '%s' again\n", d.Name()) } - cfg.Set("device", d.ToJson()) - if err := cfg.WriteConfig(); err != nil { + deviceCfg.Set("device", d.ToJson()) + if err := deviceCfg.WriteConfig(); err != nil { return nil, err } } diff --git a/cmd/jag/commands/firmware.go b/cmd/jag/commands/firmware.go index 26904c03..ddbf276b 100644 --- a/cmd/jag/commands/firmware.go +++ b/cmd/jag/commands/firmware.go @@ -27,11 +27,6 @@ func FirmwareCmd() *cobra.Command { Args: cobra.NoArgs, SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { - cfg, err := directory.GetDeviceConfig() - if err != nil { - return err - } - ctx := cmd.Context() deviceSelect, err := parseDeviceFlag(cmd) if err != nil { @@ -43,7 +38,7 @@ func FirmwareCmd() *cobra.Command { return err } - device, err := GetDevice(ctx, cfg, sdk, true, deviceSelect) + device, err := GetDevice(ctx, sdk, true, deviceSelect) if err != nil { return err } @@ -66,11 +61,6 @@ func FirmwareUpdateCmd() *cobra.Command { Args: cobra.MaximumNArgs(1), SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { - cfg, err := directory.GetDeviceConfig() - if err != nil { - return err - } - ctx := cmd.Context() deviceSelect, err := parseDeviceFlag(cmd) if err != nil { @@ -86,7 +76,7 @@ func FirmwareUpdateCmd() *cobra.Command { // the device flash stored by an older version are invalidated. newID := uuid.New().String() - device, err := GetDevice(ctx, cfg, sdk, true, deviceSelect) + device, err := GetDevice(ctx, sdk, true, deviceSelect) if err != nil { return err } @@ -173,8 +163,12 @@ func FirmwareUpdateCmd() *cobra.Command { // will have to ping again. device.SetID(newID) device.SetSDKVersion(sdk.Version) - cfg.Set("device", device.ToJson()) - return cfg.WriteConfig() + deviceCfg, err := directory.GetDeviceConfig() + if err != nil { + return err + } + deviceCfg.Set("device", device.ToJson()) + return deviceCfg.WriteConfig() }, } diff --git a/cmd/jag/commands/ping.go b/cmd/jag/commands/ping.go index fb9fc0b8..3209965d 100644 --- a/cmd/jag/commands/ping.go +++ b/cmd/jag/commands/ping.go @@ -8,7 +8,6 @@ import ( "fmt" "github.com/spf13/cobra" - "github.com/toitlang/jaguar/cmd/jag/directory" ) func PingCmd() *cobra.Command { @@ -17,11 +16,6 @@ func PingCmd() *cobra.Command { Short: "Ping a Jaguar device to see if it is active", SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { - cfg, err := directory.GetDeviceConfig() - if err != nil { - return err - } - deviceSelect, err := parseDeviceFlag(cmd) if err != nil { return err @@ -33,7 +27,7 @@ func PingCmd() *cobra.Command { return err } - device, err := GetDevice(ctx, cfg, sdk, false, deviceSelect) + device, err := GetDevice(ctx, sdk, false, deviceSelect) if err != nil { return err } diff --git a/cmd/jag/commands/run.go b/cmd/jag/commands/run.go index 2a197e62..86caa839 100644 --- a/cmd/jag/commands/run.go +++ b/cmd/jag/commands/run.go @@ -132,11 +132,6 @@ func RunCmd() *cobra.Command { return fmt.Errorf("--expression/-s is not yet supported when running on devices") } - cfg, err := directory.GetDeviceConfig() - if err != nil { - return err - } - if len(args) == 0 { return fmt.Errorf("no input file provided") } else if len(args) > 1 { @@ -163,7 +158,7 @@ func RunCmd() *cobra.Command { return err } - device, err := GetDevice(ctx, cfg, sdk, true, deviceSelect) + device, err := GetDevice(ctx, sdk, true, deviceSelect) if err != nil { return err } diff --git a/cmd/jag/commands/watch.go b/cmd/jag/commands/watch.go index 3bbcfce6..17f3f208 100644 --- a/cmd/jag/commands/watch.go +++ b/cmd/jag/commands/watch.go @@ -17,7 +17,6 @@ import ( "github.com/fsnotify/fsnotify" "github.com/spf13/cobra" - "github.com/toitlang/jaguar/cmd/jag/directory" ) func WatchCmd() *cobra.Command { @@ -27,11 +26,6 @@ func WatchCmd() *cobra.Command { Args: cobra.ExactArgs(1), SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { - cfg, err := directory.GetDeviceConfig() - if err != nil { - return err - } - programAssetsPath, err := GetProgramAssetsPath(cmd.Flags(), "assets") if err != nil { return err @@ -58,7 +52,7 @@ func WatchCmd() *cobra.Command { return err } - device, err := GetDevice(ctx, cfg, sdk, true, deviceSelect) + device, err := GetDevice(ctx, sdk, true, deviceSelect) if err != nil { return err }