-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
129 lines (117 loc) · 3.23 KB
/
cmd.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
// Package cmd implements the command line interface for the godl tool.
package cmd
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"unicode"
"github.com/ericchiang/godl/internal/download"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
)
func indent(indent, s string) string {
s = strings.TrimSpace(s)
split := strings.Split(s, "\n")
for i, line := range split {
split[i] = indent + strings.TrimLeftFunc(line, unicode.IsSpace)
}
return strings.Join(split, "\n")
}
type options struct {
disableCache bool
dir string
debug bool
}
func (o *options) project() (*download.Project, error) {
dir := o.dir
if dir == "" {
cwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("get working directory: %v", err)
}
dir = cwd
}
var cache download.Cache
if o.disableCache {
cache = download.NoCache
} else {
home, err := homedir.Dir()
if err != nil {
return nil, fmt.Errorf("could not find home directory: %v", err)
}
cache = download.NewCache(filepath.Join(home, ".godl"))
}
return &download.Project{Dir: dir, Cache: cache}, nil
}
// New returns a new instance of the godl command.
func New() *cobra.Command {
o := new(options)
l := log.New(os.Stderr, "", 0)
c := &cobra.Command{
Use: "godl [sub-command]",
Short: "A Go vendoring tool that allows incremental changes to dependencies.",
Long: indent("", `
godl is a vendoring tool that lets users download dependencies one at a
time. Unlike other tools, it does no inspection of source files in a
project, reducing the overhead of expensive operations and corner cases.
`),
}
c.AddCommand(cmdVendor(o, l))
c.AddCommand(cmdImport(o, l))
c.PersistentFlags().BoolVar(&o.disableCache, "disable-cache", false,
"Disable download cache.")
c.PersistentFlags().StringVar(&o.dir, "dir", "",
"Directory to operate in. Defaults to the current directory.")
c.PersistentFlags().BoolVarP(&o.debug, "verbose", "v", false,
"Enable verbose logging.")
return c
}
func cmdVendor(o *options, l *log.Logger) *cobra.Command {
c := &cobra.Command{
Use: "vendor",
Short: "Download dependencies to the vendor directory",
Long: indent("", `
Load the manifest file and compare it against the lock file for any dependencies
that need downloading, removal, or updating. Dependencies are then modified one
at a time.
`),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("surplus arguments")
}
p, err := o.project()
if err != nil {
return err
}
return downloadAll(p, l)
},
}
return c
}
func cmdImport(o *options, l *log.Logger) *cobra.Command {
c := &cobra.Command{
Use: "import",
Short: "Import dependencies from an existing package management file",
Example: indent(" ", `
godl import Godeps/Godeps.json
godl import glide.yaml
`),
Long: indent("", `
Inspect an existing manifest file from another package manager. Supported
tools are godeps, glide, and gvt.
`),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("import command requires argument")
}
p, err := o.project()
if err != nil {
return err
}
return importManifest(p, l, args[0])
},
}
return c
}