-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added new subcommand remove to commmad kraft lib
Signed-off-by: Md Sahil <mohdssahil1@gmail.com>
- Loading branch information
1 parent
9c23f29
commit d563d03
Showing
5 changed files
with
177 additions
and
1 deletion.
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,39 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors. | ||
// Licensed under the BSD-3-Clause License (the "License"). | ||
// You may not use this file except in compliance with the License. | ||
package lib | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"kraftkit.sh/cmdfactory" | ||
"kraftkit.sh/internal/cli/kraft/lib/remove" | ||
) | ||
|
||
type Lib struct{} | ||
|
||
func NewCmd() *cobra.Command { | ||
cmd, err := cmdfactory.New(&Lib{}, cobra.Command{ | ||
Short: "Manage and maintain Unikraft microlibraries", | ||
Use: "lib SUBCOMMAND", | ||
Aliases: []string{"library"}, | ||
Hidden: true, | ||
Annotations: map[string]string{ | ||
cmdfactory.AnnotationHelpGroup: "lib", | ||
}, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
cmd.AddCommand(remove.NewCmd()) | ||
|
||
return cmd | ||
} | ||
|
||
func (opts *Lib) Run(ctx context.Context, args []string) error { | ||
return pflag.ErrHelp | ||
} |
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,89 @@ | ||
package remove | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"kraftkit.sh/cmdfactory" | ||
"kraftkit.sh/packmanager" | ||
"kraftkit.sh/unikraft/app" | ||
) | ||
|
||
type RemoveOptions struct { | ||
Workdir string `long:"workdir" short:"w" usage:"workdir to remove lib from"` | ||
Kraftfile string `long:"kraftfile" short:"K" usage:"Set an alternative path of the Kraftfile"` | ||
} | ||
|
||
// Remove a Unikraft library from the project directory. | ||
func Remove(ctx context.Context, opts *RemoveOptions, args ...string) error { | ||
if opts == nil { | ||
opts = &RemoveOptions{} | ||
} | ||
|
||
return opts.Run(ctx, args) | ||
} | ||
|
||
func NewCmd() *cobra.Command { | ||
cmd, err := cmdfactory.New(&RemoveOptions{}, cobra.Command{ | ||
Short: "Removes a library dependency from the project directory", | ||
Use: "remove [FLAGS] LIB", | ||
Aliases: []string{"rm"}, | ||
Args: cmdfactory.MinimumArgs(1, "library name is not specified to remove from the project"), | ||
Annotations: map[string]string{ | ||
cmdfactory.AnnotationHelpGroup: "lib", | ||
}, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func (opts *RemoveOptions) Pre(cmd *cobra.Command, _ []string) error { | ||
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context()) | ||
if err != nil { | ||
return err | ||
} | ||
cmd.SetContext(ctx) | ||
return nil | ||
} | ||
|
||
func (opts *RemoveOptions) Run(ctx context.Context, args []string) error { | ||
var workdir string | ||
var err error | ||
|
||
if len(opts.Workdir) > 0 { | ||
workdir = opts.Workdir | ||
} | ||
|
||
if workdir == "." || workdir == "./" || workdir == "" { | ||
workdir, err = os.Getwd() | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
popts := []app.ProjectOption{} | ||
|
||
if len(opts.Kraftfile) > 0 { | ||
popts = append(popts, app.WithProjectKraftfile(opts.Kraftfile)) | ||
} else { | ||
popts = append(popts, app.WithProjectDefaultKraftfiles()) | ||
} | ||
|
||
project, err := app.NewProjectFromOptions( | ||
ctx, | ||
append(popts, app.WithProjectWorkdir(workdir))..., | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = project.RemoveLibrary(ctx, args[0]); err != nil { | ||
return err | ||
} | ||
|
||
return project.Save(ctx) | ||
} |
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 |
---|---|---|
|
@@ -34,4 +34,5 @@ const ( | |
// Built-in paths | ||
VendorDir = ".unikraft" | ||
BuildDir = ".unikraft/build" | ||
LibsDir = ".unikraft/libs" | ||
) |