/
generate.go
31 lines (28 loc) · 969 Bytes
/
generate.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
package aliaser
import (
"fmt"
"io"
)
// Generate loads the package defined by the given pattern, generates the
// aliases for the target package name, and writes the result to the given
// writer.
//
// Under the hood, Generate creates a new [Aliaser] with the given parameters
// and calls [Aliaser.Generate] with the writer.
func Generate(target, pattern string, wr io.Writer, opts ...Option) error {
a, err := New(&Config{TargetPackage: target, Pattern: pattern}, opts...)
if err != nil {
return fmt.Errorf("aliaser: %w", err)
}
return a.Generate(wr)
}
// GenerateFile behaves like [Generate], but it writes the aliases to the file
// with the given name. It creates the necessary directories if they don't
// exist.
func GenerateFile(target, pattern, name string, opts ...Option) error {
a, err := New(&Config{TargetPackage: target, Pattern: pattern}, opts...)
if err != nil {
return fmt.Errorf("aliaser: %w", err)
}
return a.GenerateFile(name)
}