Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: created schedule cmd #121

Merged
merged 7 commits into from
Nov 26, 2024
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
2 changes: 2 additions & 0 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/goharbor/harbor-cli/cmd/harbor/root/project"
"github.com/goharbor/harbor-cli/cmd/harbor/root/registry"
repositry "github.com/goharbor/harbor-cli/cmd/harbor/root/repository"
"github.com/goharbor/harbor-cli/cmd/harbor/root/schedule"
"github.com/goharbor/harbor-cli/cmd/harbor/root/user"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -110,6 +111,7 @@ harbor help
user.User(),
artifact.Artifact(),
HealthCommand(),
schedule.Schedule(),
)

return root
Expand Down
17 changes: 17 additions & 0 deletions cmd/harbor/root/schedule/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package schedule

import (
"github.com/spf13/cobra"
)

func Schedule() *cobra.Command {
cmd := &cobra.Command{
Use: "schedule",
Short: "Schedule jobs in Harbor",
}
cmd.AddCommand(
ListScheduleCommand(),
)

return cmd
}
38 changes: 38 additions & 0 deletions cmd/harbor/root/schedule/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package schedule

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/schedule/list"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func ListScheduleCommand() *cobra.Command {
var opts api.ListFlags

cmd := &cobra.Command{
Use: "list",
Short: "show all schedule jobs in Harbor",
Run: func(cmd *cobra.Command, args []string) {
schedule, err := api.ListSchedule(opts)
if err != nil {
log.Fatalf("failed to get schedule list: %v", err)
}
FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
utils.PrintPayloadInJSONFormat(schedule)
return
}

list.ListSchedule(schedule.Payload)
},
}

flags := cmd.Flags()
flags.Int64VarP(&opts.Page, "page", "", 1, "Page number")
flags.Int64VarP(&opts.PageSize, "page-size", "", 10, "Size of per page")

return cmd
}
30 changes: 30 additions & 0 deletions pkg/api/schedule_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package api

import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/schedule"
"github.com/goharbor/harbor-cli/pkg/utils"
)

func ListSchedule(opts ...ListFlags) (schedule.ListSchedulesOK, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return schedule.ListSchedulesOK{}, err
}

var listFlags ListFlags
if len(opts) > 0 {
listFlags = opts[0]
}

response, err := client.Schedule.ListSchedules(ctx, &schedule.ListSchedulesParams{
Page: &listFlags.Page,
PageSize: &listFlags.PageSize,
})

if err != nil {
return schedule.ListSchedulesOK{}, err
}

return *response, nil

}
39 changes: 39 additions & 0 deletions pkg/views/schedule/list/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package list

import (
"fmt"
"os"

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/base/tablelist"
)

var columns = []table.Column{
{Title: "ID", Width: 6},
{Title: "Cron", Width: 18},
{Title: "Vendor Type", Width: 28},
{Title: "Update Time", Width: 20},
}

func ListSchedule(schedule []*models.ScheduleTask) {
var rows []table.Row
for _, regis := range schedule {
updatedTime, _ := utils.FormatCreatedTime(regis.UpdateTime.String())
rows = append(rows, table.Row{
fmt.Sprintf("%d", regis.ID),
regis.Cron,
regis.VendorType,
updatedTime,
})
}

m := tablelist.NewModel(columns, rows, len(rows))

if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}