// Copyright (C) 2023-2024 Umorpha Systems // SPDX-License-Identifier: AGPL-3.0-or-later package main import ( "context" "fmt" "os" "github.com/datawire/ocibuild/pkg/cliutil" "github.com/spf13/cobra" "git.mothstuff.lol/lukeshu/eclipse/lib/apikeys" ) func main() { argparser := &cobra.Command{ Use: os.Args[0] + " {[flags]|SUBCOMMAND...}", Short: "Generate API keys", Args: cliutil.WrapPositionalArgs(cliutil.OnlySubcommands), RunE: cliutil.RunSubcommands, SilenceErrors: true, // we'll handle this ourselves after .ExecuteContext() SilenceUsage: true, // FlagErrorFunc will handle this CompletionOptions: cobra.CompletionOptions{ DisableDefaultCmd: true, }, } argparser.SetFlagErrorFunc(cliutil.FlagErrorFunc) argparser.SetHelpTemplate(cliutil.HelpTemplate) argparser.AddCommand(&cobra.Command{ Use: "leader [flags] OUT_PUB_FILE OUT_PRIV_FILE", Args: cliutil.WrapPositionalArgs(cobra.ExactArgs(2)), RunE: func(_ *cobra.Command, args []string) error { pubFilename := args[0] privFilename := args[1] pub, priv, err := apikeys.GenerateLeaderKeys() if err != nil { return err } if err := apikeys.WriteLeaderPubKey(pubFilename, pub); err != nil { return err } if err := apikeys.WriteLeaderPrivKey(privFilename, priv); err != nil { return err } return nil }, }) argparser.AddCommand(&cobra.Command{ Use: "follower [flags] IN_PRIV_FILE OUT_APIKEY_FILE", Args: cliutil.WrapPositionalArgs(cobra.ExactArgs(2)), RunE: func(_ *cobra.Command, args []string) error { inPrivKeyFilename := args[0] outAPIKeyFilename := args[1] privKey, err := apikeys.LoadLeaderPrivKey(inPrivKeyFilename) if err != nil { return err } apikey, err := apikeys.GenerateAPIKey(privKey) if err != nil { return err } return os.WriteFile(outAPIKeyFilename, []byte(apikey+"\n"), 0400) }, }) ctx := context.Background() if err := argparser.ExecuteContext(ctx); err != nil { fmt.Fprintf(os.Stderr, "%v: error: %v\n", argparser.CommandPath(), err) os.Exit(1) } }