forked from cue-lang/cue
/
fix.go
130 lines (109 loc) · 3.15 KB
/
fix.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2019 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 (
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/wylswz/cue-se/cue/ast"
"github.com/wylswz/cue-se/cue/errors"
"github.com/wylswz/cue-se/cue/format"
"github.com/wylswz/cue-se/cue/load"
"github.com/wylswz/cue-se/cue/token"
"github.com/wylswz/cue-se/tools/fix"
"github.com/spf13/cobra"
)
func newFixCmd(c *Command) *cobra.Command {
cmd := &cobra.Command{
Use: "fix [packages]",
Short: "rewrite packages to latest standards",
Long: `Fix finds CUE programs that use old syntax and old APIs and rewrites them to use newer ones.
After you update to a new CUE release, fix helps make the necessary changes
to your program.
Without any packages, fix applies to all files within a module.
`,
RunE: mkRunE(c, runFixAll),
}
cmd.Flags().BoolP(string(flagForce), "f", false,
"rewrite even when there are errors")
return cmd
}
func runFixAll(cmd *Command, args []string) error {
dir, err := os.Getwd()
if err != nil {
return err
}
var opts []fix.Option
if flagSimplify.Bool(cmd) {
opts = append(opts, fix.Simplify())
}
if len(args) == 0 {
args = []string{"./..."}
for {
if fi, err := os.Stat(filepath.Join(dir, "cue.mod")); err == nil {
if fi.IsDir() {
args = appendDirs(args, filepath.Join(dir, "cue.mod", "gen"))
args = appendDirs(args, filepath.Join(dir, "cue.mod", "pkg"))
args = appendDirs(args, filepath.Join(dir, "cue.mod", "usr"))
} else {
args = appendDirs(args, filepath.Join(dir, "pkg"))
}
break
}
dir = filepath.Dir(dir)
if info, _ := os.Stat(dir); !info.IsDir() {
return errors.Newf(token.NoPos, "no module root found")
}
}
}
instances := load.Instances(args, &load.Config{
Tests: true,
Tools: true,
})
errs := fix.Instances(instances, opts...)
if errs != nil && flagForce.Bool(cmd) {
return errs
}
done := map[*ast.File]bool{}
for _, i := range instances {
for _, f := range i.Files {
if done[f] || !strings.HasSuffix(f.Filename, ".cue") {
continue
}
done[f] = true
b, err := format.Node(f)
if err != nil {
errs = errors.Append(errs, errors.Promote(err, "format"))
}
err = os.WriteFile(f.Filename, b, 0644)
if err != nil {
errs = errors.Append(errs, errors.Promote(err, "write"))
}
}
}
return errs
}
func appendDirs(a []string, base string) []string {
_ = filepath.WalkDir(base, func(path string, entry fs.DirEntry, err error) error {
if err == nil && entry.IsDir() && path != base {
short := filepath.ToSlash(path[len(base)+1:])
if strings.ContainsAny(short, "/") {
a = append(a, short)
}
}
return nil
})
return a
}