Skip to content

Commit

Permalink
chore: add golines formatter and task
Browse files Browse the repository at this point in the history
  • Loading branch information
kai687 committed Jan 31, 2025
1 parent 89e86b4 commit 9687d3c
Show file tree
Hide file tree
Showing 78 changed files with 979 additions and 257 deletions.
5 changes: 5 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ tasks:
desc: Lint code
cmds:
- golangci-lint run
format:
desc: Format code
cmds:
- gofumpt -w pkg cmd test internal api
- golines -w pkg cmd test internal api
ci:
desc: Run everything
aliases:
Expand Down
7 changes: 6 additions & 1 deletion api/insights/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ func NewClientWithConfig(config _insights.Configuration) *Client {
// FetchEvents retrieves events from the Algolia Insights API.
func (c *Client) FetchEvents(startDate, endDate time.Time, limit int) (EventsRes, error) {
var res EventsRes
path := fmt.Sprintf("/1/events?startDate=%s&endDate=%s&limit=%d", startDate.Format("2006-01-02T15:04:05.000Z"), endDate.Format("2006-01-02T15:04:05.000Z"), limit)
path := fmt.Sprintf(
"/1/events?startDate=%s&endDate=%s&limit=%d",
startDate.Format("2006-01-02T15:04:05.000Z"),
endDate.Format("2006-01-02T15:04:05.000Z"),
limit,
)
err := c.transport.Request(&res, http.MethodGet, path, nil, call.Read, nil)
return res, err
}
5 changes: 4 additions & 1 deletion api/insights/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
func defaultHosts(r region.Region) (hosts []*transport.StatefulHost) {
switch r {
case region.DE, region.US:
hosts = append(hosts, transport.NewStatefulHost(fmt.Sprintf("insights.%s.algolia.io", r), call.IsReadWrite))
hosts = append(
hosts,
transport.NewStatefulHost(fmt.Sprintf("insights.%s.algolia.io", r), call.IsReadWrite),
)
default:
hosts = append(hosts, transport.NewStatefulHost("insights.algolia.io", call.IsReadWrite))
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ func main() {

func run(args []string) error {
flags := pflag.NewFlagSet("", pflag.ContinueOnError)
dir := flags.StringP("app_data-path", "", "", "Path directory where you want generate documentation data files")
dir := flags.StringP(
"app_data-path",
"",
"",
"Path directory where you want generate documentation data files",
)
help := flags.BoolP("help", "h", false, "Help about any command")
target := flags.StringP("target", "T", "old", "target old or new documentation website")

Expand Down
3 changes: 2 additions & 1 deletion devbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"go-task@latest",
"go@1.23.4",
"golangci-lint@1.63.4",
"gofumpt@latest"
"gofumpt@latest",
"golines@latest"
]
}
48 changes: 48 additions & 0 deletions devbox.lock
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,54 @@
"store_path": "/nix/store/71a493qsawf2gsh3s31zsbrw5pg8psmc-golangci-lint-1.63.4"
}
}
},
"golines@latest": {
"last_modified": "2025-01-19T08:16:51Z",
"resolved": "github:NixOS/nixpkgs/50165c4f7eb48ce82bd063e1fb8047a0f515f8ce#golines",
"source": "devbox-search",
"version": "0.12.2",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/j7f5yj2q07vw1jzgn4gqlaz74dg8c2rj-golines-0.12.2",
"default": true
}
],
"store_path": "/nix/store/j7f5yj2q07vw1jzgn4gqlaz74dg8c2rj-golines-0.12.2"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/s2r29b8wxa98lv8334jd1q24nq6ipaix-golines-0.12.2",
"default": true
}
],
"store_path": "/nix/store/s2r29b8wxa98lv8334jd1q24nq6ipaix-golines-0.12.2"
},
"x86_64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/33ybn8q0ry3wnrwczd8zwnz37l9p83zn-golines-0.12.2",
"default": true
}
],
"store_path": "/nix/store/33ybn8q0ry3wnrwczd8zwnz37l9p83zn-golines-0.12.2"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/46r6pzh2k9a9fqsmfswgbwwly42h6q5w-golines-0.12.2",
"default": true
}
],
"store_path": "/nix/store/46r6pzh2k9a9fqsmfswgbwwly42h6q5w-golines-0.12.2"
}
}
}
}
}
8 changes: 7 additions & 1 deletion internal/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ type Stats struct {
}

// ComputeStats computes the stats for the given index.
func ComputeStats(i iterator.Iterator, s search.Settings, limit int, only string, counter chan int) (*Stats, error) {
func ComputeStats(
i iterator.Iterator,
s search.Settings,
limit int,
only string,
counter chan int,
) (*Stats, error) {
settingsMap := settingsAsMap(s)
stats := &Stats{
Attributes: make(map[string]*AttributeStats),
Expand Down
5 changes: 4 additions & 1 deletion internal/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ type StateEntry struct {
}

// CheckForUpdate checks whether this software has had a newer release on GitHub
func CheckForUpdate(client *http.Client, stateFilePath, currentVersion string) (*ReleaseInfo, error) {
func CheckForUpdate(
client *http.Client,
stateFilePath, currentVersion string,
) (*ReleaseInfo, error) {
stateEntry, _ := getStateEntry(stateFilePath)
if stateEntry != nil && time.Since(stateEntry.CheckedForUpdateAt).Hours() < 24 {
return nil, nil
Expand Down
45 changes: 39 additions & 6 deletions pkg/ask/ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ func (my *StringSlice) WriteAnswer(name string, value interface{}) error {
return nil
}

func AskCommaSeparatedInputQuestion(message string, storage *[]string, defaultValues []string, opts ...survey.AskOpt) error {
func AskCommaSeparatedInputQuestion(
message string,
storage *[]string,
defaultValues []string,
opts ...survey.AskOpt,
) error {
stringSlice := StringSlice{}
err := survey.AskOne(
&survey.Input{
Expand All @@ -30,7 +35,13 @@ func AskCommaSeparatedInputQuestion(message string, storage *[]string, defaultVa
return err
}

func AskMultiSelectQuestion(message string, defaultValues []string, storage *[]string, options []string, opts ...survey.AskOpt) error {
func AskMultiSelectQuestion(
message string,
defaultValues []string,
storage *[]string,
options []string,
opts ...survey.AskOpt,
) error {
err := survey.AskOne(
&survey.MultiSelect{
Message: message,
Expand All @@ -43,30 +54,52 @@ func AskMultiSelectQuestion(message string, defaultValues []string, storage *[]s
return err
}

func AskSelectQuestion(message string, storage *string, options []string, defaultValue string, opts ...survey.AskOpt) error {
func AskSelectQuestion(
message string,
storage *string,
options []string,
defaultValue string,
opts ...survey.AskOpt,
) error {
return survey.AskOne(&survey.Select{
Message: message,
Options: options,
Default: defaultValue,
}, storage, opts...)
}

func AskInputQuestion(message string, storage *string, defaultValue string, opts ...survey.AskOpt) error {
func AskInputQuestion(
message string,
storage *string,
defaultValue string,
opts ...survey.AskOpt,
) error {
return survey.AskOne(&survey.Input{
Message: message,
Default: defaultValue,
}, storage, opts...)
}

func AskInputQuestionWithSuggestion(message string, storage *string, defaultValue string, suggest func(toComplete string) []string, opts ...survey.AskOpt) error {
func AskInputQuestionWithSuggestion(
message string,
storage *string,
defaultValue string,
suggest func(toComplete string) []string,
opts ...survey.AskOpt,
) error {
return survey.AskOne(&survey.Input{
Message: message,
Default: defaultValue,
Suggest: suggest,
}, storage, opts...)
}

func AskBooleanQuestion(message string, storage *bool, defaultValue bool, opts ...survey.AskOpt) error {
func AskBooleanQuestion(
message string,
storage *bool,
defaultValue bool,
opts ...survey.AskOpt,
) error {
return survey.AskOne(&survey.Confirm{
Message: message,
Default: defaultValue,
Expand Down
4 changes: 3 additions & 1 deletion pkg/auth/auth_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func errMissingACLs(missing []string) error {
}

// errAdminAPIKeyRequired is returned when the command requires an admin API Key
var errAdminAPIKeyRequired = errors.New("This command requires an admin API Key. Please use the `--api-key` flag to provide a valid admin API Key.\n")
var errAdminAPIKeyRequired = errors.New(
"This command requires an admin API Key. Please use the `--api-key` flag to provide a valid admin API Key.\n",
)

func DisableAuthCheck(cmd *cobra.Command) {
if cmd.Annotations == nil {
Expand Down
36 changes: 21 additions & 15 deletions pkg/cmd/apikeys/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,27 @@ func NewCreateCmd(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
Used for informative purposes only. It has no impact on the functionality of the API key.`,
))

_ = cmd.RegisterFlagCompletionFunc("indices", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
client, err := f.SearchClient()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
indicesRes, err := client.ListIndices()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
allowedIndices := make([]string, 0, len(indicesRes.Items))
for _, index := range indicesRes.Items {
allowedIndices = append(allowedIndices, fmt.Sprintf("%s\t%s records", index.Name, humanize.Comma(index.Entries)))
}
return allowedIndices, cobra.ShellCompDirectiveNoFileComp
})
_ = cmd.RegisterFlagCompletionFunc(
"indices",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
client, err := f.SearchClient()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
indicesRes, err := client.ListIndices()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
allowedIndices := make([]string, 0, len(indicesRes.Items))
for _, index := range indicesRes.Items {
allowedIndices = append(
allowedIndices,
fmt.Sprintf("%s\t%s records", index.Name, humanize.Comma(index.Entries)),
)
}
return allowedIndices, cobra.ShellCompDirectiveNoFileComp
},
)

_ = cmd.RegisterFlagCompletionFunc("acl",
cmdutil.StringSliceCompletionFunc(map[string]string{
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/apikeys/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ func Test_runCreateCmd(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := httpmock.Registry{}
r.Register(httpmock.REST("POST", "1/keys"), httpmock.JSONResponse(search.CreateKeyRes{Key: "foo"}))
r.Register(
httpmock.REST("POST", "1/keys"),
httpmock.JSONResponse(search.CreateKeyRes{Key: "foo"}),
)

f, out := test.NewFactory(tt.isTTY, &r, nil, "")
cmd := NewCreateCmd(f, nil)
Expand Down
16 changes: 13 additions & 3 deletions pkg/cmd/apikeys/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ func NewDeleteCmd(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
opts.APIKey = args[0]
if !confirm {
if !opts.IO.CanPrompt() {
return cmdutil.FlagErrorf("--confirm required when non-interactive shell is detected")
return cmdutil.FlagErrorf(
"--confirm required when non-interactive shell is detected",
)
}
opts.DoConfirm = true
}
Expand Down Expand Up @@ -77,7 +79,10 @@ func runDeleteCmd(opts *DeleteOptions) error {

if opts.DoConfirm {
var confirmed bool
err = prompt.Confirm(fmt.Sprintf("Delete the following API key: %s?", opts.APIKey), &confirmed)
err = prompt.Confirm(
fmt.Sprintf("Delete the following API key: %s?", opts.APIKey),
&confirmed,
)
if err != nil {
return fmt.Errorf("failed to prompt: %w", err)
}
Expand All @@ -93,7 +98,12 @@ func runDeleteCmd(opts *DeleteOptions) error {

cs := opts.IO.ColorScheme()
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out, "%s API key successfully deleted: %s\n", cs.SuccessIcon(), opts.APIKey)
fmt.Fprintf(
opts.IO.Out,
"%s API key successfully deleted: %s\n",
cs.SuccessIcon(),
opts.APIKey,
)
}
return nil
}
10 changes: 8 additions & 2 deletions pkg/cmd/apikeys/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,14 @@ func Test_runDeleteCmd(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := httpmock.Registry{}
r.Register(httpmock.REST("GET", fmt.Sprintf("1/keys/%s", tt.key)), httpmock.JSONResponse(search.Key{Value: "foo"}))
r.Register(httpmock.REST("DELETE", fmt.Sprintf("1/keys/%s", tt.key)), httpmock.JSONResponse(search.DeleteKeyRes{}))
r.Register(
httpmock.REST("GET", fmt.Sprintf("1/keys/%s", tt.key)),
httpmock.JSONResponse(search.Key{Value: "foo"}),
)
r.Register(
httpmock.REST("DELETE", fmt.Sprintf("1/keys/%s", tt.key)),
httpmock.JSONResponse(search.DeleteKeyRes{}),
)

f, out := test.NewFactory(tt.isTTY, &r, nil, "")
cmd := NewDeleteCmd(f, nil)
Expand Down
16 changes: 14 additions & 2 deletions pkg/cmd/crawler/crawl/crawl.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,27 @@ func runCrawlCmd(opts *CrawlOptions) error {
}
cs := opts.IO.ColorScheme()

opts.IO.StartProgressIndicatorWithLabel(fmt.Sprintf("Requesting crawl for %s on crawler %s", utils.Pluralize(len(opts.URLs), "URL"), opts.ID))
opts.IO.StartProgressIndicatorWithLabel(
fmt.Sprintf(
"Requesting crawl for %s on crawler %s",
utils.Pluralize(len(opts.URLs), "URL"),
opts.ID,
),
)
_, err = client.CrawlURLs(opts.ID, opts.URLs, opts.Save, opts.SaveSpecified)
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("%s Crawler API error: %w", cs.FailureIcon(), err)
}

if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out, "%s Successfully requested crawl for %s on crawler %s\n", cs.SuccessIconWithColor(cs.Green), utils.Pluralize(len(opts.URLs), "URL"), opts.ID)
fmt.Fprintf(
opts.IO.Out,
"%s Successfully requested crawl for %s on crawler %s\n",
cs.SuccessIconWithColor(cs.Green),
utils.Pluralize(len(opts.URLs), "URL"),
opts.ID,
)
}

return nil
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/crawler/crawl/crawl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ func Test_runCrawlCmd(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
r := httpmock.Registry{}
if tt.wantErr == "" {
r.Register(httpmock.REST("POST", "api/1/crawlers/"+tt.id+"/urls/crawl"), httpmock.JSONResponse(crawler.TaskIDResponse{TaskID: "taskID"}))
r.Register(
httpmock.REST("POST", "api/1/crawlers/"+tt.id+"/urls/crawl"),
httpmock.JSONResponse(crawler.TaskIDResponse{TaskID: "taskID"}),
)
} else {
r.Register(httpmock.REST("POST", "api/1/crawlers/"+tt.id+"/urls/crawl"), httpmock.ErrorResponseWithBody(crawler.ErrResponse{Err: crawler.Err{Code: "not-found", Message: "Crawler not-found not found"}}))
}
Expand Down
Loading

0 comments on commit 9687d3c

Please sign in to comment.