diff --git a/pkg/odo/cli/apiserver/apiserver.go b/pkg/odo/cli/apiserver/apiserver.go new file mode 100644 index 000000000..a7596dfc2 --- /dev/null +++ b/pkg/odo/cli/apiserver/apiserver.go @@ -0,0 +1,94 @@ +package apiserver + +import ( + "context" + "fmt" + + apiserver_impl "github.com/redhat-developer/odo/pkg/apiserver-impl" + "github.com/redhat-developer/odo/pkg/odo/cmdline" + "github.com/redhat-developer/odo/pkg/odo/genericclioptions" + "github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset" + "github.com/spf13/cobra" + "k8s.io/klog" +) + +const ( + RecommendedCommandName = "api-server" +) + +type ApiServerOptions struct { + clientset *clientset.Clientset + portFlag int +} + +func NewApiServerOptions() *ApiServerOptions { + return &ApiServerOptions{} +} + +var _ genericclioptions.Runnable = (*ApiServerOptions)(nil) +var _ genericclioptions.SignalHandler = (*ApiServerOptions)(nil) +var _ genericclioptions.Cleanuper = (*ApiServerOptions)(nil) + +func (o *ApiServerOptions) SetClientset(clientset *clientset.Clientset) { + o.clientset = clientset +} + +func (o *ApiServerOptions) Complete(ctx context.Context, cmdline cmdline.Cmdline, args []string) error { + return nil +} + +func (o *ApiServerOptions) Validate(ctx context.Context) error { + return nil +} + +func (o *ApiServerOptions) Run(ctx context.Context) (err error) { + err = o.clientset.StateClient.Init(ctx) + if err != nil { + err = fmt.Errorf("unable to save state file: %w", err) + return err + } + + ctx, cancel := context.WithCancel(ctx) + _ = apiserver_impl.StartServer( + ctx, + cancel, + o.portFlag, + nil, + nil, + o.clientset.StateClient, + ) + <-ctx.Done() + return nil +} + +func (o *ApiServerOptions) Cleanup(ctx context.Context, commandError error) error { + err := o.clientset.StateClient.SaveExit(ctx) + if err != nil { + klog.V(1).Infof("unable to persist dev state: %v", err) + } + return nil +} + +func (o *ApiServerOptions) HandleSignal(ctx context.Context, cancelFunc context.CancelFunc) error { + cancelFunc() + return nil +} + +// NewCmdApiServer implements the odo api-server command +func NewCmdApiServer(ctx context.Context, name, fullName string, testClientset clientset.Clientset) *cobra.Command { + o := NewApiServerOptions() + apiserverCmd := &cobra.Command{ + Use: name, + Short: "Start the API server", + Long: "Start the API server", + Args: cobra.MaximumNArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + return genericclioptions.GenericRun(o, testClientset, cmd, args) + }, + } + clientset.Add(apiserverCmd, + clientset.STATE, + ) + apiserverCmd.Flags().IntVar(&o.portFlag, "port", 0, "Define custom port for API Server.") + return apiserverCmd +} diff --git a/pkg/odo/cli/cli.go b/pkg/odo/cli/cli.go index 7d49b15c3..e4cc45a42 100644 --- a/pkg/odo/cli/cli.go +++ b/pkg/odo/cli/cli.go @@ -9,6 +9,8 @@ import ( "strings" "unicode" + "github.com/redhat-developer/odo/pkg/odo/cli/apiserver" + "github.com/redhat-developer/odo/pkg/odo/cli/feature" "github.com/redhat-developer/odo/pkg/odo/cli/logs" "github.com/redhat-developer/odo/pkg/odo/cli/run" "github.com/redhat-developer/odo/pkg/odo/commonflags" @@ -202,6 +204,9 @@ func odoRootCmd(ctx context.Context, name, fullName string, unknownCmdHandler fu completion.NewCmdCompletion(completion.RecommendedCommandName, util.GetFullName(fullName, completion.RecommendedCommandName)), run.NewCmdRun(run.RecommendedCommandName, util.GetFullName(fullName, run.RecommendedCommandName), testClientset), ) + if feature.IsExperimentalModeEnabled(ctx) { + rootCmdList = append(rootCmdList, apiserver.NewCmdApiServer(ctx, apiserver.RecommendedCommandName, util.GetFullName(fullName, apiserver.RecommendedCommandName), testClientset)) + } // Add all subcommands to base commands rootCmd.AddCommand(rootCmdList...) diff --git a/pkg/util/util.go b/pkg/util/util.go index 18e9cb4b7..a71cd4cca 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -640,8 +640,6 @@ func StartSignalWatcher(watchSignals []os.Signal, handle func(receivedSignal os. receivedSignal := <-signals handle(receivedSignal) - // exit here to stop spinners from rotating - os.Exit(1) } // cleanDir cleans the original folder during events like interrupted copy etc