-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.go
129 lines (110 loc) · 3.81 KB
/
server.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
// 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 create
import (
"context"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/codegen/genpackage"
"namespacelabs.dev/foundation/internal/console/tui"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/frontend/scaffold"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
)
const serverSuffix = "server"
func newServerCmd(runCommand func(ctx context.Context, args []string) error) *cobra.Command {
var (
targetPkg targetPkg
env cfg.Context
fmwkFlag string
name string
)
grpcServices := []string{}
httpServices := []string{}
return fncobra.
Cmd(&cobra.Command{
Use: "server [path/to/package]",
Short: "Creates a server.",
Args: cobra.RangeArgs(0, 1),
}).
WithFlags(func(flags *pflag.FlagSet) {
flags.StringVar(&name, "name", "", "Server name.")
flags.StringArrayVar(&grpcServices, "with_service", nil, "A service to wire to the server.")
flags.StringArrayVar(&httpServices, "with_http_service", nil, "An HTTP service to wire to the server. Format: 'path:package'.")
}).
With(parseTargetPkgWithDeps(&targetPkg, "service")...).
With(
fncobra.HardcodeEnv(&env, "dev"),
withFramework(&fmwkFlag)).
Do(func(ctx context.Context) error {
parsedHttpServices := []scaffold.HttpService{}
for _, httpService := range httpServices {
parts := strings.Split(httpService, ":")
if len(parts) != 2 {
return fnerrors.New("invalid http_services format: %s", httpService)
}
parsedHttpServices = append(parsedHttpServices, scaffold.HttpService{
Path: parts[0],
Pkg: parts[1],
})
}
fmwk, err := selectFramework(ctx, "Which framework are your services in?", fmwkFlag)
if err != nil {
return err
}
if fmwk == nil {
return context.Canceled
}
var dependencies []string
if *fmwk == schema.Framework_GO {
if err := runGoInitCmdIfNeeded(ctx, targetPkg.Root, runCommand); err != nil {
return err
}
dependencies = append(dependencies,
"namespacelabs.dev/foundation/std/grpc/logging",
"namespacelabs.dev/foundation/std/monitoring/prometheus",
"namespacelabs.dev/foundation/std/monitoring/tracing/jaeger")
}
if name == "" {
name, err = tui.Ask(ctx, "How would you like to name your server?",
"A server's name is used to generate various production resource names and thus should not contain private information.",
serverName(targetPkg.Location))
if err != nil {
return err
}
}
if name == "" {
return context.Canceled
}
opts := scaffold.GenServerOpts{Name: name, Framework: *fmwk, GrpcServices: grpcServices, Dependencies: dependencies, HttpServices: parsedHttpServices}
if err := scaffold.CreateServerScaffold(ctx, targetPkg.Root.ReadWriteFS(), targetPkg.Location, opts); err != nil {
return err
}
// Aggregates and prints all accumulated codegen errors on return.
var errorCollector fnerrors.ErrorCollector
if err := genpackage.ForLocationsGenCode(ctx, targetPkg.Root, env, []fnfs.Location{targetPkg.Location}, errorCollector.Append); err != nil {
return err
}
return errorCollector.Error()
})
}
func serverName(loc fnfs.Location) string {
var name string
base := filepath.Base(loc.RelPath)
dir := filepath.Dir(loc.RelPath)
if base != serverSuffix {
name = base
} else if dir != serverSuffix {
name = dir
}
if name != "" && !strings.HasSuffix(name, serverSuffix) {
return name + serverSuffix
}
return name
}