-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
108 lines (94 loc) · 3.28 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"errors"
"fmt"
"os"
"github.com/fatih/color"
"github.com/jessevdk/go-flags"
)
//Options
var opts struct {
Version bool `short:"v" long:"version" description:"Current version of xcclear."`
Diagnose bool `short:"d" long:"diagnose" description:"Diagnose the total space occupied by derived data, caches and other XCode related files."`
Purge bool `short:"p" long:"purge" description:"Clean all caches/unnecessary files. \nThis will not delete your 'Archives' folder and the last installed iOS version in the 'iOS DeviceSupport' folder."`
Clear string `short:"c" long:"clear" description:"Clean only the specified directories. \n Options can be:\n -'derived': The 'Derived Data' folder. \n -'device': The 'iOS DeviceSupport' folder. (Will keep the latest iOS version.) \n -'watch': 'watchOS DeviceSupport' folder. \n -'simulator': 'CoreSimulator' folder. \n -'cache': XCode caches. \n -'archives': 'Archives' folder." choice:"derived" choice:"device" choice:"watch" choice:"simulator" choice:"cache" choice:"archives"`
}
//Colors used for logging messages
var blue = color.New(color.FgBlue).Add(color.Bold).SprintFunc()
var red = color.New(color.FgRed).Add(color.Bold).SprintFunc()
var green = color.New(color.FgGreen).Add(color.Bold).SprintFunc()
func main() {
_, err := flags.Parse(&opts)
//Couldn't parse flags
if err != nil {
panic(err)
}
//Xcode not installed
xcInstalled, error := exists(xcodeDir())
if error != nil || !xcInstalled {
panic("Xcode is not installed!")
}
//No flags provided error
if !opts.Version && !opts.Diagnose && !opts.Purge && opts.Clear == "" {
panic(errors.New("No flags provided. See -h or --help for possible options."))
}
//More than one flags error
setFlags := []bool{opts.Version, opts.Diagnose, opts.Purge, opts.Clear != ""}
if CountOf(true, setFlags) > 1 {
panic(errors.New("More than one flag. Please pass in only one option at a time. See -h or --help for possible options."))
}
//Version
if opts.Version {
fmt.Println("1.0.1")
os.Exit(0)
}
//Diagnosing XCode space
if opts.Diagnose {
DiagnoseAll()
defer os.Exit(0)
}
//Clean everything
if opts.Purge {
Purge()
defer os.Exit(0)
}
//clean specified folder
switch opts.Clear {
case "derived":
size, _ := DirSize(DerivedData())
sizeMb := SizeInMB(size)
defer SizeMsg(sizeMb)
CleanDirContents(DerivedData())
case "device":
size, _ := DirSize(DeviceSupport())
realSize := size - DeviceSupportLatestVerDirSize()
sizeMb := SizeInMB(realSize)
defer SizeMsg(sizeMb)
CleanDirContents(DeviceSupport())
case "watch":
size, _ := DirSize(WatchOSSupport())
sizeMb := SizeInMB(size)
defer SizeMsg(sizeMb)
CleanDirContents(WatchOSSupport())
case "simulator":
size, _ := DirSize(CoreSimulator())
sizeMb := SizeInMB(size)
defer SizeMsg(sizeMb)
CleanDirContents(CoreSimulator())
case "cache":
size, _ := DirSize(XcCache())
sizeMb := SizeInMB(size)
defer SizeMsg(sizeMb)
CleanDirContents(XcCache())
case "archives":
fmt.Println("This will delete all files in your Archives. Please make sure you don't need any of those.\nDo you wish to continue? (yes/no)")
confirm := askForConfirmation()
if !confirm {
os.Exit(0)
}
size, _ := DirSize(Archives())
sizeMb := SizeInMB(size)
defer SizeMsg(sizeMb)
CleanDirContents(Archives())
}
}