Skip to content

Commit

Permalink
Extend CLI interface with the new SDK capabilitis: user add, remove a…
Browse files Browse the repository at this point in the history
…nd password change
  • Loading branch information
halacs committed Jan 20, 2024
1 parent 0cfe4da commit 5d28989
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 50 deletions.
2 changes: 2 additions & 0 deletions cli/cmd/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import "time"

const (
ArgNameUserId = "uid"
ArgNameNewPassword = "newpassword"
ArgNameToken = "token"
ArgNameUsername = "username"
ArgNamePassword = "password"
Expand Down
2 changes: 2 additions & 0 deletions cli/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func init() {
log.Fatalf("%v", err)
os.Exit(2)
}

log.Infof("Success")
},
}

Expand Down
2 changes: 2 additions & 0 deletions cli/cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func init() {
if err != nil {
log.Errorf("Failed to save new configuration. %v", err)
}

log.Infof("Success")
},
}

Expand Down
77 changes: 67 additions & 10 deletions cli/cmd/passwordChange.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,76 @@
package cmd

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

// 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() {
var (
userId int
newPassword string
)

passwordChangeCmd := &cobra.Command{
Use: "password-change",
Short: "Change password of a gateway user",
Long: `Change password of a gateway user`,
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 = userPasswordChange(localMac, mac, host, port, token, byte(userId), newPassword)
if err != nil {
log.Fatalf("%v", err)
os.Exit(2)
}

log.Infof("Password has been changed")
},
}

usersCmd.AddCommand(passwordChangeCmd)

passwordChangeCmd.Flags().IntVar(&userId, ArgNameUserId, 0, "ID of the user")
passwordChangeCmd.MarkFlagsOneRequired(ArgNameUserId)

passwordChangeCmd.Flags().StringVar(&newPassword, ArgNameNewPassword, "", "new password")
passwordChangeCmd.MarkFlagsOneRequired(ArgNameNewPassword)
}

func userPasswordChange(localMac [6]byte, mac [6]byte, host string, port int, token uint32, userId byte, newPassword string) 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)
}
}()

err = retry(func() error {
err2 := client.PasswordChange(userId, newPassword)
return err2
})

if err != nil {
return err
}

return nil
}
4 changes: 2 additions & 2 deletions cli/cmd/setState.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func init() {
log.Fatalf("%v", err)
os.Exit(2)
}

log.Infof("Success")
},
}

Expand Down Expand Up @@ -66,7 +68,5 @@ func setStatus(localMac [6]byte, mac [6]byte, host string, port int, devicePort
return err
}

log.Infof("Done")

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

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

func init() {
var (
userName string
password string
)

usersCreateCmd := &cobra.Command{
Use: "add",
Short: "Create a new gateway user",
Long: `Create a new gateway user`,
PreRunE: preRunFuncs,
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)
}

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

log.Infof("User has been added. User ID: %d", userId)
},
}

usersCmd.AddCommand(usersCreateCmd)

usersCreateCmd.Flags().StringVar(&userName, ArgNameUsername, "", "name of the new user")
usersCreateCmd.MarkFlagsOneRequired(ArgNameUsername)

usersCreateCmd.Flags().StringVar(&password, ArgNameNewPassword, "", "password of the new user")
usersCreateCmd.MarkFlagsOneRequired(ArgNameNewPassword)
}

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

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

var userId byte
err = retry(func() error {
var err2 error
userId, err2 = client.AddUser(userName, password)
return err2
})

if err != nil {
return 0, err
}

return userId, nil
}
19 changes: 0 additions & 19 deletions cli/cmd/usersCreate.go

This file was deleted.

19 changes: 0 additions & 19 deletions cli/cmd/usersDelete.go

This file was deleted.

74 changes: 74 additions & 0 deletions cli/cmd/usersRemove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cmd

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

func init() {
var (
userId int
)

usersDeleteCmd := &cobra.Command{
Use: "remove",
Short: "Delete a gateway user",
Long: `Delete a gateway user`,
PreRunE: preRunFuncs,
Run: func(cmd *cobra.Command, args []string) {
deviceMac := viper.GetString(ArgNameDeviceMac)
host := viper.GetString(ArgNameHost)
port := viper.GetInt(ArgNamePort)
token := viper.GetUint32(ArgNameToken)
userId := viper.GetInt(ArgNameUserId)

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

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

log.Infof("Password has been removed")
},
}

usersCmd.AddCommand(usersDeleteCmd)

usersDeleteCmd.Flags().IntVar(&userId, ArgNameUserId, 0, "ID of the user to be deleted")
usersDeleteCmd.MarkFlagsOneRequired(ArgNameUserId)
}

func userRemove(localMac [6]byte, mac [6]byte, host string, port int, token uint32, userId byte) 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)
}
}()

err = retry(func() error {
err2 := client.RemoveUser(userId)
return err2
})

if err != nil {
return err
}

return nil
}

0 comments on commit 5d28989

Please sign in to comment.