Files
mstore/cmd/mstored/starter/starter.go
T
2026-03-04 20:27:35 +02:00

81 lines
1.5 KiB
Go

/*
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
*
* This work is published and licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* Distribution of this work is permitted, but commercial use and
* modifications are strictly prohibited.
*/
package starter
import (
"os"
"path/filepath"
"mstore/app/server"
"github.com/spf13/cobra"
)
type Starter struct {
runAsDaemon bool
port int64
cmd *cobra.Command
srv *server.Server
}
func NewStarter() *Starter {
execName := filepath.Base(os.Args[0])
sta := &Starter{}
cmd := &cobra.Command{
Use: execName,
Short: "\nArtifact storage service",
SilenceUsage: true,
RunE: sta.run,
}
cmd.CompletionOptions.DisableDefaultCmd = true
cmd.Flags().BoolVarP(&sta.runAsDaemon, "asDaemon", "D", true, "Run service as daemon")
cmd.Flags().Int64VarP(&sta.port, "port", "P", 1025, "Service port")
sta.cmd = cmd
return sta
}
func (sta *Starter) GetCmd() *cobra.Command {
return sta.cmd
}
func (sta *Starter) run(cmd *cobra.Command, args []string) error {
var err error
srv, err := server.NewServer()
if err != nil {
return err
}
err = srv.Configure()
if err != nil {
return err
}
srv.SetAsDaemon(sta.runAsDaemon)
srv.SetPort(sta.port)
err = srv.Daemonize()
if err != nil {
return err
}
err = srv.Build()
if err != nil {
return err
}
err = srv.Run()
if err != nil {
return err
}
sta.srv = srv
return err
}
func (sta *Starter) Exec() error {
return sta.cmd.Execute()
}