Add delete to client

This commit is contained in:
Torjus Håkestad 2021-12-04 11:42:44 +01:00
parent f56e3e7bff
commit 7245a9a8cc

View File

@ -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)
}
}