/
modtidy.go
96 lines (84 loc) · 2.58 KB
/
modtidy.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
// Copyright 2023 The CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"bytes"
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"cuelang.org/go/internal/mod/modload"
)
func newModTidyCmd(c *Command) *cobra.Command {
cmd := &cobra.Command{
Use: "tidy",
Short: "download and tidy module dependencies",
Long: `WARNING: THIS COMMAND IS EXPERIMENTAL.
Tidy resolves all module dependencies in the current module and updates
the cue.mod/module.cue file to reflect them.
It also removes dependencies that are not needed.
It will attempt to fetch modules that aren't yet present in the
dependencies by fetching the latest available version from
a registry.
See "cue help environment" for details on how $CUE_REGISTRY is used to
determine the modules registry.
Note: you must enable the modules experiment with:
export CUE_EXPERIMENT=modules
for this command to work.
`,
RunE: mkRunE(c, runModTidy),
Args: cobra.ExactArgs(0),
}
cmd.Flags().Bool(string(flagCheck), false, "check for tidiness only; do not update module.cue file")
return cmd
}
func runModTidy(cmd *Command, args []string) error {
reg, err := getCachedRegistry()
if err != nil {
return err
}
if reg == nil {
return fmt.Errorf("modules experiment not enabled (enable with CUE_EXPERIMENT=modules)")
}
ctx := backgroundContext()
modRoot, err := findModuleRoot()
if err != nil {
return err
}
if flagCheck.Bool(cmd) {
return modload.CheckTidy(ctx, os.DirFS(modRoot), ".", reg)
}
mf, err := modload.Tidy(ctx, os.DirFS(modRoot), ".", reg, versionForModFile())
if err != nil {
return err
}
data, err := mf.Format()
if err != nil {
return fmt.Errorf("internal error: invalid module.cue file generated: %v", err)
}
modPath := filepath.Join(modRoot, "cue.mod", "module.cue")
oldData, err := os.ReadFile(modPath)
if err != nil {
// Shouldn't happen because modload.Load returns an error
// if it can't load the module file.
return err
}
if bytes.Equal(data, oldData) {
return nil
}
if err := os.WriteFile(modPath, data, 0o666); err != nil {
return err
}
return nil
}