Skip to content

Commit e977736

Browse files
author
Oluwole Fadeyi
committed
Add support for service selector
Allows the user to select which service specification to output.
1 parent 280a2b1 commit e977736

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

cmd/init.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,36 @@ func specInitCmd(common *commonoptions.Options) *cobra.Command {
6969
options.SourceContent(inputReader),
7070
options.Include(opts.IncludedDirs...))
7171
if err != nil {
72-
logger.Error(err, "parser initialization")
72+
logger.Error(err, "Parser initialization")
7373
return err
7474
}
7575

7676
services, err := parser.Parse(cmd.Context())
7777
if err != nil {
78-
logger.Error(err, "parser parsing error")
78+
logger.Error(err, "Parser parsing error")
7979
return err
8080
}
8181

8282
logger.Info("Source code was parsed")
8383

84+
// check if the user has selected a target service to output
85+
selectedService := services
86+
if opts.Service != "" {
87+
service, ok := services[opts.Service]
88+
if !ok {
89+
err := errors.Errorf("selected service %q was not found in the output", opts.Service)
90+
logger.Error(err, "")
91+
return err
92+
}
93+
selectedService = map[string]any{}
94+
selectedService[opts.Service] = service
95+
}
96+
97+
// Only print to file if the user has selected the to-file option
8498
if opts.ToFile {
8599
logger.Info("Generating specifications files in output directory.", "directory", generate.DefaultServiceDefinitionDir)
86-
if err := generate.WriteSpecifications(nil, []byte(header), services, true, ".", opts.Formats...); err != nil {
87-
logger.Error(err, "writing specification error")
100+
if err := generate.WriteSpecifications(nil, []byte(header), selectedService, true, ".", opts.Formats...); err != nil {
101+
logger.Error(err, "Generating specification file error")
88102
return err
89103
}
90104
return nil
@@ -93,8 +107,8 @@ func specInitCmd(common *commonoptions.Options) *cobra.Command {
93107
logger.Info("Printing result specification to stdout.")
94108
writer := io.WriteCloser(os.Stdout)
95109
defer writer.Close()
96-
if err := generate.WriteSpecifications(writer, []byte(header), services, false, "", opts.Formats...); err != nil {
97-
logger.Error(err, "writing specification error")
110+
if err := generate.WriteSpecifications(writer, []byte(header), selectedService, false, "", opts.Formats...); err != nil {
111+
logger.Error(err, "Writing specification to stdout error")
98112
return err
99113
}
100114
return nil

cmd/options/init/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Package spec contains the different options present under the spec generation co
1616
- [func (o *Options) Prepare(cmd *cobra.Command) *Options](<#func-options-prepare>)
1717

1818

19-
## type [Options](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/cmd/options/init/options.go#L19-L26>)
19+
## type [Options](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/cmd/options/init/options.go#L19-L28>)
2020

2121
Options is the list of options/flag available to the application, plus the clients needed by the application to function.
2222

@@ -27,27 +27,29 @@ type Options struct {
2727
Source string
2828
SrcLanguage lang.Target
2929
Specification string
30+
ToFile bool
31+
Service string
3032
*common.Options
3133
}
3234
```
3335

34-
### func [New](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/cmd/options/init/options.go#L30>)
36+
### func [New](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/cmd/options/init/options.go#L32>)
3537

3638
```go
3739
func New(c *common.Options) *Options
3840
```
3941

4042
New creates a new instance of the application's options
4143

42-
### func \(\*Options\) [Complete](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/cmd/options/init/options.go#L43>)
44+
### func \(\*Options\) [Complete](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/cmd/options/init/options.go#L45>)
4345

4446
```go
4547
func (o *Options) Complete() error
4648
```
4749

4850
Complete initialises the components needed for the application to function given the options
4951

50-
### func \(\*Options\) [Prepare](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/cmd/options/init/options.go#L37>)
52+
### func \(\*Options\) [Prepare](<https://github.com/tfadeyi/sloth-simple-comments/blob/main/cmd/options/init/options.go#L39>)
5153

5254
```go
5355
func (o *Options) Prepare(cmd *cobra.Command) *Options

cmd/options/init/options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type (
2323
SrcLanguage lang.Target
2424
Specification string
2525
ToFile bool
26+
Service string
2627
*common.Options
2728
}
2829
)
@@ -109,4 +110,10 @@ func (o *Options) addAppFlags(fs *pflag.FlagSet) {
109110
false,
110111
"Print the generated specifications to file, under ./slo_definitions.",
111112
)
113+
fs.StringVar(
114+
&o.Service,
115+
"service",
116+
"",
117+
"Outputs only the selected service specification from the resulting parsed service specification.",
118+
)
112119
}

internal/generate/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ Package generate contains utilities to generate data from a given specification
1010

1111
## Index
1212

13+
- [Constants](<#constants>)
1314
- [Variables](<#variables>)
1415
- [func IsValidOutputFormat(format string) bool](<#func-isvalidoutputformat>)
1516
- [func WriteSpecifications(writer io.Writer, header []byte, specs map[string]any, toFile bool, outputDirectory string, formats ...string) error](<#func-writespecifications>)
1617

1718

19+
## Constants
20+
21+
DefaultServiceDefinitionDir is the default filename for the output file
22+
23+
```go
24+
const DefaultServiceDefinitionDir = "slo_definitions"
25+
```
26+
1827
## Variables
1928

2029
ErrUnsupportedFormat is returned if the output format is unsupported

0 commit comments

Comments
 (0)