github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/kbfstool/git_rename.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  
     7  	"github.com/keybase/client/go/kbfs/env"
     8  	"github.com/keybase/client/go/kbfs/fsrpc"
     9  	"github.com/keybase/client/go/kbfs/libgit"
    10  	"github.com/keybase/client/go/kbfs/libkbfs"
    11  	"github.com/keybase/client/go/protocol/keybase1"
    12  	"golang.org/x/net/context"
    13  )
    14  
    15  const gitRenameUsageStr = `Usage:
    16    kbfstool git rename /keybase/tlf/path oldName newName
    17  `
    18  
    19  func doGitRename(ctx context.Context,
    20  	rpcHandler *libgit.RPCHandler, tlfStr, oldName, newName string) error {
    21  	p, err := fsrpc.NewPath(tlfStr)
    22  	if err != nil {
    23  		return err
    24  	}
    25  	if p.PathType != fsrpc.TLFPathType {
    26  		return fmt.Errorf("%q is not a TLF path", tlfStr)
    27  	}
    28  	if len(p.TLFComponents) > 0 {
    29  		return fmt.Errorf("%q is not the root path of a TLF", tlfStr)
    30  	}
    31  	folder := keybase1.FolderHandle{
    32  		Name:       p.TLFName,
    33  		FolderType: p.TLFType.FolderType(),
    34  	}
    35  
    36  	return rpcHandler.RenameRepo(ctx, folder, oldName, newName)
    37  }
    38  
    39  func gitRename(ctx context.Context, config libkbfs.Config, args []string) (exitStatus int) {
    40  	flags := flag.NewFlagSet("kbfs git rename", flag.ContinueOnError)
    41  	err := flags.Parse(args)
    42  	if err != nil {
    43  		printError("git rename", err)
    44  		return 1
    45  	}
    46  
    47  	inputs := flags.Args()
    48  	if len(inputs) != 3 {
    49  		fmt.Print(gitRenameUsageStr)
    50  		return 1
    51  	}
    52  
    53  	kbfsCtx := env.NewContext()
    54  	rpcHandler, shutdown := libgit.NewRPCHandlerWithCtx(kbfsCtx, config, nil)
    55  	defer shutdown()
    56  
    57  	err = doGitRename(ctx, rpcHandler, inputs[0], inputs[1], inputs[2])
    58  	if err != nil {
    59  		printError("git rename", err)
    60  		return 1
    61  	}
    62  
    63  	return 0
    64  }