From 929542140f1d72d890f1d5a89974d445df2cbb90 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Tue, 28 Oct 2025 20:53:23 -0700 Subject: [PATCH] list --- api/client.go | 9 ------- cmd/cmd.go | 51 +--------------------------------------- cmd/interactive.go | 2 +- cmd/list.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 60 deletions(-) create mode 100644 cmd/list.go diff --git a/api/client.go b/api/client.go index cb7dd885a..c4a8a75ca 100644 --- a/api/client.go +++ b/api/client.go @@ -352,15 +352,6 @@ func (c *Client) Create(ctx context.Context, req *CreateRequest, fn CreateProgre }) } -// List lists models that are available locally. -func (c *Client) List(ctx context.Context) (*ListResponse, error) { - var lr ListResponse - if err := c.do(ctx, http.MethodGet, "/api/tags", nil, &lr); err != nil { - return nil, err - } - return &lr, nil -} - // ListRunning lists running models. func (c *Client) ListRunning(ctx context.Context) (*ProcessResponse, error) { var lr ProcessResponse diff --git a/cmd/cmd.go b/cmd/cmd.go index e07ce2665..5b04a1864 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -620,46 +620,6 @@ func PushHandler(cmd *cobra.Command, args []string) error { return nil } -func ListHandler(cmd *cobra.Command, args []string) error { - client, err := api.ClientFromEnvironment() - if err != nil { - return err - } - - models, err := client.List(cmd.Context()) - if err != nil { - return err - } - - var data [][]string - - for _, m := range models.Models { - if len(args) == 0 || strings.HasPrefix(strings.ToLower(m.Name), strings.ToLower(args[0])) { - var size string - if m.RemoteModel != "" { - size = "-" - } else { - size = format.HumanBytes(m.Size) - } - - data = append(data, []string{m.Name, m.Digest[:12], size, format.HumanTime(m.ModifiedAt, "Never")}) - } - } - - table := tablewriter.NewWriter(os.Stdout) - table.SetHeader([]string{"NAME", "ID", "SIZE", "MODIFIED"}) - table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) - table.SetAlignment(tablewriter.ALIGN_LEFT) - table.SetHeaderLine(false) - table.SetBorder(false) - table.SetNoWhiteSpace(true) - table.SetTablePadding(" ") - table.AppendBulk(data) - table.Render() - - return nil -} - func ListRunningHandler(cmd *cobra.Command, args []string) error { client, err := api.ClientFromEnvironment() if err != nil { @@ -1728,14 +1688,6 @@ func NewCLI() *cobra.Command { RunE: SignoutHandler, } - listCmd := &cobra.Command{ - Use: "list", - Aliases: []string{"ls"}, - Short: "List models", - PreRunE: checkServerHeartbeat, - RunE: ListHandler, - } - psCmd := &cobra.Command{ Use: "ps", Short: "List running models", @@ -1781,7 +1733,6 @@ func NewCLI() *cobra.Command { stopCmd, pullCmd, pushCmd, - listCmd, psCmd, copyCmd, deleteCmd, @@ -1824,7 +1775,7 @@ func NewCLI() *cobra.Command { pushCmd, signinCmd, signoutCmd, - listCmd, + cmdList(), psCmd, copyCmd, deleteCmd, diff --git a/cmd/interactive.go b/cmd/interactive.go index cf0aced14..08472b7e6 100644 --- a/cmd/interactive.go +++ b/cmd/interactive.go @@ -186,7 +186,7 @@ func generateInteractive(cmd *cobra.Command, opts runOptions) error { continue case strings.HasPrefix(line, "/list"): args := strings.Fields(line) - if err := ListHandler(cmd, args[1:]); err != nil { + if err := listHandler(cmd, args[1:]); err != nil { return err } case strings.HasPrefix(line, "/load"): diff --git a/cmd/list.go b/cmd/list.go new file mode 100644 index 000000000..efe361c3d --- /dev/null +++ b/cmd/list.go @@ -0,0 +1,58 @@ +package cmd + +import ( + "os" + "strings" + + "github.com/olekukonko/tablewriter" + "github.com/ollama/ollama/client" + "github.com/ollama/ollama/format" + "github.com/spf13/cobra" +) + +func cmdList() *cobra.Command { + return &cobra.Command{ + Use: "list [pattern]", + Aliases: []string{"ls"}, + Short: "List available models in the local repository", + Args: cobra.MaximumNArgs(1), + PreRunE: checkServerHeartbeat, + RunE: listHandler, + } +} + +func listHandler(cmd *cobra.Command, args []string) error { + c := client.New() + w, err := c.List(cmd.Context()) + if err != nil { + return err + } + + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader([]string{"NAME", "ID", "SIZE", "MODIFIED"}) + table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) + table.SetAlignment(tablewriter.ALIGN_LEFT) + table.SetHeaderLine(false) + table.SetBorder(false) + table.SetNoWhiteSpace(true) + table.SetTablePadding(" ") + + for _, m := range w.Models { + if len(args) == 0 || strings.HasPrefix(strings.ToLower(m.Name), strings.ToLower(args[0])) { + size := format.HumanBytes(m.Size) + if m.RemoteModel != "" { + size = "-" + } + table.Append([]string{ + m.Model, + m.Digest[:12], + size, + format.HumanTime(m.ModifiedAt, "Never"), + }) + } + } + + table.Render() + + return nil +}