-
Notifications
You must be signed in to change notification settings - Fork 9
/
bufgenerate.go
70 lines (60 loc) · 1.94 KB
/
bufgenerate.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
// 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 source
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"namespacelabs.dev/foundation/internal/cli/fncobra"
source "namespacelabs.dev/foundation/internal/codegen"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
)
func newBufGenerateCmd() *cobra.Command {
var (
lang string
env cfg.Context
locs fncobra.Locations
)
return fncobra.
Cmd(&cobra.Command{
Use: "proto-generate [--lang go|typescript] <path>...",
Short: "Run buf.build generate on your codebase.",
Aliases: []string{"proto-gen", "protogen"},
Args: cobra.MinimumNArgs(1),
}).
WithFlags(func(flags *pflag.FlagSet) {
flags.StringVar(&lang, "lang", "go", "Language for proto generation. Supported values: go, typescript.")
}).
With(
fncobra.HardcodeEnv(&env, "dev"),
fncobra.ParseLocations(&locs, &env)).
Do(func(ctx context.Context) error {
var paths []string
for _, loc := range locs.Locations {
if loc.ModuleName != env.Workspace().ModuleName() {
return fnerrors.InternalError("%s: can't run codegen on files outside of the current workspace", loc.ModuleName)
}
paths = append(paths, loc.RelPath)
}
loc, err := parsing.NewPackageLoader(env).Resolve(ctx, schema.PackageName(env.Workspace().ModuleName()))
if err != nil {
return err
}
var fmwk schema.Framework
switch lang {
case "go":
fmwk = schema.Framework_GO
default:
return fmt.Errorf("unsupported language: %s", lang)
}
if err := source.GenProtosAtPaths(ctx, env, fmwk, loc.Module.ReadOnlyFS(), paths, loc.Module.ReadWriteFS()); err != nil {
return err
}
return nil
})
}