-
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 pkg
Signed-off-by: Md Sahil <mohdssahil1@gmail.com>
- Loading branch information
1 parent
54d9504
commit 50c53ea
Showing
3 changed files
with
145 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
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,85 @@ | ||
package remove | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"kraftkit.sh/cmdfactory" | ||
"kraftkit.sh/packmanager" | ||
"kraftkit.sh/unikraft/app" | ||
) | ||
|
||
type Remove struct { | ||
Workdir string `long:"workdir" short:"w" usage:"workdir location to remove pkg from that location"` | ||
Kraftfile string `long:"kraftfile" usage:"Set an alternative path of the Kraftfile"` | ||
} | ||
|
||
func New() *cobra.Command { | ||
cmd, err := cmdfactory.New(&Remove{}, cobra.Command{ | ||
Short: "Remove unikraft library from the project directory", | ||
Use: "remove [FLAGS] [PACKAGE|DIR]", | ||
Annotations: map[string]string{ | ||
cmdfactory.AnnotationHelpGroup: "pkg", | ||
}, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func (opts *Remove) 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 *Remove) Run(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
return fmt.Errorf("Package Name is not specified to remove from the project.") | ||
} | ||
var workdir string | ||
var err error | ||
|
||
if len(args) > 1 { | ||
workdir = args[1] | ||
} else { | ||
workdir = opts.Workdir | ||
} | ||
|
||
if workdir == "." || workdir == "" { | ||
workdir, err = os.Getwd() | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
ctx := cmd.Context() | ||
|
||
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 | ||
} | ||
|
||
project.RemoveLibrary(ctx, args[0]) | ||
|
||
return project.Save() | ||
} |
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