-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(volume): Add inspect subcommand
The inspect subcommand allows printing information about a volume. Signed-off-by: Luca Seritan <[email protected]>
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 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
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 | ||
} |
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