-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend CLI interface with the new SDK capabilitis: user add, remove a…
…nd password change
- Loading branch information
Showing
9 changed files
with
228 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ func init() { | |
log.Fatalf("%v", err) | ||
os.Exit(2) | ||
} | ||
|
||
log.Infof("Success") | ||
}, | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |