@@ -7,13 +7,16 @@ import (
77 "io"
88 "log"
99 "os"
10+ "path/filepath"
1011 "strings"
12+ "text/tabwriter"
1113
1214 "asdf/internal/config"
1315 "asdf/internal/exec"
1416 "asdf/internal/info"
1517 "asdf/internal/installs"
1618 "asdf/internal/plugins"
19+ "asdf/internal/resolve"
1720 "asdf/internal/shims"
1821 "asdf/internal/versions"
1922
@@ -45,6 +48,14 @@ func Execute(version string) {
4548 Usage : "The multiple runtime version manager" ,
4649 UsageText : usageText ,
4750 Commands : []* cli.Command {
51+ {
52+ Name : "current" ,
53+ Action : func (cCtx * cli.Context ) error {
54+ tool := cCtx .Args ().Get (0 )
55+
56+ return currentCommand (logger , tool )
57+ },
58+ },
4859 {
4960 Name : "exec" ,
5061 Action : func (cCtx * cli.Context ) error {
@@ -169,10 +180,114 @@ func Execute(version string) {
169180 }
170181}
171182
183+ // This function is a whole mess and needs to be refactored
184+ func currentCommand (logger * log.Logger , tool string ) error {
185+ conf , err := config .LoadConfig ()
186+ if err != nil {
187+ logger .Printf ("error loading config: %s" , err )
188+ return err
189+ }
190+
191+ currentDir , err := os .Getwd ()
192+ if err != nil {
193+ logger .Printf ("unable to get current directory: %s" , err )
194+ return err
195+ }
196+
197+ // settings here to match legacy implementation
198+ w := tabwriter .NewWriter (os .Stdout , 16 , 0 , 1 , ' ' , 0 )
199+
200+ if tool == "" {
201+ // show all
202+ allPlugins , err := plugins .List (conf , false , false )
203+ if err != nil {
204+ return err
205+ }
206+
207+ if len (allPlugins ) < 1 {
208+ fmt .Println ("No plugins installed" )
209+ return nil
210+ }
211+
212+ for _ , plugin := range allPlugins {
213+ toolversion , versionFound , versionInstalled := getVersionInfo (conf , plugin , currentDir )
214+ formatCurrentVersionLine (w , plugin , toolversion , versionFound , versionInstalled , err )
215+ }
216+ w .Flush ()
217+ return nil
218+ }
219+
220+ // show single tool
221+ plugin := plugins .New (conf , tool )
222+
223+ err = plugin .Exists ()
224+ _ , ok := err .(plugins.PluginMissing )
225+ pluginExists := ! ok
226+
227+ if pluginExists {
228+ toolversion , versionFound , versionInstalled := getVersionInfo (conf , plugin , currentDir )
229+ formatCurrentVersionLine (w , plugin , toolversion , versionFound , versionInstalled , err )
230+ w .Flush ()
231+ if ! versionFound {
232+ os .Exit (126 )
233+ }
234+
235+ if ! versionInstalled {
236+ os .Exit (1 )
237+ }
238+ } else {
239+ fmt .Printf ("No such plugin: %s\n " , tool )
240+ return err
241+ }
242+
243+ return nil
244+ }
245+
246+ func getVersionInfo (conf config.Config , plugin plugins.Plugin , currentDir string ) (resolve.ToolVersions , bool , bool ) {
247+ toolversion , found , _ := resolve .Version (conf , plugin , currentDir )
248+ installed := false
249+ if found {
250+ firstVersion := toolversion .Versions [0 ]
251+ versionType , version := versions .ParseString (firstVersion )
252+ installed = installs .IsInstalled (conf , plugin , versionType , version )
253+ }
254+ return toolversion , found , installed
255+ }
256+
257+ func formatCurrentVersionLine (w * tabwriter.Writer , plugin plugins.Plugin , toolversion resolve.ToolVersions , found bool , installed bool , err error ) error {
258+ if err != nil {
259+ return err
260+ }
261+
262+ fmt .Fprintf (w , "%s\t %s\t %s\n " , plugin .Name , formatVersions (toolversion .Versions ), formatSource (toolversion , plugin , found , installed ))
263+ return nil
264+ }
265+
266+ func formatSource (toolversion resolve.ToolVersions , plugin plugins.Plugin , found bool , installed bool ) string {
267+ if found {
268+ if ! installed {
269+ return fmt .Sprintf ("Not installed. Run \" asdf install %s %s\" " , plugin .Name , toolversion .Versions [0 ])
270+ }
271+ return filepath .Join (toolversion .Directory , toolversion .Source )
272+ }
273+ return fmt .Sprintf ("No version is set. Run \" asdf <global|shell|local> %s <version>\" " , plugin .Name )
274+ }
275+
276+ func formatVersions (versions []string ) string {
277+ switch len (versions ) {
278+ case 0 :
279+ return "______"
280+ case 1 :
281+ return versions [0 ]
282+ default :
283+ return strings .Join (versions , " " )
284+ }
285+ }
286+
172287func execCommand (logger * log.Logger , command string , args []string ) error {
173288 if command == "" {
174- logger .Printf ("no command specified " )
175- return fmt .Errorf ("no command specified " )
289+ logger .Printf ("usage: asdf exec <command> " )
290+ return fmt .Errorf ("usage: asdf exec <command> " )
176291 }
177292
178293 conf , err := config .LoadConfig ()
0 commit comments