Skip to content

Commit 02397d6

Browse files
committed
dev: Set active source in ttnv3 command
Execute parent's persistent pre-run in subcommands spf13/cobra#252
1 parent c4aae44 commit 02397d6

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

cmd/ttnv3/application.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ import (
2323

2424
var applicationsCmd = &cobra.Command{
2525
Use: "application [app-id] ...",
26-
Aliases: []string{"applications", "app"},
2726
Short: "Export all devices of an application",
27+
Aliases: []string{"applications", "app"},
28+
29+
PersistentPreRunE: commands.ExecuteParentPersistentPreRun,
2830
Run: func(cmd *cobra.Command, args []string) {
2931
commands.Export(cmd, args, func(s source.Source, item string) error {
3032
return s.RangeDevices(item, export.FromContext(cmd.Context()).ExportDev)

cmd/ttnv3/devices.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ var devicesCmd = &cobra.Command{
2525
Use: "device [dev-id] ...",
2626
Short: "Export devices by DevEUI",
2727
Aliases: []string{"end-devices", "end-device", "devices", "dev"},
28+
29+
PersistentPreRunE: commands.ExecuteParentPersistentPreRun,
2830
RunE: func(cmd *cobra.Command, args []string) error {
2931
return commands.Export(cmd, args, export.FromContext(cmd.Context()).ExportDev)
3032
},

cmd/ttnv3/ttnv3.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,22 @@ package ttnv3
1616

1717
import (
1818
"github.com/spf13/cobra"
19+
20+
"go.thethings.network/lorawan-stack-migrate/pkg/commands"
21+
"go.thethings.network/lorawan-stack-migrate/pkg/source"
1922
)
2023

2124
const sourceName = "ttnv3"
2225

2326
// TTNv3Cmd represents the ttnv3 source.
2427
var TTNv3Cmd = &cobra.Command{
25-
Use: sourceName + " ...",
26-
Short: "Export devices from The Things Stack",
28+
Use: sourceName + " ...",
29+
Short: "Export devices from The Things Stack",
30+
2731
SilenceUsage: true,
32+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
33+
source.RootConfig.Source = sourceName
34+
35+
return commands.ExecuteParentPersistentPreRun(cmd, args)
36+
},
2837
}

pkg/commands/parent.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright © 2023 The Things Network Foundation, The Things Industries B.V.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package commands
16+
17+
import "github.com/spf13/cobra"
18+
19+
func ExecuteParentPersistentPreRun(cmd *cobra.Command, args []string) error {
20+
if !cmd.HasParent() {
21+
return nil
22+
}
23+
p := cmd.Parent()
24+
25+
if f := p.PersistentPreRunE; f != nil {
26+
if err := f(p, args); err != nil {
27+
return err
28+
}
29+
} else if f := p.PersistentPreRun; f != nil {
30+
f(p, args)
31+
}
32+
return nil
33+
}
34+
35+
func ExecuteParentPersistentPostRun(cmd *cobra.Command, args []string) error {
36+
if !cmd.HasParent() {
37+
return nil
38+
}
39+
p := cmd.Parent()
40+
41+
if f := p.PersistentPostRunE; f != nil {
42+
if err := f(p, args); err != nil {
43+
return err
44+
}
45+
} else if f := p.PersistentPostRun; f != nil {
46+
f(p, args)
47+
}
48+
return nil
49+
}

0 commit comments

Comments
 (0)