Skip to content

Commit

Permalink
all: Make iterator use stdin for devices
Browse files Browse the repository at this point in the history
  • Loading branch information
KrishnaIyer committed Nov 7, 2023
1 parent 9720ffa commit 1e5e089
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Export(cmd *cobra.Command, args []string, f func(s source.Source, item stri
var iter iterator.Iterator
switch len(args) {
case 0:
iter = s.Iterator()
iter = s.Iterator(cmd.Name() == "application")
default:
iter = iterator.NewListIterator(args)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/source/chirpstack/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func createNewSource(cfg *config.Config) source.CreateSource {
}

// Iterator implements source.Source.
func (s Source) Iterator() iterator.Iterator {
func (s Source) Iterator(bool) iterator.Iterator {
return iterator.NewReaderIterator(os.Stdin, '\n')
}

Expand Down
16 changes: 9 additions & 7 deletions pkg/source/firefly/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package firefly

import (
"context"
"os"

"github.com/TheThingsNetwork/go-utils/random"
"go.thethings.network/lorawan-stack/v3/pkg/networkserver/mac"
Expand Down Expand Up @@ -52,7 +53,10 @@ func createNewSource(cfg *Config) source.CreateSource {
}

// Iterator implements source.Source.
func (s Source) Iterator() iterator.Iterator {
func (s Source) Iterator(isApplication bool) iterator.Iterator {
if !isApplication {
return iterator.NewReaderIterator(os.Stdin, '\n')
}
if s.all {
// The Firefly LNS does not group devices by an application.
// When the "all" flag is set, we get all devices accessible by the API key.
Expand Down Expand Up @@ -84,7 +88,6 @@ func (s Source) ExportDevice(devEUIString string) (*ttnpb.EndDevice, error) {
if err := joinEUI.UnmarshalText([]byte(s.joinEUI)); err != nil {
return nil, err
}

v3dev := &ttnpb.EndDevice{
Name: ffdev.Name,
Description: ffdev.Description,
Expand All @@ -102,7 +105,6 @@ func (s Source) ExportDevice(devEUIString string) (*ttnpb.EndDevice, error) {
LorawanVersion: s.derivedMacVersion,
LorawanPhyVersion: s.derivedPhyVersion,
}

if ffdev.Location != nil {
v3dev.Locations = map[string]*ttnpb.Location{
"user": {
Expand Down Expand Up @@ -136,10 +138,10 @@ func (s Source) ExportDevice(devEUIString string) (*ttnpb.EndDevice, error) {
if err != nil {
return nil, err
}
// This cannot be empty
v3dev.Session.StartedAt = timestamppb.Now()
v3dev.Session.Keys.SessionKeyId = random.Bytes(16)

if v3dev.SupportsJoin {
v3dev.Session.StartedAt = timestamppb.Now()
v3dev.Session.Keys.SessionKeyId = random.Bytes(16)
}
v3dev.Session.Keys.AppSKey.Key, err = util.UnmarshalTextToBytes(&types.AES128Key{}, ffdev.ApplicationSessionKey)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type Source interface {
// Close cleans up and terminates any open connections.
Close() error
// Iterator returns an iterator for devices.
Iterator() iterator.Iterator
Iterator(isApplication bool) iterator.Iterator
}

// CreateSource is a function that constructs a new Source.
Expand Down
2 changes: 1 addition & 1 deletion pkg/source/ttnv2/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (s *Source) ExportDevice(devID string) (*ttnpb.EndDevice, error) {
}

// Iterator implements source.Source.
func (s *Source) Iterator() iterator.Iterator {
func (s *Source) Iterator(bool) iterator.Iterator {
return iterator.NewReaderIterator(os.Stdin, '\n')
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/source/tts/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (s Source) ExportDevice(devID string) (*ttnpb.EndDevice, error) {
}

// Iterator implements source.Source.
func (s Source) Iterator() iterator.Iterator {
func (s Source) Iterator(bool) iterator.Iterator {
return iterator.NewReaderIterator(os.Stdin, '\n')
}

Expand Down

0 comments on commit 1e5e089

Please sign in to comment.