Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,27 @@ linters:
- goconst
- godot
- importas
- modernize
- nilerr
- promlinter
- revive
- sloglint
- testifylint
- unparam
- usestdlibvars
- wsl_v5
settings:
errcheck:
exclude-functions:
- fmt.Fprint
- fmt.Fprintf
- fmt.Fprintln
- (*os.File).Write
revive:
rules:
- name: exported
wsl_v5:
default: none
enable:
- after-block
- err
exclusions:
presets:
- std-error-handling
formatters:
enable:
- gci
Expand Down
1 change: 1 addition & 0 deletions cmd/qcloud/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func versionString() string {
if versionPrerelease != "" {
return version + "-" + versionPrerelease
}

return version
}

Expand Down
2 changes: 2 additions & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ Documentation: https://github.com/qdrant/qcloud-cli`,
if err := s.Config.Load(configPath); err != nil {
return err
}

if debug {
s.Logger = slog.New(slog.NewTextHandler(cmd.ErrOrStderr(), &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
}

return nil
},
SilenceUsage: true,
Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/account/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,21 @@ qcloud account describe --json`,
fmt.Fprintf(w, "Domain: %s\n", company.GetDomain())
}
}

if privs := acct.GetPrivileges(); len(privs) > 0 {
fmt.Fprintf(w, "Privileges: %s\n", strings.Join(privs, ", "))
}

if acct.GetCreatedAt() != nil {
t := acct.GetCreatedAt().AsTime()
fmt.Fprintf(w, "Created: %s (%s)\n", output.HumanTime(t), output.FullDateTime(t))
}

if acct.GetLastModifiedAt() != nil {
t := acct.GetLastModifiedAt().AsTime()
fmt.Fprintf(w, "Modified: %s (%s)\n", output.HumanTime(t), output.FullDateTime(t))
}

return nil
},
ValidArgsFunction: completion.AccountIDCompletion(s),
Expand All @@ -85,5 +89,6 @@ func resolveAccountID(s *state.State, args []string) (string, error) {
if len(args) > 0 {
return args[0], nil
}

return s.AccountID()
}
1 change: 1 addition & 0 deletions internal/cmd/account/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ qcloud account list --json`,
if v.GetCreatedAt() != nil {
return output.HumanTime(v.GetCreatedAt().AsTime())
}

return ""
})
t.SetItems(resp.GetItems())
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/account/member_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ qcloud account member describe a1b2c3d4-e5f6-7890-abcd-ef1234567890 --json`,
t := user.GetCreatedAt().AsTime()
fmt.Fprintf(w, "Created: %s (%s)\n", output.HumanTime(t), output.FullDateTime(t))
}

return nil
},
ValidArgsFunction: completion.AccountMemberIDCompletion(s),
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/account/member_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ qcloud account member list --json`,
if v.GetAccountMember().GetCreatedAt() != nil {
return output.HumanTime(v.GetAccountMember().GetCreatedAt().AsTime())
}

return ""
})
t.SetItems(resp.GetItems())
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/account/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ qcloud account update --name "New Name" --json`,
if updated.Company == nil {
updated.Company = &accountv1.Company{}
}

if cmd.Flags().Changed("company-name") {
name, _ := cmd.Flags().GetString("company-name")
updated.Company.Name = name
}

if cmd.Flags().Changed("company-domain") {
domain, _ := cmd.Flags().GetString("company-domain")
updated.Company.Domain = &domain
Expand All @@ -103,6 +105,7 @@ qcloud account update --name "New Name" --json`,
if acct == nil {
return
}

fmt.Fprintf(out, "Account %s (%s) updated successfully.\n", acct.GetId(), acct.GetName())
},
}.CobraCommand(s)
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/backup/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func newCreateCommand(s *state.State) *cobra.Command {
if err != nil {
return nil, fmt.Errorf("failed to create backup: %w", err)
}

return resp.GetBackup(), nil
},
PrintResource: func(_ *cobra.Command, out io.Writer, b *backupv1.Backup) {
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/backup/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestBackupCreate_WithRetention(t *testing.T) {
if req.GetBackup().GetRetentionPeriod() != nil {
capturedRetention = int64(req.GetBackup().GetRetentionPeriod().AsDuration().Hours()) / 24
}

assert.Equal(t, int64(7), capturedRetention)
}

Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/backup/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func newDescribeCommand(s *state.State) *cobra.Command {
if err != nil {
return nil, fmt.Errorf("failed to get backup: %w", err)
}

return resp.GetBackup(), nil
},
PrintText: func(_ *cobra.Command, w io.Writer, b *backupv1.Backup) error {
Expand All @@ -51,18 +52,22 @@ func newDescribeCommand(s *state.State) *cobra.Command {
t := b.GetCreatedAt().AsTime()
fmt.Fprintf(w, "Created: %s (%s)\n", output.HumanTime(t), output.FullDateTime(t))
}

if b.GetBackupScheduleId() != "" {
fmt.Fprintf(w, "Schedule: %s\n", b.GetBackupScheduleId())
}

if b.GetRetentionPeriod() != nil {
days := int64(b.GetRetentionPeriod().AsDuration().Hours()) / 24
fmt.Fprintf(w, "Retention: %d days\n", days)
}

if b.GetClusterInfo() != nil {
ci := b.GetClusterInfo()
fmt.Fprintf(w, "Cloud: %s\n", ci.GetCloudProviderId())
fmt.Fprintf(w, "Region: %s\n", ci.GetCloudProviderRegionId())
}

return nil
},
}.CobraCommand(s)
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/backup/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func newListCommand(s *state.State) *cobra.Command {
if err != nil {
return nil, fmt.Errorf("failed to list backups: %w", err)
}

return resp, nil
},
OutputTable: func(_ *cobra.Command, w io.Writer, resp *backupv1.ListBackupsResponse) (output.TableRenderer, error) {
Expand All @@ -60,6 +61,7 @@ func newListCommand(s *state.State) *cobra.Command {
if v.GetCreatedAt() != nil {
return output.HumanTime(v.GetCreatedAt().AsTime())
}

return ""
})
t.SetItems(resp.GetItems())
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/backup/restore_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func newRestoreListCommand(s *state.State) *cobra.Command {
if err != nil {
return nil, fmt.Errorf("failed to list backup restores: %w", err)
}

return resp, nil
},
OutputTable: func(_ *cobra.Command, w io.Writer, resp *backupv1.ListBackupRestoresResponse) (output.TableRenderer, error) {
Expand All @@ -60,6 +61,7 @@ func newRestoreListCommand(s *state.State) *cobra.Command {
if v.GetCreatedAt() != nil {
return output.HumanTime(v.GetCreatedAt().AsTime())
}

return ""
})
t.SetItems(resp.GetItems())
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/backup/schedule_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func scheduleIDCompletion(s *state.State) func(*cobra.Command, []string, string)
for _, sched := range resp.GetItems() {
completions = append(completions, sched.GetId()+"\tcluster:"+sched.GetClusterId()+" | "+sched.GetSchedule())
}

return completions, cobra.ShellCompDirectiveNoFileComp
}
}
1 change: 1 addition & 0 deletions internal/cmd/backup/schedule_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func newScheduleCreateCommand(s *state.State) *cobra.Command {
if err != nil {
return nil, fmt.Errorf("failed to create backup schedule: %w", err)
}

return resp.GetBackupSchedule(), nil
},
PrintResource: func(_ *cobra.Command, out io.Writer, sched *backupv1.BackupSchedule) {
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/backup/schedule_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestScheduleCreate_WithRetention(t *testing.T) {
if req.GetBackupSchedule().GetRetentionPeriod() != nil {
retentionDays = int64(req.GetBackupSchedule().GetRetentionPeriod().AsDuration().Hours()) / 24
}

assert.Equal(t, int64(30), retentionDays)
}

Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/backup/schedule_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func nextScheduleRun(cronExpr string) (time.Time, bool) {
if err != nil {
return time.Time{}, false
}

return s.Next(time.Now().UTC()), true
}

Expand Down Expand Up @@ -56,6 +57,7 @@ The --cluster-id flag is required because the API requires the cluster ID to loo
if err != nil {
return nil, fmt.Errorf("failed to get backup schedule: %w", err)
}

return resp.GetBackupSchedule(), nil
},
PrintText: func(_ *cobra.Command, w io.Writer, sched *backupv1.BackupSchedule) error {
Expand All @@ -65,15 +67,18 @@ The --cluster-id flag is required because the API requires the cluster ID to loo
if next, ok := nextScheduleRun(sched.GetSchedule()); ok {
fmt.Fprintf(w, "Next Run: %s (%s)\n", output.HumanTime(next), output.FullDateTime(next))
}

fmt.Fprintf(w, "Status: %s\n", output.BackupScheduleStatus(sched.GetStatus()))
if sched.GetCreatedAt() != nil {
t := sched.GetCreatedAt().AsTime()
fmt.Fprintf(w, "Created: %s (%s)\n", output.HumanTime(t), output.FullDateTime(t))
}

if sched.GetRetentionPeriod() != nil {
days := int64(sched.GetRetentionPeriod().AsDuration().Hours()) / 24
fmt.Fprintf(w, "Retention: %d days\n", days)
}

return nil
},
}.CobraCommand(s)
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/backup/schedule_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func newScheduleListCommand(s *state.State) *cobra.Command {
if err != nil {
return nil, fmt.Errorf("failed to list backup schedules: %w", err)
}

return resp, nil
},
OutputTable: func(_ *cobra.Command, w io.Writer, resp *backupv1.ListBackupSchedulesResponse) (output.TableRenderer, error) {
Expand All @@ -60,12 +61,14 @@ func newScheduleListCommand(s *state.State) *cobra.Command {
if next, ok := nextScheduleRun(v.GetSchedule()); ok {
return output.HumanTime(next)
}

return ""
})
t.AddField("CREATED", func(v *backupv1.BackupSchedule) string {
if v.GetCreatedAt() != nil {
return output.HumanTime(v.GetCreatedAt().AsTime())
}

return ""
})
t.SetItems(resp.GetItems())
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/backup/schedule_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ The --cluster-id flag is required because the API requires the cluster ID to loo
if err != nil {
return nil, fmt.Errorf("failed to get backup schedule: %w", err)
}

return resp.GetBackupSchedule(), nil
},
Update: func(s *state.State, cmd *cobra.Command, sched *backupv1.BackupSchedule) (*backupv1.BackupSchedule, error) {
Expand All @@ -75,6 +76,7 @@ The --cluster-id flag is required because the API requires the cluster ID to loo
if retentionDays < 1 {
return nil, fmt.Errorf("--retention-days must be at least 1")
}

d := time.Duration(retentionDays) * 24 * time.Hour
sched.RetentionPeriod = durationpb.New(d)
}
Expand All @@ -85,6 +87,7 @@ The --cluster-id flag is required because the API requires the cluster ID to loo
if err != nil {
return nil, fmt.Errorf("failed to update backup schedule: %w", err)
}

return resp.GetBackupSchedule(), nil
},
PrintResource: func(_ *cobra.Command, out io.Writer, sched *backupv1.BackupSchedule) {
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/backup/schedule_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func TestScheduleUpdate_WithRetention(t *testing.T) {
if req.GetBackupSchedule().GetRetentionPeriod() != nil {
retentionDays = int64(req.GetBackupSchedule().GetRetentionPeriod().AsDuration().Hours()) / 24
}

assert.Equal(t, int64(14), retentionDays)
}

Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/base/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ func (gc Cmd) CobraCommand(s *state.State) *cobra.Command {
if gc.Long != "" {
cmd.Long = gc.Long
}

cmd.Example = gc.Example
cmd.RunE = func(cmd *cobra.Command, args []string) error {
return gc.Run(s, cmd, args)
}
if gc.ValidArgsFunction != nil {
cmd.ValidArgsFunction = gc.ValidArgsFunction
}

return cmd
}
5 changes: 5 additions & 0 deletions internal/cmd/base/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,27 @@ func (cc CreateCmd[T]) CobraCommand(s *state.State) *cobra.Command {
if cc.Long != "" {
cmd.Long = cc.Long
}

cmd.Example = cc.Example
cmd.RunE = func(cmd *cobra.Command, args []string) error {
resource, err := cc.Run(s, cmd, args)
if err != nil {
return err
}

if s.Config.JSONOutput() {
return output.PrintJSON(cmd.OutOrStdout(), resource)
}

if cc.PrintResource != nil {
cc.PrintResource(cmd, cmd.OutOrStdout(), resource)
}

return nil
}
if cc.ValidArgsFunction != nil {
cmd.ValidArgsFunction = cc.ValidArgsFunction
}

return cmd
}
3 changes: 3 additions & 0 deletions internal/cmd/base/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ func (dc DescribeCmd[T]) CobraCommand(s *state.State) *cobra.Command {
if err != nil {
return err
}

if s.Config.JSONOutput() {
return output.PrintJSON(cmd.OutOrStdout(), resource)
}

return dc.PrintText(cmd, cmd.OutOrStdout(), resource)
},
}
if dc.ValidArgsFunction != nil {
cmd.ValidArgsFunction = dc.ValidArgsFunction
}

return cmd
}
Loading