forked from gpmgo/gopm
/
remove.go
164 lines (146 loc) · 4.24 KB
/
remove.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2013 GPMGo Members. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"fmt"
"os"
"runtime"
"strings"
"github.com/GPMGo/gpm/doc"
"github.com/GPMGo/gpm/utils"
)
var (
removeCache map[string]bool // Saves packages that have been removed.
)
var cmdRemove = &Command{
UsageLine: "remove [remove flags] <packages|bundles|snapshots>",
}
func init() {
removeCache = make(map[string]bool)
cmdRemove.Run = runRemove
}
func runRemove(cmd *Command, args []string) {
// Check length of arguments.
if len(args) < 1 {
fmt.Printf(fmt.Sprintf("%s\n", promptMsg["NoPackage"]))
return
}
// Generate temporary nodes.
nodes := make([]*doc.Node, len(args))
for i := range nodes {
nodes[i] = new(doc.Node)
nodes[i].ImportPath = args[i]
}
// Removes packages.
removePackages(nodes)
// Save local nodes to file.
fw, err := os.Create(appPath + "data/nodes.json")
if err != nil {
fmt.Printf(fmt.Sprintf("ERROR: runRemove -> %s\n", promptMsg["OpenFile"]), err)
return
}
defer fw.Close()
fbytes, err := json.MarshalIndent(&localNodes, "", "\t")
if err != nil {
fmt.Printf(fmt.Sprintf("ERROR: runRemove -> %s\n", promptMsg["ParseJSON"]), err)
return
}
fw.Write(fbytes)
}
// removePackages removes packages from local file system.
func removePackages(nodes []*doc.Node) {
// Check all packages, they may be bundles, snapshots or raw packages path.
for _, n := range nodes {
// Check if it is a bundle or snapshot.
switch {
case strings.HasSuffix(n.ImportPath, ".b"):
l := len(n.ImportPath)
// Check local bundles.
bnodes := checkLocalBundles(n.ImportPath[:l-2])
if len(bnodes) > 0 {
// Check with users if continue.
fmt.Printf(fmt.Sprintf("%s\n", promptMsg["BundleInfo"]), n.ImportPath[:l-2])
for _, bn := range bnodes {
fmt.Printf("[%s] -> %s: %s.\n", bn.ImportPath, bn.Type, bn.Value)
}
fmt.Printf(fmt.Sprintf("%s\n", promptMsg["ContinueRemove"]))
var option string
fmt.Fscan(os.Stdin, &option)
if strings.ToLower(option) != "y" {
os.Exit(0)
}
removePackages(bnodes)
} else {
// Check from server.
// TODO: api.GetBundleInfo()
fmt.Println("Unable to find bundle, and we cannot check with server right now.")
}
case strings.HasSuffix(n.ImportPath, ".s"):
case utils.IsValidRemotePath(n.ImportPath):
if !removeCache[n.ImportPath] {
// Remove package.
node, imports := removePackage(n)
if len(imports) > 0 {
fmt.Println("Check denpendencies for removing package has not been supported.")
}
// Remove record in local nodes.
if node != nil {
removeNode(node)
}
}
default:
// Invalid import path.
fmt.Printf(fmt.Sprintf("%s\n", promptMsg["SkipInvalidPath"]), n.ImportPath)
}
}
}
// removeNode removes node from local nodes.
func removeNode(n *doc.Node) {
// Check if this node exists.
for i, v := range localNodes {
if n.ImportPath == v.ImportPath {
localNodes = append(localNodes[:i], localNodes[i+1:]...)
return
}
}
}
// removePackage removes package from local file system.
func removePackage(node *doc.Node) (*doc.Node, []string) {
// Find package in GOPATH.
paths := utils.GetGOPATH()
for _, p := range paths {
absPath := p + "/src/" + utils.GetProjectPath(node.ImportPath) + "/"
if utils.IsExist(absPath) {
fmt.Printf(fmt.Sprintf("%s\n", promptMsg["RemovePackage"]), node.ImportPath)
// Remove files.
os.RemoveAll(absPath)
// Remove file in GOPATH/bin
proName := utils.GetExecuteName(node.ImportPath)
paths := utils.GetGOPATH()
var gopath string
for _, v := range paths {
if utils.IsExist(v + "/bin/" + proName) {
gopath = v // Don't need to find again.
os.Remove(v + "/bin/" + proName)
}
}
pkgPath := "/pkg/" + runtime.GOOS + "_" + runtime.GOARCH + "/" + node.ImportPath
// Remove file in GOPATH/pkg
if len(gopath) == 0 {
for _, v := range paths {
if utils.IsExist(v + pkgPath + "/") {
gopath = v
}
}
}
os.RemoveAll(gopath + pkgPath + "/")
os.Remove(gopath + pkgPath + ".a")
return node, nil
}
}
// Cannot find package.
fmt.Printf(fmt.Sprintf("%s\n", promptMsg["PackageNotFound"]), node.ImportPath)
return nil, nil
}