root.go

 1package main
 2
 3import (
 4	"context"
 5	"os"
 6	"runtime/debug"
 7	"strconv"
 8	"strings"
 9	"time"
10
11	"github.com/charmbracelet/log"
12	"github.com/spf13/cobra"
13)
14
15var (
16	// Version contains the application version number. It's set via ldflags
17	// when building.
18	Version = ""
19
20	// CommitSHA contains the SHA of the commit that this application was built
21	// against. It's set via ldflags when building.
22	CommitSHA = ""
23
24	rootCmd = &cobra.Command{
25		Use:          "soft",
26		Short:        "A self-hostable Git server for the command line",
27		Long:         "Soft Serve is a self-hostable Git server for the command line.",
28		SilenceUsage: true,
29	}
30)
31
32func init() {
33	rootCmd.AddCommand(
34		serveCmd,
35		manCmd,
36		hookCmd,
37		migrateConfig,
38	)
39	rootCmd.CompletionOptions.HiddenDefaultCmd = true
40
41	if len(CommitSHA) >= 7 {
42		vt := rootCmd.VersionTemplate()
43		rootCmd.SetVersionTemplate(vt[:len(vt)-1] + " (" + CommitSHA[0:7] + ")\n")
44	}
45	if Version == "" {
46		if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
47			Version = info.Main.Version
48		} else {
49			Version = "unknown (built from source)"
50		}
51	}
52	rootCmd.Version = Version
53}
54
55func main() {
56	ctx := context.Background()
57	logger := log.NewWithOptions(os.Stderr, log.Options{
58		ReportTimestamp: true,
59		TimeFormat:      time.DateOnly,
60	})
61	if debug, _ := strconv.ParseBool(os.Getenv("SOFT_SERVE_DEBUG")); debug {
62		logger.SetLevel(log.DebugLevel)
63	}
64
65	switch strings.ToLower(os.Getenv("SOFT_SERVE_LOG_FORMAT")) {
66	case "json":
67		logger.SetFormatter(log.JSONFormatter)
68	case "logfmt":
69		logger.SetFormatter(log.LogfmtFormatter)
70	case "text":
71		logger.SetFormatter(log.TextFormatter)
72	}
73
74	ctx = log.WithContext(ctx, logger)
75	if err := rootCmd.ExecuteContext(ctx); err != nil {
76		os.Exit(1)
77	}
78}