Skip to content

Commit

Permalink
bluetoothctl: extracted actions (#2314)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube authored Mar 29, 2024
1 parent 3c0c9ee commit e35326c
Show file tree
Hide file tree
Showing 19 changed files with 157 additions and 91 deletions.
62 changes: 0 additions & 62 deletions completers/bluetoothctl_completer/cmd/action/action.go

This file was deleted.

8 changes: 6 additions & 2 deletions completers/bluetoothctl_completer/cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/bluetoothctl_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/bluetoothctl"
"github.com/carapace-sh/carapace/pkg/style"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +17,9 @@ func init() {
carapace.Gen(agentCmd).Standalone()
rootCmd.AddCommand(agentCmd)
carapace.Gen(agentCmd).PositionalCompletion(
action.ActionAgents(),
carapace.Batch(
carapace.ActionValues("off", "on", "auto").StyleF(style.ForKeyword),
bluetoothctl.ActionAgentCapabilities(),
).ToA(),
)
}
4 changes: 2 additions & 2 deletions completers/bluetoothctl_completer/cmd/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/bluetoothctl_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/bluetoothctl"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +16,6 @@ func init() {
carapace.Gen(blockCmd).Standalone()
rootCmd.AddCommand(blockCmd)
carapace.Gen(blockCmd).PositionalCompletion(
action.ActionDevices(""),
bluetoothctl.ActionDevices(bluetoothctl.DeviceOpts{}.Default()),
)
}
4 changes: 2 additions & 2 deletions completers/bluetoothctl_completer/cmd/cancelPairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/bluetoothctl_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/bluetoothctl"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +16,6 @@ func init() {
carapace.Gen(cancelPairingCmd).Standalone()
rootCmd.AddCommand(cancelPairingCmd)
carapace.Gen(cancelPairingCmd).PositionalCompletion(
action.ActionDevices("Paired"),
bluetoothctl.ActionDevices(bluetoothctl.DeviceOpts{Paired: true}),
)
}
4 changes: 2 additions & 2 deletions completers/bluetoothctl_completer/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/bluetoothctl_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/bluetoothctl"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +16,6 @@ func init() {
carapace.Gen(connectCmd).Standalone()
rootCmd.AddCommand(connectCmd)
carapace.Gen(connectCmd).PositionalCompletion(
action.ActionDevices(""),
bluetoothctl.ActionDevices(bluetoothctl.DeviceOpts{}.Default()),
)
}
8 changes: 7 additions & 1 deletion completers/bluetoothctl_completer/cmd/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace/pkg/style"
"github.com/spf13/cobra"
)

Expand All @@ -15,6 +16,11 @@ func init() {
carapace.Gen(devicesCmd).Standalone()
rootCmd.AddCommand(devicesCmd)
carapace.Gen(devicesCmd).PositionalCompletion(
carapace.ActionValues("Paired", "Bonded", "Trusted", "Connected"),
carapace.ActionStyledValues(
"Paired", style.Yellow,
"Bonded", style.Magenta,
"Trusted", style.Green,
"Connected", style.Blue,
),
)
}
4 changes: 2 additions & 2 deletions completers/bluetoothctl_completer/cmd/disconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/bluetoothctl_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/bluetoothctl"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +16,6 @@ func init() {
carapace.Gen(disconnectCmd).Standalone()
rootCmd.AddCommand(disconnectCmd)
carapace.Gen(disconnectCmd).PositionalCompletion(
action.ActionDevices("Connected"),
bluetoothctl.ActionDevices(bluetoothctl.DeviceOpts{Connected: true}),
)
}
4 changes: 2 additions & 2 deletions completers/bluetoothctl_completer/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/bluetoothctl_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/bluetoothctl"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +16,6 @@ func init() {
carapace.Gen(infoCmd).Standalone()
rootCmd.AddCommand(infoCmd)
carapace.Gen(infoCmd).PositionalCompletion(
action.ActionDevices(""),
bluetoothctl.ActionDevices(bluetoothctl.DeviceOpts{}.Default()),
)
}
4 changes: 2 additions & 2 deletions completers/bluetoothctl_completer/cmd/pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/bluetoothctl_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/bluetoothctl"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +16,6 @@ func init() {
carapace.Gen(pairCmd).Standalone()
rootCmd.AddCommand(pairCmd)
carapace.Gen(pairCmd).PositionalCompletion(
action.ActionDevices(""),
bluetoothctl.ActionDevices(bluetoothctl.DeviceOpts{}.Default()),
)
}
4 changes: 2 additions & 2 deletions completers/bluetoothctl_completer/cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/bluetoothctl_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/bluetoothctl"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +16,6 @@ func init() {
carapace.Gen(removeCmd).Standalone()
rootCmd.AddCommand(removeCmd)
carapace.Gen(removeCmd).PositionalCompletion(
action.ActionDevices(""),
bluetoothctl.ActionDevices(bluetoothctl.DeviceOpts{}.Default()),
)
}
4 changes: 2 additions & 2 deletions completers/bluetoothctl_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/bluetoothctl_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/bluetoothctl"
"github.com/spf13/cobra"
)

Expand All @@ -28,6 +28,6 @@ func init() {
rootCmd.Flags().BoolP("version", "v", false, "Display version")

carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"agent": action.ActionAgents(),
"agent": bluetoothctl.ActionAgentCapabilities(),
})
}
4 changes: 2 additions & 2 deletions completers/bluetoothctl_completer/cmd/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/bluetoothctl_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/bluetoothctl"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +16,6 @@ func init() {
carapace.Gen(selectCmd).Standalone()
rootCmd.AddCommand(selectCmd)
carapace.Gen(selectCmd).PositionalCompletion(
action.ActionControllers(),
bluetoothctl.ActionControllers(),
)
}
4 changes: 2 additions & 2 deletions completers/bluetoothctl_completer/cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/bluetoothctl_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/bluetoothctl"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +16,6 @@ func init() {
carapace.Gen(showCmd).Standalone()
rootCmd.AddCommand(showCmd)
carapace.Gen(showCmd).PositionalCompletion(
action.ActionControllers(),
bluetoothctl.ActionControllers(),
)
}
4 changes: 2 additions & 2 deletions completers/bluetoothctl_completer/cmd/trust.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/bluetoothctl_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/bluetoothctl"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +16,6 @@ func init() {
carapace.Gen(trustCmd).Standalone()
rootCmd.AddCommand(trustCmd)
carapace.Gen(trustCmd).PositionalCompletion(
action.ActionDevices(""),
bluetoothctl.ActionDevices(bluetoothctl.DeviceOpts{}.Default()),
)
}
4 changes: 2 additions & 2 deletions completers/bluetoothctl_completer/cmd/unblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/bluetoothctl_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/bluetoothctl"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +16,6 @@ func init() {
carapace.Gen(unblockCmd).Standalone()
rootCmd.AddCommand(unblockCmd)
carapace.Gen(unblockCmd).PositionalCompletion(
action.ActionDevices(""),
bluetoothctl.ActionDevices(bluetoothctl.DeviceOpts{}.Default()),
)
}
4 changes: 2 additions & 2 deletions completers/bluetoothctl_completer/cmd/untrust.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/bluetoothctl_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/bluetoothctl"
"github.com/spf13/cobra"
)

Expand All @@ -16,6 +16,6 @@ func init() {
carapace.Gen(untrustCmd).Standalone()
rootCmd.AddCommand(untrustCmd)
carapace.Gen(untrustCmd).PositionalCompletion(
action.ActionDevices("Trusted"),
bluetoothctl.ActionDevices(bluetoothctl.DeviceOpts{Trusted: true}),
)
}
17 changes: 17 additions & 0 deletions pkg/actions/tools/bluetoothctl/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package bluetoothctl

import "github.com/carapace-sh/carapace"

// ActionAgentCapabilities completes agent capabilities
//
// NoInputNoOutput
// KeyboardOnly
func ActionAgentCapabilities() carapace.Action {
return carapace.ActionValues(
"NoInputNoOutput",
"KeyboardOnly",
"KeyboardDisplay",
"DisplayYesNo",
"DisplayOnly",
).Tag("bluetooth agent capabilities")
}
27 changes: 27 additions & 0 deletions pkg/actions/tools/bluetoothctl/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package bluetoothctl

import (
"strings"

"github.com/carapace-sh/carapace"
)

// ActionControllers completes controllers
func ActionControllers() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return carapace.ActionExecCommand("bluetoothctl", "list")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
vals := make([]string, 0)

for _, line := range lines {
if line == "" {
break
}

fields := strings.Fields(line)
vals = append(vals, fields[1], strings.Join(fields[2:], " "))
}
return carapace.ActionValuesDescribed(vals...)
})
}).Tag("bluetooth controllers")
}
Loading

0 comments on commit e35326c

Please sign in to comment.