package cmd import ( "context" "log" "gitea.loveuer.com/loveuer/cluster/internal/api" "gitea.loveuer.com/loveuer/cluster/internal/opt" "gitea.loveuer.com/loveuer/cluster/pkg/database/db" "gitea.loveuer.com/loveuer/cluster/pkg/store" "gitea.loveuer.com/loveuer/cluster/pkg/tool" "github.com/spf13/cobra" ) func Run(ctx context.Context) error { _cmd := &cobra.Command{ Use: "cluster", Short: "Cluster is a lightweight OCI registry implementation written in Go using Fiber v3.", RunE: func(cmd *cobra.Command, args []string) error { var ( err error stopFns = []func(context.Context) error{} stopApi func(context.Context) error ) if err = opt.Init(cmd.Context()); err != nil { return err } if err = db.Init(cmd.Context(), opt.GlobalDataDir); err != nil { return err } if err = store.Init(cmd.Context(), opt.GlobalDataDir); err != nil { return err } if stopApi, err = api.Init(cmd.Context(), opt.GlobalAddress, db.Default, store.Default); err != nil { return err } stopFns = append(stopFns, stopApi) <-cmd.Context().Done() log.Println("[W] 收到退出信号,开始退出...") tool.MustStop(tool.Timeout(5), stopFns...) return nil }, } _cmd.PersistentFlags().BoolVar(&opt.GlobalDebug, "debug", false, "Enable debug mode") _cmd.PersistentFlags().StringVarP(&opt.GlobalAddress, "address", "A", "0.0.0.0:9119", "API server listen address") _cmd.PersistentFlags().StringVarP(&opt.GlobalDataDir, "data-dir", "D", "./x-storage", "Data directory for storing all data") return _cmd.ExecuteContext(ctx) }