-
Notifications
You must be signed in to change notification settings - Fork 0
/
alias.go
78 lines (60 loc) · 1.51 KB
/
alias.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package alias
import (
"errors"
"slices"
"github.com/dangermike/roku_toy/aliasing"
"github.com/dangermike/roku_toy/logging"
"github.com/spf13/cobra"
)
func Alias() *cobra.Command {
cmd := &cobra.Command{
Use: "alias",
Short: "add an alias for a Roku device",
RunE: aliasE,
}
cmd.Flags().BoolP("verbose", "v", false, "verbose logging")
return cmd
}
func Unalias() *cobra.Command {
cmd := &cobra.Command{
Use: "unalias",
Short: "remove an alias for a Roku device",
RunE: unaliasE,
}
cmd.Flags().BoolP("verbose", "v", false, "verbose logging")
return cmd
}
func aliasE(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("verbose")
if err != nil {
return err
}
if len(args) != 2 {
return errors.New("USN and name required")
}
ctx := logging.NewContext(cmd.Context(), logging.Configure(debug))
aliases, err := aliasing.Load(ctx)
if err != nil {
return err
}
aliases = append(aliases, aliasing.Alias{USN: args[0], Name: args[1]})
return aliasing.Save(ctx, aliases)
}
func unaliasE(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("verbose")
if err != nil {
return err
}
if len(args) != 1 {
return errors.New("USN or name required")
}
ctx := logging.NewContext(cmd.Context(), logging.Configure(debug))
aliases, err := aliasing.Load(ctx)
if err != nil {
return err
}
aliases = slices.DeleteFunc(aliases, func(a aliasing.Alias) bool {
return a.Name == args[0] || a.USN == args[0]
})
return aliasing.Save(ctx, aliases)
}