1package main
 2
 3import (
 4	"context"
 5	"os"
 6	"runtime/debug"
 7
 8	"github.com/charmbracelet/log"
 9	. "github.com/charmbracelet/soft-serve/internal/log"
10	"github.com/spf13/cobra"
11	"go.uber.org/automaxprocs/maxprocs"
12)
13
14var (
15	// Version contains the application version number. It's set via ldflags
16	// when building.
17	Version = ""
18
19	// CommitSHA contains the SHA of the commit that this application was built
20	// against. It's set via ldflags when building.
21	CommitSHA = ""
22
23	rootCmd = &cobra.Command{
24		Use:          "soft",
25		Short:        "A self-hostable Git server for the command line",
26		Long:         "Soft Serve is a self-hostable Git server for the command line.",
27		SilenceUsage: true,
28	}
29)
30
31func init() {
32	rootCmd.AddCommand(
33		serveCmd,
34		manCmd,
35		hookCmd,
36		migrateConfig,
37	)
38	rootCmd.CompletionOptions.HiddenDefaultCmd = true
39
40	if len(CommitSHA) >= 7 {
41		vt := rootCmd.VersionTemplate()
42		rootCmd.SetVersionTemplate(vt[:len(vt)-1] + " (" + CommitSHA[0:7] + ")\n")
43	}
44	if Version == "" {
45		if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
46			Version = info.Main.Version
47		} else {
48			Version = "unknown (built from source)"
49		}
50	}
51	rootCmd.Version = Version
52}
53
54func main() {
55	logger := NewDefaultLogger()
56
57	// Set global logger
58	log.SetDefault(logger)
59
60	// Set the max number of processes to the number of CPUs
61	// This is useful when running soft serve in a container
62	if _, err := maxprocs.Set(maxprocs.Logger(logger.Debugf)); err != nil {
63		logger.Warn("couldn't set automaxprocs", "error", err)
64	}
65
66	ctx := log.WithContext(context.Background(), logger)
67	if err := rootCmd.ExecuteContext(ctx); err != nil {
68		os.Exit(1)
69	}
70}