Skip to content

Commit c85ccba

Browse files
committed
fix: cdn default file
This single commit includes many unrelated changes which I can't be bothered to separate now. AI suggested a bunch of improvements, such as more consistent error handling, new flags (--dry-run, --verbose, --quiet) and automatically refactored code when it encountered lint warnings.
1 parent 706bba9 commit c85ccba

File tree

12 files changed

+1337
-385
lines changed

12 files changed

+1337
-385
lines changed

pkg/cmd/generate/cdn/cdn.go

Lines changed: 397 additions & 122 deletions
Large diffs are not rendered by default.

pkg/cmd/generate/cdn/cdn_test.go

Lines changed: 230 additions & 128 deletions
Large diffs are not rendered by default.

pkg/cmd/generate/clients/clients.go

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package clients
33
import (
44
_ "embed"
55
"fmt"
6-
"log"
6+
"io"
77
"os"
88
"path/filepath"
99
"strings"
@@ -12,6 +12,8 @@ import (
1212
"github.com/MakeNowJust/heredoc"
1313
"github.com/algolia/docli/pkg/cmd/generate/utils"
1414
"github.com/algolia/docli/pkg/dictionary"
15+
"github.com/algolia/docli/pkg/output"
16+
"github.com/algolia/docli/pkg/validate"
1517
"github.com/pb33f/libopenapi"
1618
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
1719
"github.com/spf13/cobra"
@@ -89,10 +91,16 @@ func NewClientsCommand() *cobra.Command {
8991
docli gen clients specs/search.yml -o doc/libraries/sdk/methods
9092
`),
9193
Args: cobra.ExactArgs(1),
92-
Run: func(cmd *cobra.Command, args []string) {
94+
RunE: func(cmd *cobra.Command, args []string) error {
9395
opts.InputFilename = args[0]
9496
opts.APIName = utils.GetAPIName(opts.InputFilename)
95-
runCommand(opts)
97+
98+
printer, err := output.New(cmd)
99+
if err != nil {
100+
return err
101+
}
102+
103+
return runCommand(opts, printer)
96104
},
97105
}
98106

@@ -102,30 +110,44 @@ func NewClientsCommand() *cobra.Command {
102110
return cmd
103111
}
104112

105-
func runCommand(opts *Options) {
113+
func runCommand(opts *Options, printer *output.Printer) error {
114+
if err := validate.ExistingFile(opts.InputFilename, "spec file"); err != nil {
115+
return err
116+
}
117+
118+
if err := validate.OutputDir(opts.OutputDirectory, "output directory"); err != nil {
119+
return err
120+
}
121+
106122
specFile, err := os.ReadFile(opts.InputFilename)
107123
if err != nil {
108-
log.Fatalf("Error: %e", err)
124+
return fmt.Errorf("read spec file %s: %w", opts.InputFilename, err)
109125
}
110126

111-
fmt.Printf("Generating API client references for spec: %s\n", opts.InputFilename)
112-
fmt.Printf("Writing output in: %s\n", opts.OutputDirectory)
127+
printer.Infof("Generating API client references for spec: %s\n", opts.InputFilename)
128+
printer.Infof("Writing output in: %s\n", opts.OutputDirectory)
113129

114130
spec, err := utils.LoadSpec(specFile)
115131
if err != nil {
116-
log.Fatalf("Error: %e", err)
132+
return fmt.Errorf("load spec %s: %w", opts.InputFilename, err)
117133
}
118134

119135
opData, err := getAPIData(spec, opts)
120136
if err != nil {
121-
log.Fatalf("Error: %e", err)
137+
return fmt.Errorf("parse spec %s: %w", opts.InputFilename, err)
122138
}
123139

140+
printer.Verbosef("Spec %s has %d operations.\n", opts.InputFilename, len(opData))
141+
124142
tmpl := template.Must(template.New("method").Funcs(template.FuncMap{
125143
"trim": strings.TrimSpace,
126144
}).Parse(methodTemplate))
127145

128-
writeAPIData(opData, tmpl)
146+
if err := writeAPIData(opData, tmpl, printer); err != nil {
147+
return fmt.Errorf("write output: %w", err)
148+
}
149+
150+
return nil
129151
}
130152

131153
// getAPIData reads the OpenAPI spec and parses the operation data.
@@ -155,7 +177,7 @@ func getAPIData(
155177

156178
acl, err := utils.GetACL(op)
157179
if err != nil {
158-
return nil, err
180+
return nil, fmt.Errorf("get ACL for %s %s: %w", opPairs.Key(), pathName, err)
159181
}
160182

161183
short, long := utils.SplitDescription(op.Description)
@@ -195,27 +217,27 @@ func getAPIData(
195217
}
196218
}
197219

198-
fmt.Printf("Spec %s has %d operations.\n", opts.InputFilename, count)
199-
200220
return result, nil
201221
}
202222

203223
// writeAPIData writes the OpenAPI data to MDX files.
204-
func writeAPIData(data []OperationData, template *template.Template) error {
224+
func writeAPIData(
225+
data []OperationData,
226+
template *template.Template,
227+
printer *output.Printer,
228+
) error {
205229
for _, item := range data {
206-
if err := os.MkdirAll(item.OutputPath, 0o700); err != nil {
207-
return err
230+
if !printer.IsDryRun() {
231+
if err := os.MkdirAll(item.OutputPath, 0o700); err != nil {
232+
return err
233+
}
208234
}
209235

210236
fullPath := filepath.Join(item.OutputPath, item.OutputFilename)
211237

212-
out, err := os.Create(fullPath)
213-
if err != nil {
214-
return err
215-
}
216-
217-
err = template.Execute(out, item)
218-
if err != nil {
238+
if err := printer.WriteFile(fullPath, func(w io.Writer) error {
239+
return template.Execute(w, item)
240+
}); err != nil {
219241
return err
220242
}
221243
}

pkg/cmd/generate/guides/guides.go

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package guides
33
import (
44
"encoding/json"
55
"fmt"
6-
"log"
6+
"io"
77
"os"
88
"path/filepath"
99
"sort"
@@ -12,6 +12,8 @@ import (
1212
"github.com/MakeNowJust/heredoc"
1313
"github.com/algolia/docli/pkg/cmd/generate/utils"
1414
"github.com/algolia/docli/pkg/dictionary"
15+
"github.com/algolia/docli/pkg/output"
16+
"github.com/algolia/docli/pkg/validate"
1517
"github.com/spf13/cobra"
1618
)
1719

@@ -38,9 +40,15 @@ func NewGuidesCommand() *cobra.Command {
3840
docli gen guides guides.json -o openapi-snippets/guides
3941
`),
4042
Args: cobra.ExactArgs(1),
41-
Run: func(cmd *cobra.Command, args []string) {
43+
RunE: func(cmd *cobra.Command, args []string) error {
4244
opts.GuidesFile = args[0]
43-
runCommand(opts)
45+
46+
printer, err := output.New(cmd)
47+
if err != nil {
48+
return err
49+
}
50+
51+
return runCommand(opts, printer)
4452
},
4553
}
4654

@@ -50,19 +58,27 @@ func NewGuidesCommand() *cobra.Command {
5058
return cmd
5159
}
5260

53-
func runCommand(opts *Options) {
61+
func runCommand(opts *Options, printer *output.Printer) error {
62+
if err := validate.ExistingFile(opts.GuidesFile, "guides file"); err != nil {
63+
return err
64+
}
65+
66+
if err := validate.OutputDir(opts.OutputDirectory, "output directory"); err != nil {
67+
return err
68+
}
69+
5470
bytes, err := os.ReadFile(opts.GuidesFile)
5571
if err != nil {
56-
log.Fatalf("Error: %e", err)
72+
return fmt.Errorf("read guides file %s: %w", opts.GuidesFile, err)
5773
}
5874

5975
var data GuidesMap
6076
if err := json.Unmarshal(bytes, &data); err != nil {
61-
log.Fatalf("Error: %e", err)
77+
return fmt.Errorf("parse guides file %s: %w", opts.GuidesFile, err)
6278
}
6379

64-
fmt.Printf("Generating guide snippet files for: %s\n", opts.GuidesFile)
65-
fmt.Printf("Writing output in: %s\n", opts.OutputDirectory)
80+
printer.Infof("Generating guide snippet files for: %s\n", opts.GuidesFile)
81+
printer.Infof("Writing output in: %s\n", opts.OutputDirectory)
6682

6783
guideNames := make([]string, 0, len(data))
6884
for guide := range data {
@@ -76,11 +92,14 @@ func runCommand(opts *Options) {
7692
opts.OutputDirectory,
7793
fmt.Sprintf("%s.mdx", utils.ToKebabCase(guide)),
7894
generateMarkdownSnippet(data[guide]),
95+
printer,
7996
)
8097
if err != nil {
81-
log.Fatalf("Error: %e", err)
98+
return fmt.Errorf("write guide %s to %s: %w", guide, opts.OutputDirectory, err)
8299
}
83100
}
101+
102+
return nil
84103
}
85104

86105
// generateMarkdownSnippet generates a CodeGroup block.
@@ -123,23 +142,18 @@ func sortLanguages(snippet map[string]string) []string {
123142
}
124143

125144
// writeGuide writes the guide snippets into MDX files.
126-
func writeGuide(path string, filename string, snippet string) error {
127-
err := os.MkdirAll(path, 0o700)
128-
if err != nil {
129-
return err
145+
func writeGuide(path string, filename string, snippet string, printer *output.Printer) error {
146+
if !printer.IsDryRun() {
147+
if err := os.MkdirAll(path, 0o700); err != nil {
148+
return err
149+
}
130150
}
131151

132152
fullPath := filepath.Join(path, filename)
133153

134-
out, err := os.Create(fullPath)
135-
if err != nil {
136-
return err
137-
}
138-
defer out.Close()
154+
return printer.WriteFile(fullPath, func(w io.Writer) error {
155+
_, err := io.WriteString(w, snippet)
139156

140-
if _, err := out.WriteString(snippet); err != nil {
141157
return err
142-
}
143-
144-
return nil
158+
})
145159
}

0 commit comments

Comments
 (0)