Skip to content

Commit

Permalink
feat(volume): Add inspect subcommand
Browse files Browse the repository at this point in the history
The inspect subcommand allows printing information about
a volume.

Signed-off-by: Luca Seritan <[email protected]>
  • Loading branch information
LucaSeri committed Apr 22, 2024
1 parent d7f004e commit b65b021
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
82 changes: 82 additions & 0 deletions internal/cli/kraft/volume/inspect/inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2024, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file expect in compliance with the License.
package inspect

import (
"context"
"encoding/json"
"fmt"

"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

volumeapi "kraftkit.sh/api/volume/v1alpha1"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/iostreams"
"kraftkit.sh/machine/volume"
)

type Inspect struct {
Driver string `noattribute:"true"`
}

func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&Inspect{}, cobra.Command{
Short: "Inspect a machine volume",
Use: "inspect VOLUME",
Aliases: []string{"get"},
Args: cobra.ExactArgs(1),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "volume",
},
Example: heredoc.Doc(`
# Inspect a volume
$ kraft volume inspect my-volume
`),
})
if err != nil {
panic(err)
}

return cmd
}

func (opts *Inspect) Pre(cmd *cobra.Command, _ []string) error {
opts.Driver = cmd.Flag("driver").Value.String()
return nil
}

func (opts *Inspect) Run(ctx context.Context, args []string) error {
var err error

strategy, ok := volume.Strategies()[opts.Driver]
if !ok {
return fmt.Errorf("unsupported network driver strategy: %v (contributions welcome!)", opts.Driver)
}

controller, err := strategy.NewVolumeV1alpha1(ctx)
if err != nil {
return err
}

volume, err := controller.Get(ctx, &volumeapi.Volume{
ObjectMeta: v1.ObjectMeta{
Name: args[0],
},
})
if err != nil {
return err
}

ret, err := json.Marshal(volume)
if err != nil {
return err
}

fmt.Fprintf(iostreams.G(ctx).Out, "%s\n", ret)

return nil
}
2 changes: 2 additions & 0 deletions internal/cli/kraft/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"kraftkit.sh/cmdfactory"
"kraftkit.sh/internal/cli/kraft/volume/create"
"kraftkit.sh/internal/cli/kraft/volume/inspect"
"kraftkit.sh/internal/set"
"kraftkit.sh/machine/volume"
)
Expand All @@ -36,6 +37,7 @@ func NewCmd() *cobra.Command {
}

cmd.AddCommand(create.NewCmd())
cmd.AddCommand(inspect.NewCmd())

return cmd
}
Expand Down

0 comments on commit b65b021

Please sign in to comment.