MCPcopy Index your code
hub / github.com/voidint/g / use

Function use

cli/use.go:33–90  ·  view source on GitHub ↗
(ctx *cli.Context)

Source from the content-addressed store, hash-verified

31)
32
33func use(ctx *cli.Context) error {
34 vname := ctx.Args().First()
35 if vname == "" {
36 // Uses go.mod if available and version is omitted
37 goModData, err := os.ReadFile("go.mod")
38 if err != nil {
39 if errors.Is(err, os.ErrNotExist) {
40 return cli.Exit(wrapstring("No go.mod file found"), 1)
41 }
42 return cli.Exit(errstring(err), 1)
43 }
44
45 goDirective := getGoDirective(goModData)
46 wd, err := os.Getwd()
47 if err != nil {
48 wd = "."
49 }
50 if goDirective == "" {
51 return cli.Exit(wrapstring(fmt.Sprintf("Go directive does not exist in %q files", filepath.Join(wd, "go.mod"))), 1)
52 }
53 fmt.Printf("Found %q with version <%s>\n", filepath.Join(wd, "go.mod"), goDirective)
54 vname = goDirective
55 }
56
57 versions, err := listLocalVersions(versionsDir)
58 if err != nil {
59 return cli.Exit(errstring(err), 1)
60 }
61
62 // Try to match the version number strictly first
63 for i := range versions {
64 if versions[i].Name() != vname {
65 continue
66 }
67 if err = switchVersion(versions[i].Name()); err != nil {
68 return cli.Exit(errstring(err), 1)
69 }
70 return nil
71 }
72
73 // Try fuzzy matching the version number again
74 cs, err := semver.NewConstraint(vname)
75 if err != nil {
76 return cli.Exit(errstring(err), 1)
77 }
78
79 for j := len(versions) - 1; j >= 0; j-- {
80 if !versions[j].MatchConstraint(cs) {
81 continue
82 }
83 if err = switchVersion(versions[j].Name()); err != nil {
84 return cli.Exit(errstring(err), 1)
85 }
86 return nil
87 }
88
89 return cli.Exit(wrapstring(fmt.Sprintf("The %q version does not exist, please install it first.", vname)), 1)
90}

Callers

nothing calls this directly

Calls 7

wrapstringFunction · 0.85
errstringFunction · 0.85
getGoDirectiveFunction · 0.85
listLocalVersionsFunction · 0.85
switchVersionFunction · 0.85
MatchConstraintMethod · 0.80
NameMethod · 0.65

Tested by

no test coverage detected