diff --git a/internal/cmd/root/root.go b/internal/cmd/root/root.go index 197d660..34d5232 100644 --- a/internal/cmd/root/root.go +++ b/internal/cmd/root/root.go @@ -16,6 +16,7 @@ import ( domainCmd "github.com/zeabur/cli/internal/cmd/domain" profileCmd "github.com/zeabur/cli/internal/cmd/profile" projectCmd "github.com/zeabur/cli/internal/cmd/project" + serverCmd "github.com/zeabur/cli/internal/cmd/server" serviceCmd "github.com/zeabur/cli/internal/cmd/service" templateCmd "github.com/zeabur/cli/internal/cmd/template" uploadCmd "github.com/zeabur/cli/internal/cmd/upload" @@ -120,6 +121,7 @@ func NewCmdRoot(f *cmdutil.Factory, version, commit, date string) (*cobra.Comman cmd.AddCommand(versionCmd.NewCmdVersion(f, version, commit, date)) cmd.AddCommand(authCmd.NewCmdAuth(f)) cmd.AddCommand(projectCmd.NewCmdProject(f)) + cmd.AddCommand(serverCmd.NewCmdServer(f)) cmd.AddCommand(serviceCmd.NewCmdService(f)) cmd.AddCommand(deploymentCmd.NewCmdDeployment(f)) cmd.AddCommand(templateCmd.NewCmdTemplate(f)) diff --git a/internal/cmd/server/list/list.go b/internal/cmd/server/list/list.go new file mode 100644 index 0000000..dff76c3 --- /dev/null +++ b/internal/cmd/server/list/list.go @@ -0,0 +1,35 @@ +package list + +import ( + "context" + + "github.com/spf13/cobra" + + "github.com/zeabur/cli/internal/cmdutil" + "github.com/zeabur/cli/pkg/model" +) + +func NewCmdList(f *cmdutil.Factory) *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List dedicated servers", + Aliases: []string{"ls"}, + RunE: func(cmd *cobra.Command, args []string) error { + return runList(f) + }, + } + + return cmd +} + +func runList(f *cmdutil.Factory) error { + servers, err := f.ApiClient.GetServers(context.Background()) + if err != nil { + return err + } + + s := model.Servers(servers) + f.Printer.Table(s.Header(), s.Rows()) + + return nil +} diff --git a/internal/cmd/server/server.go b/internal/cmd/server/server.go new file mode 100644 index 0000000..bdac8a8 --- /dev/null +++ b/internal/cmd/server/server.go @@ -0,0 +1,22 @@ +// Package server contains the cmd for managing dedicated servers +package server + +import ( + "github.com/spf13/cobra" + + "github.com/zeabur/cli/internal/cmdutil" + + serverListCmd "github.com/zeabur/cli/internal/cmd/server/list" +) + +// NewCmdServer creates the server command +func NewCmdServer(f *cmdutil.Factory) *cobra.Command { + cmd := &cobra.Command{ + Use: "server", + Short: "Manage dedicated servers", + } + + cmd.AddCommand(serverListCmd.NewCmdList(f)) + + return cmd +} diff --git a/pkg/model/server.go b/pkg/model/server.go index 9c5a2c8..9aba28d 100644 --- a/pkg/model/server.go +++ b/pkg/model/server.go @@ -32,3 +32,24 @@ func (s Server) String() string { func (s Server) IsAvailable() bool { return true } + +type Servers []Server + +func (s Servers) Header() []string { + return []string{"ID", "Name", "Location", "IP"} +} + +func (s Servers) Rows() [][]string { + rows := make([][]string, len(s)) + for i, server := range s { + location := server.IP + if server.Country != nil { + location = *server.Country + if server.City != nil { + location = fmt.Sprintf("%s, %s", *server.City, *server.Country) + } + } + rows[i] = []string{server.GetID(), server.Name, location, server.IP} + } + return rows +}