-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclean.go
58 lines (52 loc) · 1.79 KB
/
clean.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
package main
import (
"flag"
"os"
"path/filepath"
"github.com/aws/ec2-macos-init/internal/paths"
"github.com/aws/ec2-macos-init/lib/ec2macosinit"
)
// clean removes old instance history. It has two options:
// current - This is the option when -all isn't provided. It only removes the current instance's history.
// all - When -all is provided, all instance history is removed.
func clean(baseDir string, c *ec2macosinit.InitConfig) {
// Define flags
cleanFlags := flag.NewFlagSet("clean", flag.ExitOnError)
cleanAll := cleanFlags.Bool("all", false, "Optional; Remove all instance history. Default is false.")
// Parse flags
err := cleanFlags.Parse(os.Args[2:])
if err != nil {
c.Log.Fatalf(64, "Unable to parse arguments: %s", err)
}
// Clean all or clean the current instance
historyPath := paths.AllInstancesHistory(baseDir)
if *cleanAll {
c.Log.Info("Removing all instance history")
// Read instance history directory
dir, err := os.ReadDir(historyPath)
if err != nil {
c.Log.Fatalf(66, "Unable to read instance history located at %s: %s", historyPath, err)
}
for _, d := range dir {
// Remove everything
err := os.RemoveAll(filepath.Join(historyPath, d.Name()))
if err != nil {
c.Log.Fatalf(1, "Unable to remove instance history: %s", err)
}
}
} else {
c.Log.Infof("Getting current instance ID from IMDS")
// Instance ID is needed, run setup
err = SetupInstanceID(c)
if err != nil {
c.Log.Fatalf(75, "Unable to get instance ID: %s", err)
}
c.Log.Infof("Removing history for the current instance [%s]", c.IMDS.InstanceID)
// Remove current instance history
err := os.RemoveAll(paths.InstanceHistory(baseDir, c.IMDS.InstanceID))
if err != nil {
c.Log.Fatalf(1, "Unable to remove instance history: %s", err)
}
}
c.Log.Info("Clean complete")
}