-
Notifications
You must be signed in to change notification settings - Fork 5
feat: implement app cache deletion #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alessio-perugini
wants to merge
6
commits into
arduino:main
Choose a base branch
from
alessio-perugini:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4f25421
feat: implement app cache deletion
alessio-perugini ac57bdc
feature: add `--force` flag
alessio-perugini 137fcc3
feat: add `clean-cache` under app
alessio-perugini 015582e
feat: apply CR suggestion
alessio-perugini 2b3db88
Update internal/orchestrator/cache.go
alessio-perugini 12e7ccb
Update internal/orchestrator/cache.go
alessio-perugini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,68 @@ | ||
| package app | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/completion" | ||
| "github.com/arduino/arduino-app-cli/cmd/arduino-app-cli/internal/servicelocator" | ||
| "github.com/arduino/arduino-app-cli/cmd/feedback" | ||
| "github.com/arduino/arduino-app-cli/internal/orchestrator" | ||
| "github.com/arduino/arduino-app-cli/internal/orchestrator/app" | ||
| "github.com/arduino/arduino-app-cli/internal/orchestrator/config" | ||
| ) | ||
|
|
||
| func newCacheCleanCmd(cfg config.Configuration) *cobra.Command { | ||
| var forceClean bool | ||
| appCmd := &cobra.Command{ | ||
| Use: "clean-cache", | ||
| Short: "Delete app cache", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if len(args) == 0 { | ||
| return cmd.Help() | ||
| } | ||
alessio-perugini marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| app, err := Load(args[0]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return cacheCleanHandler(cmd.Context(), app, forceClean) | ||
| }, | ||
| ValidArgsFunction: completion.ApplicationNames(cfg), | ||
| } | ||
| appCmd.Flags().BoolVarP(&forceClean, "force", "", false, "Forcefully clean the cache even if the app is running") | ||
|
|
||
| return appCmd | ||
| } | ||
|
|
||
| func cacheCleanHandler(ctx context.Context, app app.ArduinoApp, forceClean bool) error { | ||
| err := orchestrator.CleanAppCache( | ||
| ctx, | ||
| servicelocator.GetDockerClient(), | ||
| app, | ||
| orchestrator.CleanAppCacheRequest{ForceClean: forceClean}, | ||
| ) | ||
| if err != nil { | ||
| feedback.Fatal(err.Error(), feedback.ErrGeneric) | ||
| } | ||
| feedback.PrintResult(cacheCleanResult{ | ||
| AppName: app.Name, | ||
| Path: app.ProvisioningStateDir().String(), | ||
| }) | ||
| return nil | ||
| } | ||
|
|
||
| type cacheCleanResult struct { | ||
| AppName string `json:"appName"` | ||
| Path string `json:"path"` | ||
| } | ||
|
|
||
| func (r cacheCleanResult) String() string { | ||
| return fmt.Sprintf("✓ Cache of %q App cleaned", r.AppName) | ||
| } | ||
|
|
||
| func (r cacheCleanResult) Data() interface{} { | ||
| return r | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,40 @@ | ||
| package orchestrator | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
|
|
||
| "github.com/docker/cli/cli/command" | ||
|
|
||
| "github.com/arduino/arduino-app-cli/internal/orchestrator/app" | ||
| ) | ||
|
|
||
| type CleanAppCacheRequest struct { | ||
| ForceClean bool | ||
| } | ||
|
|
||
| var ErrCleanCacheRunningApp = errors.New("cannot remove cache of a running app") | ||
|
|
||
| // CleanAppCache removes the `.cache` folder. If it detects that the app is running | ||
| // it tries to stop it first. | ||
| func CleanAppCache( | ||
| ctx context.Context, | ||
| docker command.Cli, | ||
| app app.ArduinoApp, | ||
| req CleanAppCacheRequest, | ||
| ) error { | ||
| runningApp, err := getRunningApp(ctx, docker.Client()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if runningApp.FullPath.EqualsTo(app.FullPath) { | ||
alessio-perugini marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return ErrCleanCacheRunningApp | ||
| } | ||
|
|
||
| if req.ForceClean && app.AppComposeFilePath().Exist() { | ||
| // We try to remove docker related resources at best effort | ||
| _ = StopAndDestroyApp(ctx, app) | ||
alessio-perugini marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
alessio-perugini marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return app.ProvisioningStateDir().RemoveAll() | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.