Skip to content

Commit

Permalink
dev: Set active source in ttnv3 command
Browse files Browse the repository at this point in the history
Execute parent's persistent pre-run in subcommands spf13/cobra#252
  • Loading branch information
happyRip committed Apr 25, 2023
1 parent c4aae44 commit 02397d6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
4 changes: 3 additions & 1 deletion cmd/ttnv3/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import (

var applicationsCmd = &cobra.Command{
Use: "application [app-id] ...",
Aliases: []string{"applications", "app"},
Short: "Export all devices of an application",
Aliases: []string{"applications", "app"},

PersistentPreRunE: commands.ExecuteParentPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
commands.Export(cmd, args, func(s source.Source, item string) error {
return s.RangeDevices(item, export.FromContext(cmd.Context()).ExportDev)
Expand Down
2 changes: 2 additions & 0 deletions cmd/ttnv3/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var devicesCmd = &cobra.Command{
Use: "device [dev-id] ...",
Short: "Export devices by DevEUI",
Aliases: []string{"end-devices", "end-device", "devices", "dev"},

PersistentPreRunE: commands.ExecuteParentPersistentPreRun,
RunE: func(cmd *cobra.Command, args []string) error {
return commands.Export(cmd, args, export.FromContext(cmd.Context()).ExportDev)
},
Expand Down
13 changes: 11 additions & 2 deletions cmd/ttnv3/ttnv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ package ttnv3

import (
"github.com/spf13/cobra"

"go.thethings.network/lorawan-stack-migrate/pkg/commands"
"go.thethings.network/lorawan-stack-migrate/pkg/source"
)

const sourceName = "ttnv3"

// TTNv3Cmd represents the ttnv3 source.
var TTNv3Cmd = &cobra.Command{
Use: sourceName + " ...",
Short: "Export devices from The Things Stack",
Use: sourceName + " ...",
Short: "Export devices from The Things Stack",

SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
source.RootConfig.Source = sourceName

return commands.ExecuteParentPersistentPreRun(cmd, args)
},
}
49 changes: 49 additions & 0 deletions pkg/commands/parent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright © 2023 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package commands

import "github.com/spf13/cobra"

func ExecuteParentPersistentPreRun(cmd *cobra.Command, args []string) error {
if !cmd.HasParent() {
return nil
}
p := cmd.Parent()

if f := p.PersistentPreRunE; f != nil {
if err := f(p, args); err != nil {
return err
}
} else if f := p.PersistentPreRun; f != nil {
f(p, args)
}
return nil
}

func ExecuteParentPersistentPostRun(cmd *cobra.Command, args []string) error {
if !cmd.HasParent() {
return nil
}
p := cmd.Parent()

if f := p.PersistentPostRunE; f != nil {
if err := f(p, args); err != nil {
return err
}
} else if f := p.PersistentPostRun; f != nil {
f(p, args)
}
return nil
}

0 comments on commit 02397d6

Please sign in to comment.