Skip to content

Commit 648c3c8

Browse files
authored
Merge pull request #58 from slosive/cleanup-sourcecode
Small codebase cleanup
2 parents 39ec498 + c03d492 commit 648c3c8

File tree

11 files changed

+71
-97
lines changed

11 files changed

+71
-97
lines changed

README.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
<p align="center">
2121
<strong>[<a href="https://slotalk.fadey.io/docs/intro">Docs</a>]</strong>
22-
<strong>[<a href="#Try-it!">Try it!</a>]</strong>
23-
<strong>[<a href="#Install">Install</a>]</strong>
24-
<strong>[<a href="#Get-Started">Get Started</a>]</strong>
22+
<strong>[<a href="#try-it">Try it!</a>]</strong>
23+
<strong>[<a href="#-install">Install</a>]</strong>
24+
<strong>[<a href="#-get-started">Get Started</a>]</strong>
2525
</p>
2626

2727
> ⚠ The tool is still not ready for real production use yet.
@@ -40,6 +40,18 @@ SLOscribe can be used in tandem with the [Sloth CLI](https://github.com/slok/slo
4040
* [Go](https://go.dev/doc/install)
4141
* [Nix](https://zero-to-nix.com/start/install) (optional)
4242

43+
## 🔽 Install
44+
45+
The SLOscribe CLI can be installed easily via the installation script.
46+
47+
```shell
48+
curl -sfL https://raw.githubusercontent.com/slosive/sloscribe/main/install.sh | sh -
49+
```
50+
51+
This will install the required binary for your machine (Linux/Mac only).
52+
53+
For other ways of installing the tool please check the [installation guide](https://slotalk.fadey.io/docs/category/installation).
54+
4355
## 📚 Basic example - Generate Prometheus alert groups from code annotations
4456

4557
This example shows how sloth's comments can be added next to the prometheus metrics defined in a `metrics.go` file.
@@ -54,7 +66,7 @@ This example shows how sloth's comments can be added next to the prometheus metr
5466
// @sloth.slo description 95% of logins to the chat-gpt app should be successful.
5567
// @sloth.alerting name ChatGPTAvailability
5668
metricTenantLogins = prometheus.NewCounter(
57-
prometheus.GaugeOpts{
69+
prometheus.CounterOpts{
5870
Namespace: "chatgpt",
5971
Subsystem: "auth0",
6072
Name: "tenant_login_operations_total",
@@ -303,18 +315,6 @@ groups:
303315
304316
</details>
305317
306-
## 🔽 Install
307-
308-
The SLOscribe CLI can be installed easily via the installation script.
309-
310-
```shell
311-
curl -sfL https://raw.githubusercontent.com/slosive/sloscribe/main/install.sh | sh -
312-
```
313-
314-
This will install the required binary for your machine (Linux/Mac only).
315-
316-
For other ways of installing the tool please check the [installation guide](https://slotalk.fadey.io/docs/category/installation).
317-
318318
## 🚀 Get Started
319319
320320
1. Add comments to your source code. See [Declarative Comments](https://slotalk.fadey.io/docs/category/sloth-annotations).
@@ -342,14 +342,14 @@ Usage:
342342
sloscribe init [flags]
343343
344344
Flags:
345-
--dirs strings Comma separated list of directories to be parses by the tool (default [...])
346-
-f, --file string Target file to parse. example: ./metrics.go
347-
--format strings Output format (yaml,json). (default [yaml])
348-
-h, --help help for init
349-
--lang string Language of the source files. (go) (default "go")
350-
--services strings Comma separated list of service names. These will select the output service specifications returned by the tool.
351-
--specification string The name of the specification the tool should parse the source file for, example: sloth or sloth-k8s. (default "sloth")
352-
--to-file Print the generated specifications to file, under ./slo_definitions.
345+
--dirs strings Comma separated list of directories to be recursively parsed by the tool (default [/home/jetstack-oluwole/go/src/github.com/slosive/sloscribe])
346+
-f, --file string Source code file to parse for annotations. Example: ./metrics.go
347+
--format strings Format of the output returned by the tool. Available: yaml, json. (default [yaml])
348+
-h, --help help for init
349+
--lang string Target source code language. Available: go. (default "go")
350+
--service-selector strings Comma separated list of service specification names. These will select the output service specifications returned by the tool. Example: --service-selector app1,app3
351+
--specification string The SLO specification the tool should parse the source file for. Available: sloth, sloth-k8s. (default "sloth")
352+
--to-file Tells the tool to save the generated specifications to file, under ./slo_definitions.
353353
354354
Global Flags:
355355
--log-level string Only log messages with the given severity or above. One of: [none, debug, info, warn], errors will always be printed (default "info")

cmd/init.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ func specInitCmd(common *commonoptions.Options) *cobra.Command {
7878
options.SourceContent(inputReader),
7979
options.Include(opts.IncludedDirs...))
8080
if err != nil {
81-
logger.Error(err, "Parser initialization")
81+
logger.Error(err, "Parser initialization error, please try again")
8282
return err
8383
}
8484

8585
services, err := parser.Parse(cmd.Context())
8686
if err != nil {
87-
logger.Error(err, "Parser parsing error")
87+
logger.Error(err, "Parsing error, please try again")
8888
return err
8989
}
9090

@@ -100,45 +100,41 @@ func specInitCmd(common *commonoptions.Options) *cobra.Command {
100100

101101
service, ok := services[serviceName]
102102
if !ok {
103-
err := errors.Errorf("selected service %q was not found in the parser output", serviceName)
104-
logger.Warn(err, "")
103+
logger.Warn(errors.Errorf("selected service specification %q was not found in the parser output", serviceName), "")
105104
} else {
106105
selectedServices[serviceName] = service
107106
}
108107
}
109108

110109
// Only print to file if the user has selected the to-file option
111110
if opts.ToFile {
112-
logger.Info("Generating specifications files in output directory ✍🏿", "directory", generate.DefaultServiceDefinitionDir)
111+
logger.Info("Generating service specification(s) files in output directory", "directory", generate.DefaultServiceDefinitionDir)
113112
if outputKubernetes {
114-
if err := generate.WriteK8Specifications(nil, []byte(header), selectedServices, true, ".", opts.Formats...); err != nil {
115-
logger.Error(err, "Generating specification file error")
116-
return err
117-
}
113+
err = generate.WriteK8Specifications(nil, []byte(header), selectedServices, true, ".", opts.Formats...)
118114
} else {
119-
if err := generate.WriteSpecifications(nil, []byte(header), selectedServices, true, ".", opts.Formats...); err != nil {
120-
logger.Error(err, "Generating specification file error")
121-
return err
122-
}
115+
err = generate.WriteSpecifications(nil, []byte(header), selectedServices, true, ".", opts.Formats...)
116+
}
117+
if err != nil {
118+
logger.Error(err, "Error generating specification file for the parsed service, please try again")
119+
return err
123120
}
124121
return nil
125122
}
126123

127-
logger.Info("Printing result specification to stdout ✍🏿")
124+
logger.Info("Printing parsed service specification(s) ✍🏿")
128125
writer := cmd.OutOrStdout()
129126

130127
// Print the specification(s) to stout or file
131128
if outputKubernetes {
132-
if err := generate.WriteK8Specifications(writer, []byte(header), selectedServices, false, "", opts.Formats...); err != nil {
133-
logger.Error(err, "Writing specification to stdout error")
134-
return err
135-
}
129+
err = generate.WriteK8Specifications(writer, []byte(header), selectedServices, false, "", opts.Formats...)
136130
} else {
137-
if err := generate.WriteSpecifications(writer, []byte(header), selectedServices, false, "", opts.Formats...); err != nil {
138-
logger.Error(err, "Writing specification to stdout error")
139-
return err
140-
}
131+
err = generate.WriteSpecifications(writer, []byte(header), selectedServices, false, "", opts.Formats...)
141132
}
133+
if err != nil {
134+
logger.Error(err, "Error printing service specification(s) to standard output")
135+
return err
136+
}
137+
142138
return nil
143139
},
144140
}

cmd/options/common/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Package common contains the different common options across the different comman
1717

1818

1919
<a name="Options"></a>
20-
## type [Options](<https://github.com/slosive/sloscribe/blob/main/cmd/options/common/options.go#L15-L18>)
20+
## type [Options](<https://github.com/slosive/sloscribe/blob/main/cmd/options/common/options.go#L14-L17>)
2121

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

@@ -29,7 +29,7 @@ type Options struct {
2929
```
3030

3131
<a name="New"></a>
32-
### func [New](<https://github.com/slosive/sloscribe/blob/main/cmd/options/common/options.go#L22>)
32+
### func [New](<https://github.com/slosive/sloscribe/blob/main/cmd/options/common/options.go#L21>)
3333

3434
```go
3535
func New() *Options
@@ -38,7 +38,7 @@ func New() *Options
3838
New creates a new instance of the application's options
3939

4040
<a name="Options.Complete"></a>
41-
### func \(\*Options\) [Complete](<https://github.com/slosive/sloscribe/blob/main/cmd/options/common/options.go#L33>)
41+
### func \(\*Options\) [Complete](<https://github.com/slosive/sloscribe/blob/main/cmd/options/common/options.go#L32>)
4242

4343
```go
4444
func (o *Options) Complete() error
@@ -47,7 +47,7 @@ func (o *Options) Complete() error
4747
Complete initialises the components needed for the application to function given the options
4848

4949
<a name="Options.Prepare"></a>
50-
### func \(\*Options\) [Prepare](<https://github.com/slosive/sloscribe/blob/main/cmd/options/common/options.go#L27>)
50+
### func \(\*Options\) [Prepare](<https://github.com/slosive/sloscribe/blob/main/cmd/options/common/options.go#L26>)
5151

5252
```go
5353
func (o *Options) Prepare(cmd *cobra.Command) *Options

cmd/options/common/options.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/slosive/sloscribe/internal/logging"
77
"github.com/spf13/cobra"
88
"github.com/spf13/pflag"
9-
goaloe "github.com/tfadeyi/go-aloe"
109
)
1110

1211
type (
@@ -38,9 +37,7 @@ func (o *Options) Complete() error {
3837
// @aloe summary The log level passed to the --log-level flag is not supported.
3938
// @aloe details The log level passed to the --log-level flag is not currently supported by the tool.
4039
// The following are supported: none, debug, info(default), warn.
41-
err = goaloe.DefaultOrDie().Error(
42-
multierr.Append(err, errors.Errorf("invalid log-level %q was passed to --log-level flag", o.LogLevel)),
43-
"invalid_log_level")
40+
err = multierr.Append(err, errors.Errorf("invalid log-level %q was passed to --log-level flag", o.LogLevel))
4441
}
4542
return err
4643
}

cmd/options/init/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Package spec contains the different options present under the spec generation co
1717

1818

1919
<a name="Options"></a>
20-
## type [Options](<https://github.com/slosive/sloscribe/blob/main/cmd/options/init/options.go#L19-L29>)
20+
## type [Options](<https://github.com/slosive/sloscribe/blob/main/cmd/options/init/options.go#L18-L28>)
2121

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

@@ -36,7 +36,7 @@ type Options struct {
3636
```
3737

3838
<a name="New"></a>
39-
### func [New](<https://github.com/slosive/sloscribe/blob/main/cmd/options/init/options.go#L33>)
39+
### func [New](<https://github.com/slosive/sloscribe/blob/main/cmd/options/init/options.go#L32>)
4040

4141
```go
4242
func New(c *common.Options) *Options
@@ -45,7 +45,7 @@ func New(c *common.Options) *Options
4545
New creates a new instance of the application's options
4646

4747
<a name="Options.Complete"></a>
48-
### func \(\*Options\) [Complete](<https://github.com/slosive/sloscribe/blob/main/cmd/options/init/options.go#L46>)
48+
### func \(\*Options\) [Complete](<https://github.com/slosive/sloscribe/blob/main/cmd/options/init/options.go#L45>)
4949

5050
```go
5151
func (o *Options) Complete() error
@@ -54,7 +54,7 @@ func (o *Options) Complete() error
5454
Complete initialises the components needed for the application to function given the options
5555

5656
<a name="Options.Prepare"></a>
57-
### func \(\*Options\) [Prepare](<https://github.com/slosive/sloscribe/blob/main/cmd/options/init/options.go#L40>)
57+
### func \(\*Options\) [Prepare](<https://github.com/slosive/sloscribe/blob/main/cmd/options/init/options.go#L39>)
5858

5959
```go
6060
func (o *Options) Prepare(cmd *cobra.Command) *Options

cmd/options/init/options.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/slosive/sloscribe/internal/parser/lang"
1111
"github.com/spf13/cobra"
1212
"github.com/spf13/pflag"
13-
goaloe "github.com/tfadeyi/go-aloe"
1413
)
1514

1615
type (
@@ -52,9 +51,7 @@ func (o *Options) Complete() error {
5251
// The following are the supported languages: yaml(default), json.
5352
for _, format := range o.Formats {
5453
if ok := generate.IsValidOutputFormat(format); !ok {
55-
err = goaloe.DefaultOrDie().Error(
56-
multierr.Append(err, errors.Errorf("invalid format %q was passed to --format flag", format)),
57-
"unsupported_output_format")
54+
err = multierr.Append(err, errors.Errorf("invalid format %q was passed to --format flag", format))
5855
}
5956
}
6057

@@ -64,9 +61,7 @@ func (o *Options) Complete() error {
6461
// @aloe details The source language passed to the --lang flag is not currently supported by the tool.
6562
// The following are the supported languages: go, wasm(experimental).
6663
if ok := lang.IsSupportedLanguage(o.SourceLanguage); !ok {
67-
err = goaloe.DefaultOrDie().Error(
68-
multierr.Append(err, errors.Errorf("unsupported language %q was passed to --lang flag", o.SourceLanguage)),
69-
"unsupported_language")
64+
err = multierr.Append(err, errors.Errorf("unsupported language %q was passed to --lang flag", o.SourceLanguage))
7065
}
7166
return err
7267
}
@@ -84,43 +79,43 @@ func (o *Options) addAppFlags(fs *pflag.FlagSet) {
8479
&o.IncludedDirs,
8580
"dirs",
8681
[]string{getWorkingDirOrDie()},
87-
"Comma separated list of directories to be parses by the tool",
82+
"Comma separated list of directories to be recursively parsed by the tool",
8883
)
8984
fs.StringSliceVar(
9085
&o.Formats,
9186
"format",
9287
[]string{"yaml"},
93-
"Output format (yaml,json).",
88+
"Format of the output returned by the tool. Available: yaml, json.",
9489
)
9590
fs.StringVar(
9691
(*string)(&o.SourceLanguage),
9792
"lang",
9893
"go",
99-
"Language of the source files. (go)",
94+
"Target source code language. Available: go.",
10095
)
10196
fs.StringVarP(
10297
&o.Source,
10398
"file",
10499
"f",
105100
"",
106-
"Target file to parse. example: ./metrics.go",
101+
"Source code file to parse for annotations. Example: ./metrics.go",
107102
)
108103
fs.BoolVar(
109104
&o.ToFile,
110105
"to-file",
111106
false,
112-
"Print the generated specifications to file, under ./slo_definitions.",
107+
"Tells the tool to save the generated specifications to file, under ./slo_definitions.",
113108
)
114109
fs.StringSliceVar(
115110
&o.Services,
116-
"services",
111+
"service-selector",
117112
[]string{},
118-
"Comma separated list of service names. These will select the output service specifications returned by the tool.",
113+
"Comma separated list of service specification names. These will select the output service specifications returned by the tool. Example: --service-selector app1,app3 ",
119114
)
120115
fs.StringVar(
121116
&o.Target,
122117
"specification",
123118
"sloth",
124-
"The name of the specification the tool should parse the source file for, example: sloth or sloth-k8s.",
119+
"The SLO specification the tool should parse the source file for. Available: sloth, sloth-k8s.",
125120
)
126121
}

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ require (
1212
github.com/spf13/cobra v1.7.0
1313
github.com/spf13/pflag v1.0.5
1414
github.com/stretchr/testify v1.8.4
15-
github.com/tfadeyi/go-aloe v0.0.2
1615
gopkg.in/yaml.v3 v3.0.1
1716
k8s.io/apimachinery v0.27.3
1817
sigs.k8s.io/yaml v1.3.0
@@ -27,7 +26,6 @@ require (
2726
github.com/json-iterator/go v1.1.12 // indirect
2827
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2928
github.com/modern-go/reflect2 v1.0.2 // indirect
30-
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
3129
github.com/pmezard/go-difflib v1.0.0 // indirect
3230
github.com/rogpeppe/go-internal v1.10.0 // indirect
3331
golang.org/x/net v0.8.0 // indirect

go.sum

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
3838
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
3939
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
4040
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
41-
github.com/pelletier/go-toml/v2 v2.0.7 h1:muncTPStnKRos5dpVKULv2FVd4bMOhNePj9CjgDb8Us=
42-
github.com/pelletier/go-toml/v2 v2.0.7/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
4341
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4442
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4543
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
@@ -52,16 +50,9 @@ github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRM
5250
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
5351
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
5452
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
55-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
56-
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
5753
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
58-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
59-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
60-
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
6154
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
6255
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
63-
github.com/tfadeyi/go-aloe v0.0.2 h1:3zKDn2Bg2pDL2ES8CIHL6TCV8hTUlamjoNY5aWjmQtA=
64-
github.com/tfadeyi/go-aloe v0.0.2/go.mod h1:iowd4nC94VHlcq3vebbcGX/8DMkC5oa7iGPPbBwbAf0=
6556
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
6657
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
6758
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -100,7 +91,6 @@ gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
10091
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
10192
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
10293
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
103-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10494
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10595
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10696
k8s.io/apimachinery v0.27.3 h1:Ubye8oBufD04l9QnNtW05idcOe9Z3GQN8+7PqmuVcUM=

0 commit comments

Comments
 (0)