-
Notifications
You must be signed in to change notification settings - Fork 787
/
create_client.go
118 lines (92 loc) · 2.99 KB
/
create_client.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
package cmd
import (
"fmt"
"go/build"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/spf13/cobra"
)
const (
optionGroupWithVersion = "group-with-version"
optionInputPackage = "input-package"
optionOutputPackage = "output-package"
optionInputBase = "input-base"
optionOutputBase = "output-base"
optionBoilerplateFile = "boilerplate-file"
optionModuleName = "module-name"
)
// CreateClientOptions the options for the create client command
type CreateClientOptions struct {
CreateOptions
OutputBase string
BoilerplateFile string
GroupsWithVersions []string
InputPackage string
GoPathInputPackage string
GoPathOutputPackage string
GoPathOutputBase string
OutputPackage string
ClientGenVersion string
InputBase string
}
var (
createClientLong = templates.LongDesc(`Generates clients, OpenAPI spec and API docs for custom resources.
Custom resources are defined using Go structs.
Available generators include:
* jx create client openapi # Generates OpenAPI specs, required to generate API docs and clients other than Go
* jx create client docs # Generates API docs from the OpenAPI specs
* jx create client go # Generates a Go client directly from custom resources
`)
createClientExample = templates.Examples(`
`)
)
// NewCmdCreateClient creates clients for CRDs
func NewCmdCreateClient(commonOpts *CommonOptions) *cobra.Command {
o := &CreateClientOptions{
CreateOptions: CreateOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "client",
Short: "Creates clients for Custom Resources",
Long: createClientLong,
Example: createClientExample,
Run: func(cmd *cobra.Command, args []string) {
o.Cmd = cmd
o.Args = args
err := o.Run()
CheckErr(err)
},
}
cmd.AddCommand(NewCmdCreateClientDocs(commonOpts))
cmd.AddCommand(NewCmdCreateClientOpenAPI(commonOpts))
cmd.AddCommand(NewCmdCreateClientGo(commonOpts))
return cmd
}
// Run implements this command
func (o *CreateClientOptions) Run() error {
return o.Cmd.Help()
}
func (o *CreateClientOptions) configureGoPath() error {
if build.Default.GOPATH == "" {
return errors.New("GOPATH must be set")
}
inputPath := filepath.Join(o.InputBase, o.InputPackage)
outputPath := filepath.Join(o.OutputBase, o.OutputPackage)
if !strings.HasPrefix(inputPath, build.Default.GOPATH) {
return errors.Errorf("input %s is not in GOPATH (%s)", inputPath, build.Default.GOPATH)
}
if !strings.HasPrefix(outputPath, build.Default.GOPATH) {
return errors.Errorf("output %s is not in GOPATH (%s)", outputPath, build.Default.GOPATH)
}
// Work out the InputPackage relative to GOROOT
o.GoPathInputPackage = strings.TrimPrefix(inputPath,
fmt.Sprintf("%s/", filepath.Join(build.Default.GOPATH, "src")))
// Work out the OutputPackage relative to GOROOT
o.GoPathOutputPackage = strings.TrimPrefix(outputPath,
fmt.Sprintf("%s/", filepath.Join(build.Default.GOPATH, "src")))
return nil
}