soft-serve

Fork https://github.com/charmbracelet/soft-serve

git clone git://git.lin.moe/go/soft-serve.git

  1package main
  2
  3import (
  4	"context"
  5	"os"
  6	"runtime/debug"
  7
  8	"github.com/charmbracelet/log/v2"
  9	"github.com/charmbracelet/soft-serve/cmd/soft/admin"
 10	"github.com/charmbracelet/soft-serve/cmd/soft/hook"
 11	"github.com/charmbracelet/soft-serve/cmd/soft/serve"
 12	"github.com/charmbracelet/soft-serve/pkg/config"
 13	logr "github.com/charmbracelet/soft-serve/pkg/log"
 14	"github.com/charmbracelet/soft-serve/pkg/version"
 15	"github.com/spf13/cobra"
 16	"go.uber.org/automaxprocs/maxprocs"
 17)
 18
 19var (
 20	// Version contains the application version number. It's set via ldflags
 21	// when building.
 22	Version = ""
 23
 24	// CommitSHA contains the SHA of the commit that this application was built
 25	// against. It's set via ldflags when building.
 26	CommitSHA = ""
 27
 28	// CommitDate contains the date of the commit that this application was
 29	// built against. It's set via ldflags when building.
 30	CommitDate = ""
 31
 32	rootCmd = &cobra.Command{
 33		Use:          "soft",
 34		Short:        "A self-hostable Git server for the command line",
 35		Long:         "Soft Serve is a self-hostable Git server for the command line.",
 36		SilenceUsage: true,
 37	}
 38)
 39
 40func init() {
 41	rootCmd.AddCommand(
 42		serve.Command,
 43		hook.Command,
 44		admin.Command,
 45	)
 46	rootCmd.CompletionOptions.HiddenDefaultCmd = true
 47
 48	if len(CommitSHA) >= 7 {
 49		vt := rootCmd.VersionTemplate()
 50		rootCmd.SetVersionTemplate(vt[:len(vt)-1] + " (" + CommitSHA[0:7] + ")\n")
 51	}
 52	if Version == "" {
 53		if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
 54			Version = info.Main.Version
 55		} else {
 56			Version = "unknown (built from source)"
 57		}
 58	}
 59	rootCmd.Version = Version
 60
 61	version.Version = Version
 62	version.CommitSHA = CommitSHA
 63	version.CommitDate = CommitDate
 64}
 65
 66func main() {
 67	ctx := context.Background()
 68	cfg := config.DefaultConfig()
 69	if cfg.Exist() {
 70		if err := cfg.Parse(); err != nil {
 71			log.Fatal(err)
 72		}
 73	}
 74
 75	if err := cfg.ParseEnv(); err != nil {
 76		log.Fatal(err)
 77	}
 78
 79	ctx = config.WithContext(ctx, cfg)
 80	logger, f, err := logr.NewLogger(cfg)
 81	if err != nil {
 82		log.Errorf("failed to create logger: %v", err)
 83	}
 84
 85	ctx = log.WithContext(ctx, logger)
 86	if f != nil {
 87		defer f.Close() // nolint: errcheck
 88	}
 89
 90	// Set global logger
 91	log.SetDefault(logger)
 92
 93	var opts []maxprocs.Option
 94	if config.IsVerbose() {
 95		opts = append(opts, maxprocs.Logger(log.Debugf))
 96	}
 97
 98	// Set the max number of processes to the number of CPUs
 99	// This is useful when running soft serve in a container
100	if _, err := maxprocs.Set(opts...); err != nil {
101		log.Warn("couldn't set automaxprocs", "error", err)
102	}
103
104	if err := rootCmd.ExecuteContext(ctx); err != nil {
105		os.Exit(1)
106	}
107}