github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/tools/teamchain/main.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"flag"
     6  	"fmt"
     7  	"os"
     8  
     9  	"github.com/davecgh/go-spew/spew"
    10  	"github.com/keybase/client/go/libkb"
    11  	"github.com/keybase/client/go/logger"
    12  	"github.com/keybase/client/go/protocol/keybase1"
    13  	"github.com/keybase/client/go/teams"
    14  )
    15  
    16  type ChainFile struct {
    17  	Chain []teams.SCChainLink `json:"chain"`
    18  }
    19  
    20  func main() {
    21  	err := main2()
    22  	if err != nil {
    23  		fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
    24  		os.Exit(1)
    25  	}
    26  }
    27  
    28  func main2() (err error) {
    29  	var silent bool
    30  	flag.BoolVar(&silent, "silent", false, "print nothing")
    31  	var filepath string
    32  	flag.StringVar(&filepath, "path", "", "path to file containing json team chain")
    33  	flag.Parse()
    34  	if filepath == "" {
    35  		flag.Usage()
    36  		return fmt.Errorf("missing required path flag")
    37  	}
    38  
    39  	var cf ChainFile
    40  	if filepath == "-" {
    41  		cf, err = readChainStdin()
    42  	} else {
    43  		cf, err = readChainFile(filepath)
    44  	}
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	g := libkb.NewGlobalContext().Init()
    50  	g.Log = logger.New("sc")
    51  	if err := g.ConfigureCaches(); err != nil {
    52  		return err
    53  	}
    54  	mctx := libkb.NewMetaContextBackground(g)
    55  	var reader keybase1.UserVersion
    56  	var state *teams.TeamSigChainState
    57  	for _, prelink := range cf.Chain {
    58  		link, err := teams.UnpackChainLink(&prelink)
    59  		if err != nil {
    60  			return err
    61  		}
    62  
    63  		implicitAdmin := link.TeamAdmin() != nil // Assume all admin claims are OK.
    64  		signerX := teams.NewSignerX(
    65  			keybase1.NewUserVersion(prelink.UID, prelink.EldestSeqno), implicitAdmin)
    66  		newState, err := teams.AppendChainLink(mctx.Ctx(), g, reader, state, link, &signerX)
    67  		if err != nil {
    68  			return err
    69  		}
    70  		state = &newState
    71  	}
    72  	if !silent {
    73  		fmt.Printf("%v\n", spew.Sdump(state))
    74  	}
    75  	return nil
    76  }
    77  
    78  func readChainStdin() (res ChainFile, err error) {
    79  	err = json.NewDecoder(os.Stdin).Decode(&res)
    80  	return res, err
    81  }
    82  
    83  func readChainFile(path string) (res ChainFile, err error) {
    84  	f, err := os.Open(path)
    85  	if err != nil {
    86  		return res, err
    87  	}
    88  	err = json.NewDecoder(f).Decode(&res)
    89  	return res, err
    90  }