-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
155 lines (134 loc) · 4.22 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
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
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/spf13/cobra"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigs := make(chan os.Signal)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
cancel()
}()
var (
excludeBuilds, excludeDirs []string
update, updateDeps bool
verbose bool
constraint bool
)
var cmdGet = &cobra.Command{
Use: "get [packages to import]",
Short: "Gets list of specified packages with its dependencies.",
Long: `get supports importing specific version of package (by tag, branch name or commit hash) and local packages`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var (
backupVendor bool
isUpdate = update || updateDeps
)
if isUpdate && vendorExists() {
if err := copyDir(context.Background(), manifest.VendorPath, manifest.VendorPath+".orig"); err != nil {
return fmt.Errorf("cannot backup vendor: %v", err)
}
backupVendor = true
}
newPkgs, err := Get(ctx, args, update, updateDeps, constraint, verbose)
if err == nil {
if backupVendor {
if err := os.RemoveAll(manifest.VendorPath + ".orig"); err != nil {
fmt.Printf("cannot delete backup: %v\n", err)
}
}
return nil
}
// restore origin vendor after fail.
if backupVendor {
if err := os.RemoveAll(manifest.VendorPath); err != nil {
return fmt.Errorf("cannot delete vendor: %v", err)
}
if err := os.Rename(manifest.VendorPath+".orig", manifest.VendorPath); err != nil {
return fmt.Errorf("cannot rename vendor.orig: %v", err)
}
return err
} else if !isUpdate {
for _, pkg := range newPkgs {
if err := os.RemoveAll(manifest.VendorPath + "/" + pkg); err != nil {
fmt.Printf("cannot remove pkg (%s): %v\n", pkg, err)
}
}
}
return err
},
}
cmdGet.Flags().BoolVarP(&verbose, "verbose", "v", false, "")
cmdGet.Flags().BoolVarP(&constraint, "constraint", "c", false, "add package with version to constraint")
cmdGet.Flags().BoolVarP(&update, "update", "u", false, "update package if exists")
cmdGet.Flags().BoolVarP(&update, "update-deps", "", false, "update package dependencies")
var cmdInstall = &cobra.Command{
Use: "install",
Short: "Install installs vendor dependencies from manifest.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
err := Install(ctx, verbose)
if !ctxCancelled(ctx) {
return err
}
os.RemoveAll(manifest.VendorPath)
return nil
},
}
cmdInstall.Flags().BoolVarP(&verbose, "verbose", "v", false, "")
var cmdInit = &cobra.Command{
Use: "init",
Short: "Init defines a manifest for current project.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
err := Init(ctx, excludeBuilds, excludeDirs)
if !ctxCancelled(ctx) {
return err
}
return nil
},
}
cmdInit.Flags().StringSliceVarP(&excludeBuilds, "exclude-builds", "", []string{"appenginevm", "appengine", "android", "integration", "ignore"}, "builds to exclude from import")
cmdInit.Flags().StringSliceVarP(&excludeDirs, "exclude-dirs", "", []string{"test", "_fixture", "integration"}, "directories to exclude from import")
var cmdFetch = &cobra.Command{
Use: "fetch",
Short: "Fetch fetches dependencies for current project.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
dir, err := os.Getwd()
if err != nil {
return fmt.Errorf("cannot get current directory: %v", err)
}
if vendorExists() {
return errors.New("vendor directory already exists")
}
err = Fetch(ctx, strings.TrimPrefix(dir, os.Getenv("GOPATH")+"/src/"), update, verbose)
if err != nil || ctxCancelled(ctx) {
os.RemoveAll(manifest.VendorPath)
}
return err
},
}
cmdFetch.Flags().BoolVarP(&verbose, "verbose", "v", false, "")
var rootCmd = &cobra.Command{Use: "ven"}
rootCmd.AddCommand(cmdInit, cmdFetch, cmdGet, cmdInstall)
rootCmd.Execute()
}
func ctxCancelled(сtx context.Context) bool {
select {
case <-сtx.Done():
return сtx.Err() == context.Canceled
default:
return false
}
}