Skip to content

Commit

Permalink
Don't pass the device-config all over the place. (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch authored Sep 20, 2024
1 parent b7ed49d commit d23b6d5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 59 deletions.
22 changes: 3 additions & 19 deletions cmd/jag/commands/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/toitlang/jaguar/cmd/jag/directory"
)

func ContainerCmd() *cobra.Command {
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 10 additions & 6 deletions cmd/jag/commands/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"strings"

"github.com/spf13/viper"
"github.com/toitlang/jaguar/cmd/jag/directory"
)

const (
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
}
Expand Down
22 changes: 8 additions & 14 deletions cmd/jag/commands/firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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()
},
}

Expand Down
8 changes: 1 addition & 7 deletions cmd/jag/commands/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"

"github.com/spf13/cobra"
"github.com/toitlang/jaguar/cmd/jag/directory"
)

func PingCmd() *cobra.Command {
Expand All @@ -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
Expand All @@ -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
}
Expand Down
7 changes: 1 addition & 6 deletions cmd/jag/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
8 changes: 1 addition & 7 deletions cmd/jag/commands/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (

"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
"github.com/toitlang/jaguar/cmd/jag/directory"
)

func WatchCmd() *cobra.Command {
Expand All @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit d23b6d5

Please sign in to comment.