Commit 45cd0c5c authored by Richer Maximilien's avatar Richer Maximilien

[c] Add unregister cmd

parent eacf1637
Pipeline #2131 passed with stage
...@@ -58,6 +58,6 @@ func init() { ...@@ -58,6 +58,6 @@ func init() {
_ = viper.BindPFlag("timeout", RootCmd.PersistentFlags().Lookup("timeout")) _ = viper.BindPFlag("timeout", RootCmd.PersistentFlags().Lookup("timeout"))
// Bind subcommands to root // Bind subcommands to root
RootCmd.AddCommand(dfss.VersionCmd, registerCmd, authCmd, newCmd, showCmd, fetchCmd, importCmd, exportCmd, signCmd) RootCmd.AddCommand(dfss.VersionCmd, registerCmd, authCmd, newCmd, showCmd, fetchCmd, importCmd, exportCmd, signCmd, unregisterCmd)
} }
package cmd
import (
"fmt"
"os"
"dfss/dfssc/security"
"dfss/dfssc/user"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var unregisterCmd = &cobra.Command{
Use: "unregister",
Short: "delete current client information on platform",
Run: func(cmd *cobra.Command, args []string) {
// Read info from provided certificate
cert, err := security.GetCertificate(viper.GetString("file_cert"))
if err != nil {
fmt.Fprintln(os.Stderr, "An error occurred:", err.Error())
os.Exit(2)
}
// Confirmation
var ready string
readStringParam("Do you REALLY want to delete "+cert.Subject.CommonName+"? Type 'yes' to confirm", "", &ready)
if ready != "yes" {
fmt.Println("Unregistering aborted!")
os.Exit(1)
}
err = user.Unregister()
if err != nil {
fmt.Fprintln(os.Stderr, "Error: cannot unregister:", err.Error())
os.Exit(2)
}
},
}
package user
import (
"errors"
"golang.org/x/net/context"
"google.golang.org/grpc"
pb "dfss/dfssp/api"
"dfss/net"
)
// Unregister a user from the platform
func Unregister() error {
client, err := connect()
if err != nil {
return err
}
// Stop the context if it takes too long for the platform to answer
ctx, cancel := context.WithTimeout(context.Background(), net.DefaultTimeout)
defer cancel()
response, err := client.Unregister(ctx, &pb.Empty{})
if err != nil {
return errors.New(grpc.ErrorDesc(err))
}
if response.Code != pb.ErrorCode_SUCCESS {
return errors.New(response.Message)
}
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment