forked from govend/govend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (134 loc) · 4.72 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
// Copyright 2016 govend. All rights reserved.
// Use of this source code is governed by an Apache 2.0
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"log"
"github.com/govend/govend/deps"
"github.com/govend/govend/imports"
"github.com/spf13/cobra"
)
var semver = "v0.1.10-beta"
// cli flag values
var (
version bool
verbose bool
tree bool
update bool
results bool
lock bool
hold bool
prune bool
ignore bool
scan bool
skipTestFiles bool
skipFilters bool
format string
)
// cli flag usage descriptions
const (
govendDesc = `The govend command scans and downloads dependencies.
`
versionDesc = `The --version flag prints the installed version of govend.
`
verboseDesc = `The -v flag prints package paths as they are vendored.
`
treeDesc = `The -t flag works with the -v flag to print the names of packages
as an indented tree.
`
resultsDesc = `The -r flag works with the -v flag to print a summary of the
number of packages scanned, packages skipped, and repositories downloaded.
`
formatDesc = `The -f flag works with the -l flag and -s flag to define a
format. The default format is YAML, but JSON and TOML is also supported.
`
updateDesc = `The -u flag uses the network to update packages and their
dependencies. By default the network is used to check out missing
packages and ensure correct revision versions.
`
lockDesc = `The -l flag locks down dependency versions by writing to disk a
manifest vendor file containing repository revision hashes.
`
holdDesc = `The --hold flag holds on to a dependency, even if it is not used
as an import path in the project codebase. This ability to freeze
dependencies is useful for vendoring Go tool versions per project.
`
pruneDesc = `The --prune flag removes vendored packages that are not needed
by leveraging the dependency tree after vendoring has completed.
`
ignoreDesc = `The --ignore flag ignores any packages that are NOT found in the
manifest file.
`
scanDesc = `The -s flag scans the current or provided directory for external
packages.
`
skipFiltersDesc = `The --skipFilters flag works with the -s flag to show the
raw unfiltered list of scanned packages.
`
skipTestFilesDesc = `The --skipTestFiles flag works with the -s flag and
default govend command to skip scanning files that end in "_test.go".
`
)
// govend represents the command root
var govend = &cobra.Command{
Use: "govend",
Short: "The govend command vendors external packages.",
Long: govendDesc,
Run: func(cmd *cobra.Command, args []string) {
switch {
case version:
fmt.Println(semver)
case scan:
// we should always assume the local directory unless a specific
// directory path is provided
path := "."
if len(args) > 0 {
path = args[0]
}
// parse flag options relevant to the scan command
sOpts := imports.ParseOptions(skipTestFiles, skipFilters)
pkgs, err := imports.Scan(path, sOpts...)
if err != nil {
log.Fatal(err)
}
b, err := imports.Format(pkgs, format)
if err != nil {
log.Fatal(err)
}
// always print the scan results to screen
fmt.Printf("%s\n", b)
default:
// the default govend command triggers vending since this is the most
// common use case.
// first we need to check that the current project environment is
// suitable for vendoring packages, otherwise the user will not get
// the results they expect when attempting `go build` or `go install`
if err := deps.Vendorable(verbose); err != nil {
log.Fatal(err)
}
// parse flag options relevant to the vend command
vOpts := deps.ParseOptions(update, lock, hold, prune, ignore, verbose, tree, results)
// vendor according to the options provided
if err := deps.Vend(args, format, vOpts...); err != nil {
log.Fatal(err)
}
}
},
}
func main() {
govend.Flags().BoolVar(&version, "version", false, versionDesc)
govend.Flags().BoolVarP(&verbose, "verbose", "v", false, verboseDesc)
govend.Flags().BoolVarP(&tree, "tree", "t", false, treeDesc)
govend.Flags().BoolVarP(&results, "results", "r", false, resultsDesc)
govend.Flags().StringVarP(&format, "format", "f", "YAML", formatDesc)
govend.Flags().BoolVarP(&update, "update", "u", false, updateDesc)
govend.Flags().BoolVarP(&lock, "lock", "l", false, lockDesc)
govend.Flags().BoolVar(&hold, "hold", false, holdDesc)
govend.Flags().BoolVar(&prune, "prune", false, pruneDesc)
govend.Flags().BoolVarP(&ignore, "ignore", "i", false, ignoreDesc)
govend.Flags().BoolVarP(&scan, "scan", "s", false, scanDesc)
govend.Flags().BoolVar(&skipFilters, "skipFilters", false, skipFiltersDesc)
govend.Flags().BoolVar(&skipTestFiles, "skipTestFiles", false, skipTestFilesDesc)
govend.Execute()
}