From 7245a9a8cc17d9ed1287b7c2c2df94b5e04f1e51 Mon Sep 17 00:00:00 2001 From: = Date: Sat, 4 Dec 2021 11:42:44 +0100 Subject: [PATCH] Add delete to client --- cmd/ezshare.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/cmd/ezshare.go b/cmd/ezshare.go index a7ccaab..c1ad365 100644 --- a/cmd/ezshare.go +++ b/cmd/ezshare.go @@ -31,6 +31,11 @@ func main() { Name: "config", Usage: "Path to config-file.", }, + &cli.BoolFlag{ + Name: "verbose", + Aliases: []string{"v"}, + Usage: "Be more verbose", + }, }, Commands: []*cli.Command{ { @@ -82,6 +87,12 @@ func main() { ArgsUsage: "PATH [PATH]..", Action: ActionClientUpload, }, + { + Name: "delete", + Usage: "Delete file with id", + ArgsUsage: "ID [ID]..", + Action: ActionClientDelete, + }, { Name: "list", Usage: "List files", @@ -372,6 +383,39 @@ func ActionClientList(c *cli.Context) error { return nil } +func ActionClientDelete(c *cli.Context) error { + cfg, err := getConfig(c) + if err != nil { + return err + } + + addr := cfg.Client.DefaultServer + if c.IsSet("addr") { + addr = c.String("addr") + } + + clientCreds, err := cfg.Client.Creds() + if err != nil { + return err + } + conn, err := grpc.DialContext(c.Context, addr, grpc.WithTransportCredentials(clientCreds)) + + if err != nil { + return err + } + defer conn.Close() + + client := pb.NewFileServiceClient(conn) + for _, arg := range c.Args().Slice() { + _, err := client.DeleteFile(c.Context, &pb.DeleteFileRequest{Id: arg}) + if err != nil { + return fmt.Errorf("error deleting file: %w", err) + } + fmt.Printf("Deleted file %s\n", arg) + } + return nil +} + func ActionGencerts(c *cli.Context) error { outDir := "." if c.IsSet("out-dir") { @@ -396,8 +440,13 @@ func getConfig(c *cli.Context) (*config.Config, error) { } cfg, err := config.FromDefaultLocations() if err == nil { - fmt.Printf("Config loaded from %s\n", cfg.Location()) - fmt.Printf("Config: %+v\n", cfg) + verbosePrint(c, fmt.Sprintf("Config loaded from %s", cfg.Location())) } return cfg, err } + +func verbosePrint(c *cli.Context, message string) { + if c.Bool("verbose") { + fmt.Println(message) + } +}