2023-04-21 04:47:07 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
// upload uploads os images.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/image/upload/internal/cmd"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := execute(); err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func execute() error {
|
|
|
|
rootCmd := newRootCmd()
|
|
|
|
ctx, cancel := signalContext(context.Background(), os.Interrupt)
|
|
|
|
defer cancel()
|
|
|
|
return rootCmd.ExecuteContext(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRootCmd() *cobra.Command {
|
|
|
|
rootCmd := &cobra.Command{
|
|
|
|
Use: "upload",
|
2023-05-23 06:12:58 -04:00
|
|
|
Short: "Uploads OS image related artifacts",
|
|
|
|
Long: "Uploads OS image related artifacts.",
|
2023-04-21 04:47:07 -04:00
|
|
|
PersistentPreRun: preRunRoot,
|
|
|
|
}
|
|
|
|
|
|
|
|
rootCmd.SetOut(os.Stdout)
|
|
|
|
|
2023-05-23 06:12:58 -04:00
|
|
|
rootCmd.AddCommand(cmd.NewImageCmd())
|
2023-05-23 09:09:14 -04:00
|
|
|
rootCmd.AddCommand(cmd.NewInfoCmd())
|
|
|
|
rootCmd.AddCommand(cmd.NewMeasurementsCmd())
|
2023-04-21 04:47:07 -04:00
|
|
|
|
|
|
|
return rootCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func preRunRoot(cmd *cobra.Command, _ []string) {
|
|
|
|
cmd.SilenceUsage = true
|
|
|
|
}
|