Conditions | 38 |
Total Lines | 149 |
Code Lines | 103 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like action.ConfigWizard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package action |
||
20 | func ConfigWizard(base string) { |
||
21 | cache.SystemLock() |
||
22 | _, err := gpath.Glide() |
||
23 | glidefile := gpath.GlideFile |
||
24 | if err != nil { |
||
25 | msg.Info("Unable to find a glide.yaml file. Would you like to create one now? Yes (Y) or No (N)") |
||
26 | bres := msg.PromptUntilYorN() |
||
27 | if bres { |
||
28 | // Guess deps |
||
29 | conf := guessDeps(base, false) |
||
30 | // Write YAML |
||
31 | if err := conf.WriteFile(glidefile); err != nil { |
||
32 | msg.Die("Could not save %s: %s", glidefile, err) |
||
33 | } |
||
34 | } else { |
||
35 | msg.Err("Unable to find configuration file. Please create configuration information to continue.") |
||
36 | } |
||
37 | } |
||
38 | |||
39 | conf := EnsureConfig() |
||
40 | |||
41 | cache.Setup() |
||
42 | |||
43 | msg.Info("Looking for dependencies to make suggestions on") |
||
44 | msg.Info("--> Scanning for dependencies not using version ranges") |
||
45 | msg.Info("--> Scanning for dependencies using commit ids") |
||
46 | var deps []*cfg.Dependency |
||
47 | for _, dep := range conf.Imports { |
||
48 | if wizardLookInto(dep) { |
||
49 | deps = append(deps, dep) |
||
50 | } |
||
51 | } |
||
52 | for _, dep := range conf.DevImports { |
||
53 | if wizardLookInto(dep) { |
||
54 | deps = append(deps, dep) |
||
55 | } |
||
56 | } |
||
57 | |||
58 | msg.Info("Gathering information on each dependency") |
||
59 | msg.Info("--> This may take a moment. Especially on a codebase with many dependencies") |
||
60 | msg.Info("--> Gathering release information for dependencies") |
||
61 | msg.Info("--> Looking for dependency imports where versions are commit ids") |
||
62 | for _, dep := range deps { |
||
63 | wizardFindVersions(dep) |
||
64 | } |
||
65 | |||
66 | var changes int |
||
67 | for _, dep := range deps { |
||
68 | remote := dep.Remote() |
||
69 | |||
70 | // First check, ask if the tag should be used instead of the commit id for it. |
||
71 | cur := cache.MemCurrent(remote) |
||
72 | if cur != "" && cur != dep.Reference { |
||
73 | wizardSugOnce() |
||
74 | var dres bool |
||
75 | asked, use, val := wizardOnce("current") |
||
76 | if !use { |
||
77 | dres = wizardAskCurrent(cur, dep) |
||
78 | } |
||
79 | if !asked { |
||
80 | as := wizardRemember() |
||
81 | wizardSetOnce("current", as, dres) |
||
82 | } |
||
83 | |||
84 | if asked && use { |
||
85 | dres = val.(bool) |
||
86 | } |
||
87 | |||
88 | if dres { |
||
89 | msg.Info("Updating %s to use the tag %s instead of commit id %s", dep.Name, cur, dep.Reference) |
||
90 | dep.Reference = cur |
||
91 | changes++ |
||
92 | } |
||
93 | } |
||
94 | |||
95 | // Second check, if no version is being used and there's a semver release ask about latest. |
||
96 | memlatest := cache.MemLatest(remote) |
||
97 | if dep.Reference == "" && memlatest != "" { |
||
98 | wizardSugOnce() |
||
99 | var dres bool |
||
100 | asked, use, val := wizardOnce("latest") |
||
101 | if !use { |
||
102 | dres = wizardAskLatest(memlatest, dep) |
||
103 | } |
||
104 | if !asked { |
||
105 | as := wizardRemember() |
||
106 | wizardSetOnce("latest", as, dres) |
||
107 | } |
||
108 | |||
109 | if asked && use { |
||
110 | dres = val.(bool) |
||
111 | } |
||
112 | |||
113 | if dres { |
||
114 | msg.Info("Updating %s to use the release %s instead of no release", dep.Name, memlatest) |
||
115 | dep.Reference = memlatest |
||
116 | changes++ |
||
117 | } |
||
118 | } |
||
119 | |||
120 | // Third check, if the version is semver offer to use a range instead. |
||
121 | sv, err := semver.NewVersion(dep.Reference) |
||
122 | if err == nil { |
||
123 | wizardSugOnce() |
||
124 | var res string |
||
125 | asked, use, val := wizardOnce("range") |
||
126 | if !use { |
||
127 | res = wizardAskRange(sv, dep) |
||
128 | } |
||
129 | if !asked { |
||
130 | as := wizardRemember() |
||
131 | wizardSetOnce("range", as, res) |
||
132 | } |
||
133 | |||
134 | if asked && use { |
||
135 | res = val.(string) |
||
136 | } |
||
137 | |||
138 | if res == "m" { |
||
139 | r := "^" + sv.String() |
||
140 | msg.Info("Updating %s to use the range %s instead of commit id %s", dep.Name, r, dep.Reference) |
||
141 | dep.Reference = r |
||
142 | changes++ |
||
143 | } else if res == "p" { |
||
144 | r := "~" + sv.String() |
||
145 | msg.Info("Updating %s to use the range %s instead of commit id %s", dep.Name, r, dep.Reference) |
||
146 | dep.Reference = r |
||
147 | changes++ |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | |||
152 | if changes > 0 { |
||
153 | msg.Info("Configuration changes have been made. Would you like to write these") |
||
154 | msg.Info("changes to your configuration file? Yes (Y) or No (N)") |
||
155 | dres := msg.PromptUntilYorN() |
||
156 | if dres { |
||
157 | msg.Info("Writing updates to configuration file (%s)", glidefile) |
||
158 | if err := conf.WriteFile(glidefile); err != nil { |
||
159 | msg.Die("Could not save %s: %s", glidefile, err) |
||
160 | } |
||
161 | msg.Info("You can now edit the glide.yaml file.:") |
||
162 | msg.Info("--> For more information on versions and ranges see https://glide.sh/docs/versions/") |
||
163 | msg.Info("--> For details on additional metadata see https://glide.sh/docs/glide.yaml/") |
||
164 | } else { |
||
165 | msg.Warn("Change not written to configuration file") |
||
166 | } |
||
167 | } else { |
||
168 | msg.Info("No proposed changes found. Have a nice day.") |
||
169 | } |
||
358 |