-
Notifications
You must be signed in to change notification settings - Fork 941
Expand file tree
/
Copy pathset.go
More file actions
116 lines (96 loc) · 3.19 KB
/
Copy pathset.go
File metadata and controls
116 lines (96 loc) · 3.19 KB
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
// Package set provides the 'asdf set' command
package set
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/asdf-vm/asdf/internal/config"
"github.com/asdf-vm/asdf/internal/plugins"
"github.com/asdf-vm/asdf/internal/toolversions"
"github.com/asdf-vm/asdf/internal/versions"
)
// Main function is the entrypoint for the 'asdf set' command
func Main(_ io.Writer, stderr io.Writer, args []string, home bool, parents bool, homeFunc func() (string, error)) error {
if len(args) < 1 {
return printError(stderr, "tool and version must be provided as arguments")
}
if len(args) < 2 {
return printError(stderr, "version must be provided as an argument")
}
if home && parents {
return printError(stderr, "home and parents flags cannot both be specified; must be one location or the other")
}
conf, err := config.LoadConfig()
if err != nil {
return printError(stderr, fmt.Sprintf("error loading config: %s", err))
}
resolvedVersions := []string{}
for _, version := range args[1:] {
parsedVersion := toolversions.ParseFromCliArg(version)
if parsedVersion.Type == "latest" {
plugin := plugins.New(conf, args[0])
resolvedVersion, err := versions.Latest(plugin, parsedVersion.Value)
if err != nil {
return fmt.Errorf("unable to resolve latest version for %s", plugin.Name)
}
resolvedVersions = append(resolvedVersions, resolvedVersion)
continue
}
resolvedVersions = append(resolvedVersions, version)
}
tv := toolversions.ToolVersions{Name: args[0], Versions: resolvedVersions}
if home {
homeDir, err := homeFunc()
if err != nil {
return err
}
filepath := filepath.Join(homeDir, conf.DefaultToolVersionsFilename)
err = toolversions.WriteToolVersionsToFile(filepath, []toolversions.ToolVersions{tv})
if err != nil {
err = printError(stderr, fmt.Sprintf("error writing version file: %s", err))
}
return err
}
currentDir, err := os.Getwd()
if err != nil {
return printError(stderr, fmt.Sprintf("unable to get current directory: %s", err))
}
if parents {
// locate file in parents dir and update it
path, found := findVersionFileInParentDirs(conf, currentDir)
if !found {
return printError(stderr, fmt.Sprintf("No %s version file found in parent directories", conf.DefaultToolVersionsFilename))
}
err = toolversions.WriteToolVersionsToFile(path, []toolversions.ToolVersions{tv})
if err != nil {
err = printError(stderr, fmt.Sprintf("error writing version file: %s", err))
}
return err
}
// Write new file in current dir
filepath := filepath.Join(currentDir, conf.DefaultToolVersionsFilename)
return toolversions.WriteToolVersionsToFile(filepath, []toolversions.ToolVersions{tv})
}
func printError(stderr io.Writer, msg string) error {
if !strings.HasSuffix(msg, "\n") {
msg += "\n"
}
fmt.Fprint(stderr, msg)
return errors.New(strings.TrimSuffix(msg, "\n"))
}
func findVersionFileInParentDirs(conf config.Config, directory string) (string, bool) {
directory = filepath.Dir(directory)
for {
path := filepath.Join(directory, conf.DefaultToolVersionsFilename)
if _, err := os.Stat(path); err == nil {
return path, true
}
if directory == "/" {
return "", false
}
directory = filepath.Dir(directory)
}
}