-
Notifications
You must be signed in to change notification settings - Fork 9
/
unprepare.go
73 lines (56 loc) · 2.02 KB
/
unprepare.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cmd
import (
"context"
"fmt"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/build/buildkit"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/compute/cache"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/console/tui"
"namespacelabs.dev/foundation/internal/parsing/devhost"
"namespacelabs.dev/foundation/internal/unprepare"
)
func NewUnprepareCmd() *cobra.Command {
var pruneCache bool
cmd := &cobra.Command{
Use: "unprepare",
Short: "Removes Namespace-created docker containers and user-level caches.",
Args: cobra.NoArgs,
RunE: fncobra.RunE(func(ctx context.Context, args []string) error {
result, err := tui.Ask(ctx, "Do you want to remove all of Namespace locally managed resources?",
`If you've run Namespace before, various resources were setup in your
workstation, including a result cache, but most importantly a series of
containers running within your Docker instance.
Do you wish to remove these?
Type "unprepare" for them to be removed.`, "")
if result != "unprepare" {
return context.Canceled
}
if err != nil {
return err
}
// Remove k3d cluster(s) and registry(ies).
if err := unprepare.UnprepareK3d(ctx); err != nil {
return err
}
// Stop and remove the builtkit daemon container.
if err := buildkit.RemoveBuildkitd(ctx); err != nil {
return err
}
if pruneCache {
// Prune cached build artifacts and command history artifacts.
if err := cache.Prune(ctx); err != nil {
return err
}
}
fmt.Fprintf(console.Stdout(ctx), "The contents of your %q are no longer valid.\n", devhost.DevHostFilename)
return nil
}),
}
cmd.Flags().BoolVar(&pruneCache, "prune_cache", pruneCache, "If set, also triggers a global cache prune.")
return cmd
}