-
Notifications
You must be signed in to change notification settings - Fork 9
/
fmt.go
118 lines (100 loc) · 2.92 KB
/
fmt.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cmd
import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"namespacelabs.dev/foundation/framework/rpcerrors/multierr"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/frontend/fncue"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/module"
)
func NewFmtCmd() *cobra.Command {
var (
env cfg.Context
)
all := false
check := false
return fncobra.Cmd(&cobra.Command{
Use: "fmt",
Short: "Format foundation configurations of all packages in the workspace.",
Args: cobra.NoArgs,
}).
With(fncobra.HardcodeEnv(&env, "dev")).
WithFlags(func(flags *pflag.FlagSet) {
flags.BoolVar(&all, "all", all, "If set to true, walks through all directories to look for .cue files to format, instead of all packages.")
flags.BoolVar(&check, "check", check, "If set to true, fails if a file would have to be updated.")
}).
Do(func(ctx context.Context) error {
root, err := module.FindRoot(ctx, ".")
if err != nil {
return err
}
opts := fnfs.WriteFileExtendedOpts{
AnnounceWrite: console.Stdout(ctx),
FailOverwrite: check,
}
var errs []error
if !all {
if err := walkSchemas(ctx, env, root, func(loc fnfs.Location, name string) {
if err := fncue.Format(ctx, root.ReadWriteFS(), loc, name, opts); err != nil {
errs = append(errs, err)
}
}); err != nil {
return err
}
} else {
if err := filepath.WalkDir(root.Abs(), func(path string, de fs.DirEntry, err error) error {
if err != nil {
return err
}
switch {
case de.IsDir():
return nil
case filepath.Ext(path) == ".cue":
rel, err := filepath.Rel(root.Abs(), filepath.Dir(path))
if err != nil {
return err
}
if err := fncue.Format(ctx, root.ReadWriteFS(), root.RelPackage(rel), filepath.Base(path), opts); err != nil {
errs = append(errs, err)
}
}
return nil
}); err != nil {
return err
}
}
return multierr.New(errs...)
})
}
func walkSchemas(ctx context.Context, env cfg.Context, root *parsing.Root, f func(fnfs.Location, string)) error {
list, err := parsing.ListSchemas(ctx, env, root)
if err != nil {
return err
}
for _, e := range list.Locations {
ents, err := os.ReadDir(filepath.Join(root.Abs(), e.RelPath))
if err != nil {
fmt.Fprintln(console.Stderr(ctx), "failed to readdir", err)
continue
}
for _, ent := range ents {
if ent.IsDir() || filepath.Ext(ent.Name()) != ".cue" {
continue
}
f(e, ent.Name())
}
}
return nil
}