forked from kardianos/govendor
/
modify.go
161 lines (149 loc) · 4.11 KB
/
modify.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
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package run
import (
"errors"
"flag"
"fmt"
"io"
"github.com/kardianos/govendor/context"
"github.com/kardianos/govendor/help"
"github.com/kardianos/govendor/prompt"
)
func (r *runner) Modify(w io.Writer, subCmdArgs []string, mod context.Modify, ask prompt.Prompt) (help.HelpMessage, error) {
msg := help.MsgFull
switch mod {
case context.Add:
msg = help.MsgAdd
case context.Update, context.AddUpdate:
msg = help.MsgUpdate
case context.Remove:
msg = help.MsgRemove
case context.Fetch:
msg = help.MsgFetch
}
var err error
/*
// Fake example prompt.
q := &prompt.Question{
Error: "An error goes here",
Prompt: "Do you want to do this?",
Type: prompt.TypeSelectOne,
Options: []prompt.Option{
prompt.NewOption("yes", "Yes, continue", false),
prompt.NewOption("no", "No, stop", false),
prompt.NewOption("", "What?", true),
},
}
q.Options[2].Chosen = true
q.Options[2] = prompt.ValidateOption(q.Options[2], "Choose again!")
resp, err := ask.Ask(q)
if err != nil {
return msg, err
}
if resp == prompt.RespCancel {
fmt.Printf("Cancelled\n")
return help.MsgNone, nil
}
chosen := q.AnswerSingle(true)
fmt.Printf("Chosen: %s\n", chosen.String())
*/
listFlags := flag.NewFlagSet("mod", flag.ContinueOnError)
listFlags.SetOutput(nullWriter{})
dryrun := listFlags.Bool("n", false, "dry-run")
verbose := listFlags.Bool("v", false, "verbose")
short := listFlags.Bool("short", false, "choose the short path")
long := listFlags.Bool("long", false, "choose the long path")
tree := listFlags.Bool("tree", false, "copy all folders including and under selected folder")
insecure := listFlags.Bool("insecure", false, "allow insecure network updates")
uncommitted := listFlags.Bool("uncommitted", false, "allows adding uncommitted changes. Doesn't update revision or checksum")
err = listFlags.Parse(subCmdArgs)
if err != nil {
return msg, err
}
if *short && *long {
return help.MsgNone, errors.New("cannot select both long and short path")
}
args := listFlags.Args()
if len(args) == 0 {
return msg, errors.New("missing package or status")
}
ctx, err := r.NewContextWD(context.RootVendor)
if err != nil {
return checkNewContextError(err)
}
if *verbose {
ctx.Logger = w
}
ctx.Insecure = *insecure
cgp, err := currentGoPath(ctx)
if err != nil {
return msg, err
}
f, err := parseFilter(cgp, args)
if err != nil {
return msg, err
}
mops := make([]context.ModifyOption, 0, 3)
if *uncommitted {
mops = append(mops, context.Uncommitted)
}
if *tree {
mops = append(mops, context.IncludeTree)
}
// Add explicit imports.
for _, imp := range f.Import {
err = ctx.ModifyImport(imp, mod, mops...)
if err != nil {
return help.MsgNone, err
}
}
err = ctx.ModifyStatus(f.Status, mod, mops...)
if err != nil {
return help.MsgNone, err
}
// Auto-resolve package conflicts.
conflicts := ctx.Check()
conflicts = ctx.ResolveAutoVendorFileOrigin(conflicts)
if *long {
conflicts = context.ResolveAutoLongestPath(conflicts)
}
if *short {
conflicts = context.ResolveAutoShortestPath(conflicts)
}
ctx.ResloveApply(conflicts)
// TODO: loop through conflicts to see if there are any remaining conflicts.
// Print out any here.
if *dryrun {
for _, op := range ctx.Operation {
switch op.Type {
case context.OpRemove:
fmt.Fprintf(w, "Remove %q\n", op.Src)
case context.OpCopy:
fmt.Fprintf(w, "Copy %q -> %q\n", op.Src, op.Dest)
for _, ignore := range op.IgnoreFile {
fmt.Fprintf(w, "\tIgnore %q\n", ignore)
}
case context.OpFetch:
fmt.Fprintf(w, "Fetch %q\n", op.Src)
}
}
return help.MsgNone, nil
}
// Write intent, make the changes, then record any checksums or recursive info.
err = ctx.WriteVendorFile()
if err != nil {
return help.MsgNone, err
}
// Write out vendor file and do change.
err = ctx.Alter()
vferr := ctx.WriteVendorFile()
if err != nil {
return help.MsgNone, err
}
if vferr != nil {
return help.MsgNone, vferr
}
return help.MsgNone, nil
}