github.com/keybase/client/go@v0.0.0-20240520164431-4f512a4c85a3/client/cmd_chat_archive_list.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"slices"
     6  	"strings"
     7  
     8  	"github.com/keybase/cli"
     9  	"github.com/keybase/client/go/chatrender"
    10  	"github.com/keybase/client/go/libcmdline"
    11  	"github.com/keybase/client/go/libkb"
    12  	gregor1 "github.com/keybase/client/go/protocol/gregor1"
    13  	"github.com/keybase/client/go/protocol/keybase1"
    14  	"golang.org/x/net/context"
    15  )
    16  
    17  type CmdChatArchiveList struct {
    18  	libkb.Contextified
    19  }
    20  
    21  func NewCmdChatArchiveListListRunner(g *libkb.GlobalContext) *CmdChatArchiveList {
    22  	return &CmdChatArchiveList{
    23  		Contextified: libkb.NewContextified(g),
    24  	}
    25  }
    26  
    27  func newCmdChatArchiveList(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
    28  	return cli.Command{
    29  		Name:  "archive-list",
    30  		Usage: "List metadata about chat archive jobs",
    31  		Action: func(c *cli.Context) {
    32  			cl.ChooseCommand(NewCmdChatArchiveListListRunner(g), "archive-list", c)
    33  			cl.SetLogForward(libcmdline.LogForwardNone)
    34  		},
    35  	}
    36  }
    37  
    38  func (c *CmdChatArchiveList) Run() error {
    39  	client, err := GetChatLocalClient(c.G())
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	res, err := client.ArchiveChatList(context.TODO(), keybase1.TLFIdentifyBehavior_CHAT_CLI)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	ui := c.G().UI.GetTerminalUI()
    49  	ui.Printf("Found %d job(s)\n\n", len(res.Jobs))
    50  	for _, job := range res.Jobs {
    51  		percent := 100
    52  		if job.MessagesTotal > 0 {
    53  			percent = int((100 * job.MessagesComplete) / job.MessagesTotal)
    54  		}
    55  		matchingConvs := ""
    56  		if job.Request.Query == nil || (job.Request.Query.Name == nil && job.Request.Query.TopicName == nil && len(job.Request.Query.ConvIDs) == 0) {
    57  			matchingConvs = "<all chat>"
    58  		} else {
    59  			names := make([]string, 0, len(job.MatchingConvs))
    60  			for _, conv := range job.MatchingConvs {
    61  				name := conv.Name
    62  				if len(name) == 0 {
    63  					continue
    64  				}
    65  				if conv.Channel != "" {
    66  					name += fmt.Sprintf("#%s", conv.Channel)
    67  				}
    68  				names = append(names, name)
    69  			}
    70  			slices.Sort(names)
    71  			matchingConvs += strings.Join(names, "\n")
    72  		}
    73  		ui.Printf(`Job ID: %s
    74  Matching Conversations:
    75  %s
    76  Output Path: %s
    77  Started At: %s (%s)
    78  Status: %s
    79  Progress: %d%% (%d of %d messages archived)
    80  `, job.Request.JobID, matchingConvs, job.Request.OutputPath,
    81  			chatrender.FmtTime(gregor1.FromTime(job.StartedAt), chatrender.RenderOptions{UseDateTime: true}),
    82  			chatrender.FmtTime(gregor1.FromTime(job.StartedAt), chatrender.RenderOptions{}),
    83  			job.Status.String(), percent, job.MessagesComplete, job.MessagesTotal)
    84  		if job.Err != "" {
    85  			ui.Printf("Err: %s\n", job.Err)
    86  		}
    87  		ui.Printf("\n")
    88  	}
    89  	return nil
    90  }
    91  
    92  func (c *CmdChatArchiveList) ParseArgv(ctx *cli.Context) (err error) {
    93  	if len(ctx.Args()) > 0 {
    94  		return fmt.Errorf("no arguments required")
    95  	}
    96  	return nil
    97  }
    98  
    99  func (c *CmdChatArchiveList) GetUsage() libkb.Usage {
   100  	return libkb.Usage{
   101  		Config: true,
   102  		API:    true,
   103  	}
   104  }