-
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: Added a subcommand show for pkg
Signed-off-by: sahil <mohdssahil1@gmail.com>
- Loading branch information
1 parent
3f3ded3
commit baf8395
Showing
7 changed files
with
160 additions
and
8 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
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,95 @@ | ||
package show | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/MakeNowJust/heredoc" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v2" | ||
"kraftkit.sh/cmdfactory" | ||
"kraftkit.sh/iostreams" | ||
"kraftkit.sh/manifest" | ||
"kraftkit.sh/packmanager" | ||
) | ||
|
||
type Show struct { | ||
Output string `long:"output" short:"o" usage:"Set output format" default:"yaml"` | ||
Cache bool `long:"local" short:"l" usage:"Search package details locally" default:"false"` | ||
} | ||
|
||
func New() *cobra.Command { | ||
cmd, err := cmdfactory.New(&Show{}, cobra.Command{ | ||
Short: "Shows a Unikraft package", | ||
Use: "show [FLAGS] [PACKAGE|DIR]", | ||
Aliases: []string{"sw"}, | ||
Long: heredoc.Doc(` | ||
Shows a Unikraft package like library, core etc details | ||
`), | ||
Example: heredoc.Doc(` | ||
# Shows details for the library nginx | ||
$ kraft pkg show nginx`), | ||
Annotations: map[string]string{ | ||
cmdfactory.AnnotationHelpGroup: "pkg", | ||
}, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func (opts *Show) Pre(cmd *cobra.Command, _ []string) error { | ||
ctx := cmd.Context() | ||
pm, err := packmanager.NewUmbrellaManager(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cmd.SetContext(packmanager.WithPackageManager(ctx, pm)) | ||
|
||
return nil | ||
} | ||
|
||
func (opts *Show) Run(cmd *cobra.Command, args []string) error { | ||
opts.Output = strings.ToLower(opts.Output) | ||
if len(args) == 0 { | ||
return fmt.Errorf("Package name is not specified to show information") | ||
} else if opts.Output != "json" && opts.Output != "yaml" { | ||
return fmt.Errorf("Specified output format is not supported") | ||
} | ||
|
||
ctx := cmd.Context() | ||
|
||
metadata, err := packmanager.G(ctx).Show(ctx, opts.Output, packmanager.WithCache(opts.Cache), packmanager.WithName(args[0])) | ||
|
||
if metadata != nil { | ||
var byteCode []byte | ||
var manifestStruct *manifest.Manifest | ||
if opts.Cache { | ||
manifestStruct, err = manifest.NewManifestFromFile(ctx, reflect.ValueOf(metadata).Elem().FieldByName("Origin").String()) | ||
} else { | ||
manifestStruct, err = manifest.NewManifestFromURL(ctx, reflect.ValueOf(metadata).Elem().FieldByName("Origin").String()) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
if opts.Output == "json" { | ||
byteCode, err = json.Marshal(manifestStruct) | ||
} else { | ||
byteCode, err = yaml.Marshal(manifestStruct) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprint(iostreams.G(ctx).Out, string(byteCode)+"\n") | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
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
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
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
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
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