2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
2022-05-13 05:56:43 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Execute starts the CLI.
|
|
|
|
func Execute() error {
|
2022-05-11 03:20:37 -04:00
|
|
|
rootCmd := NewRootCmd()
|
2022-03-22 11:03:15 -04:00
|
|
|
ctx, cancel := signalContext(context.Background(), os.Interrupt)
|
|
|
|
defer cancel()
|
|
|
|
return rootCmd.ExecuteContext(ctx)
|
|
|
|
}
|
|
|
|
|
2022-05-11 03:20:37 -04:00
|
|
|
// NewRootCmd creates the root command.
|
|
|
|
func NewRootCmd() *cobra.Command {
|
|
|
|
rootCmd := &cobra.Command{
|
|
|
|
Use: "constellation",
|
|
|
|
Short: "Manage your Constellation cluster",
|
|
|
|
Long: "Manage your Constellation cluster.",
|
|
|
|
PersistentPreRun: preRunRoot,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set output of cmd.Print to stdout. (By default, it's stderr.)
|
|
|
|
rootCmd.SetOut(os.Stdout)
|
|
|
|
|
2022-05-13 05:56:43 -04:00
|
|
|
rootCmd.PersistentFlags().String("config", constants.ConfigFilename, "use settings from a config")
|
|
|
|
must(rootCmd.MarkPersistentFlagFilename("config", "json"))
|
2022-05-11 03:20:37 -04:00
|
|
|
|
|
|
|
rootCmd.AddCommand(newCreateCmd())
|
|
|
|
rootCmd.AddCommand(newInitCmd())
|
|
|
|
rootCmd.AddCommand(newVerifyCmd())
|
|
|
|
rootCmd.AddCommand(newRecoverCmd())
|
|
|
|
rootCmd.AddCommand(newTerminateCmd())
|
|
|
|
rootCmd.AddCommand(newVersionCmd())
|
2022-05-12 09:14:52 -04:00
|
|
|
rootCmd.AddCommand(newConfigCmd())
|
2022-05-11 03:20:37 -04:00
|
|
|
|
|
|
|
return rootCmd
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
// signalContext returns a context that is canceled on the handed signal.
|
|
|
|
// The signal isn't watched after its first occurrence. Call the cancel
|
|
|
|
// function to ensure the internal goroutine is stopped and the signal isn't
|
|
|
|
// watched any longer.
|
|
|
|
func signalContext(ctx context.Context, sig os.Signal) (context.Context, context.CancelFunc) {
|
|
|
|
sigCtx, stop := signal.NotifyContext(ctx, sig)
|
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
stopDone := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() { stopDone <- struct{}{} }()
|
|
|
|
defer stop()
|
|
|
|
select {
|
|
|
|
case <-sigCtx.Done():
|
|
|
|
fmt.Println(" Signal caught. Press ctrl+c again to terminate the program immediately.")
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cancelFunc := func() {
|
|
|
|
done <- struct{}{}
|
|
|
|
<-stopDone
|
|
|
|
}
|
|
|
|
|
|
|
|
return sigCtx, cancelFunc
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:42:13 -04:00
|
|
|
func preRunRoot(cmd *cobra.Command, args []string) {
|
|
|
|
cmd.SilenceUsage = true
|
|
|
|
}
|
|
|
|
|
2022-05-04 02:50:50 -04:00
|
|
|
func must(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|