Skip to content

Commit

Permalink
users and groups commands: added subcommands to be prepared for editi…
Browse files Browse the repository at this point in the history
…ng capabilities later
  • Loading branch information
halacs committed Jan 13, 2024
1 parent fa6ba6b commit f7700e8
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 127 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Available Commands:
completion Generate the autocompletion script for the specified shell
discover Discover Hörmann BiSecur gateways on the local network
get-name Queries the name of the Hörmann BiSecur gateway
groups Manages users defined in your Hörmann BiSecur gateway.
groups Manages doors defined in your Hörmann BiSecur gateway.
help Help about any command
login
logout
Expand Down Expand Up @@ -95,7 +95,7 @@ INFO[2023-12-26T21:00:19+01:00] Token: 0x7974DB57

### Get users
```bash
$ ./dist/halsecur users
$ ./dist/halsecur users list
DEBU[2023-12-26T21:01:38+01:00] Connecting to 192.168.3.232:4000
DEBU[2023-12-26T21:01:38+01:00] Request: SrcMAC=0x000000000009, DstMAC=0x5410EC8528BB, BodyLength=0x0, packet=[Tag=0x1, Token=0x7974DB57, CommandID=0x6 (0x6), payload=[Jcmp: {"CMD":"GET_USERS"}], Checksum=0x0, isResponse=false], Checksum=0x0, isResponse: false
DEBU[2023-12-26T21:01:38+01:00] Request bytes: 30303030303030303030303935343130454338353238424230303143303137393734444235373036374232323433344434343232334132323437343535343546353535333435353235333232374441314431
Expand All @@ -107,7 +107,7 @@ INFO[2023-12-26T21:01:38+01:00] Users: [ID=0, Name="admin", IsAdmin=true, Groups
### Get groups
```bash
$ ./dist/halsecur groups
$ ./dist/halsecur groups list
DEBU[2023-12-26T21:02:20+01:00] Connecting to 192.168.3.232:4000
DEBU[2023-12-26T21:02:20+01:00] Request: SrcMAC=0x000000000009, DstMAC=0x5410EC8528BB, BodyLength=0x0, packet=[Tag=0x1, Token=0x7974DB57, CommandID=0x6 (0x6), payload=[Jcmp: {"CMD":"GET_GROUPS"}], Checksum=0x0, isResponse=false], Checksum=0x0, isResponse: false
DEBU[2023-12-26T21:02:20+01:00] Request bytes: 303030303030303030303039353431304543383532384242303031443031373937344442353730363742323234333444343432323341323234373435353435463437353234463535353035333232374446303446
Expand Down
70 changes: 8 additions & 62 deletions cli/cmd/groups.go
Original file line number Diff line number Diff line change
@@ -1,71 +1,17 @@
package cmd

import (
"bisecur/cli"
"bisecur/sdk"
"github.com/spf13/viper"
"os"

"github.com/spf13/cobra"
)

func init() {
groupsCmd := &cobra.Command{
Use: GroupsCmdName,
Short: "Manages users defined in your Hörmann BiSecur gateway.",
Long: ``,
PreRunE: preRunFuncs,
Run: func(cmd *cobra.Command, args []string) {
// TODO implement query user list and rights, add and delete user, password change of an already existing user

deviceMac := viper.GetString(ArgNameDeviceMac)
host := viper.GetString(ArgNameHost)
port := viper.GetInt(ArgNamePort)
token := viper.GetUint32(ArgNameToken)

mac, err := cli.ParesMacString(deviceMac)
if err != nil {
log.Fatalf("%v", err)
os.Exit(1)
}

err = listGroups(localMac, mac, host, port, token)
if err != nil {
log.Fatalf("%v", err)
os.Exit(2)
}
},
}

rootCmd.AddCommand(groupsCmd)
var groupsCmd = &cobra.Command{
Use: GroupsCmdName,
Short: "Manages doors defined in your Hörmann BiSecur gateway.",
Long: ``,
PreRunE: preRunFuncs,
Run: nil,
}

func listGroups(localMac [6]byte, mac [6]byte, host string, port int, token uint32) error {
client := sdk.NewClient(log, localMac, mac, host, port, token)
err := client.Open()
if err != nil {
return err
}

defer func() {
err2 := client.Close()
if err2 != nil {
log.Errorf("%v", err)
}
}()

var groups *sdk.Groups
err = retry(func() error {
var err2 error
groups, err2 = client.GetGroups()
return err2
})

if err != nil {
return err
}

log.Infof("Groups: %s", groups.String())

return nil
func init() {
rootCmd.AddCommand(groupsCmd)
}
18 changes: 18 additions & 0 deletions cli/cmd/groupsAdd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/spf13/cobra"
)

var groupsCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a new gateway group",
Long: `Create a new gateway group`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatalf("Not implemented yet")
},
}

func init() {
groupsCmd.AddCommand(groupsCreateCmd)
}
18 changes: 18 additions & 0 deletions cli/cmd/groupsDelete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/spf13/cobra"
)

var groupsDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete a gateway group",
Long: `Delete a gateway group`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatalf("Not implemented yet")
},
}

func init() {
groupsCmd.AddCommand(groupsDeleteCmd)
}
68 changes: 68 additions & 0 deletions cli/cmd/groupsList.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmd

import (
"bisecur/cli"
"bisecur/sdk"
"github.com/spf13/viper"
"os"

"github.com/spf13/cobra"
)

var groupsListCmd = &cobra.Command{
Use: "list",
Short: "List current gateway groups",
Long: `List current gateway groups`,
Run: func(cmd *cobra.Command, args []string) {
deviceMac := viper.GetString(ArgNameDeviceMac)
host := viper.GetString(ArgNameHost)
port := viper.GetInt(ArgNamePort)
token := viper.GetUint32(ArgNameToken)

mac, err := cli.ParesMacString(deviceMac)
if err != nil {
log.Fatalf("%v", err)
os.Exit(1)
}

err = listGroups(localMac, mac, host, port, token)
if err != nil {
log.Fatalf("%v", err)
os.Exit(2)
}
},
}

func init() {
groupsCmd.AddCommand(groupsListCmd)
}

func listGroups(localMac [6]byte, mac [6]byte, host string, port int, token uint32) error {
client := sdk.NewClient(log, localMac, mac, host, port, token)
err := client.Open()
if err != nil {
return err
}

defer func() {
err2 := client.Close()
if err2 != nil {
log.Errorf("%v", err)
}
}()

var groups *sdk.Groups
err = retry(func() error {
var err2 error
groups, err2 = client.GetGroups()
return err2
})

if err != nil {
return err
}

log.Infof("Groups: %s", groups.String())

return nil
}
19 changes: 19 additions & 0 deletions cli/cmd/passwordChange.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"github.com/spf13/cobra"
)

// passwordChangeCmd represents the passwordChange command
var passwordChangeCmd = &cobra.Command{
Use: "passwordChange",
Short: "Change password of a gateway user",
Long: `Change password of a gateway user`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatalf("Not implemented yet")
},
}

func init() {
usersCmd.AddCommand(passwordChangeCmd)
}
70 changes: 8 additions & 62 deletions cli/cmd/users.go
Original file line number Diff line number Diff line change
@@ -1,71 +1,17 @@
package cmd

import (
"bisecur/cli"
"bisecur/sdk"
"github.com/spf13/viper"
"os"

"github.com/spf13/cobra"
)

func init() {
usersCmd := &cobra.Command{
Use: UsersCmdUse,
Short: "Manages users defined in your Hörmann BiSecur gateway.",
Long: ``,
PreRunE: preRunFuncs,
Run: func(cmd *cobra.Command, args []string) {
// TODO implement query user list and rights, add and delete user, password change of an already existing user

deviceMac := viper.GetString(ArgNameDeviceMac)
host := viper.GetString(ArgNameHost)
port := viper.GetInt(ArgNamePort)
token := viper.GetUint32(ArgNameToken)

mac, err := cli.ParesMacString(deviceMac)
if err != nil {
log.Fatalf("%v", err)
os.Exit(1)
}

err = listUsers(localMac, mac, host, port, token)
if err != nil {
log.Fatalf("%v", err)
os.Exit(2)
}
},
}

rootCmd.AddCommand(usersCmd)
var usersCmd = &cobra.Command{
Use: UsersCmdUse,
Short: "Manages users defined in your Hörmann BiSecur gateway.",
Long: ``,
PreRunE: preRunFuncs,
Run: nil,
}

func listUsers(localMac [6]byte, mac [6]byte, host string, port int, token uint32) error {
client := sdk.NewClient(log, localMac, mac, host, port, token)
err := client.Open()
if err != nil {
return err
}

defer func() {
err2 := client.Close()
if err2 != nil {
log.Errorf("%v", err)
}
}()

var users *sdk.Users
err = retry(func() error {
var err2 error
users, err2 = client.GetUsers()
return err2
})

if err != nil {
return err
}

log.Infof("Users: %s", users.String())

return nil
func init() {
rootCmd.AddCommand(usersCmd)
}
18 changes: 18 additions & 0 deletions cli/cmd/usersCreate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/spf13/cobra"
)

var usersCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a new gateway user",
Long: `Create a new gateway user`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatalf("Not implemented yet")
},
}

func init() {
usersCmd.AddCommand(usersCreateCmd)
}
18 changes: 18 additions & 0 deletions cli/cmd/usersDelete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/spf13/cobra"
)

var usersDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete a gateway user",
Long: `Delete a gateway user`,
Run: func(cmd *cobra.Command, args []string) {
log.Fatalf("Not implemented yet")
},
}

func init() {
usersCmd.AddCommand(usersDeleteCmd)
}
Loading

0 comments on commit f7700e8

Please sign in to comment.