1package cmd
2
3import (
4 "fmt"
5
6 appCfg "github.com/charmbracelet/soft-serve/internal/config"
7 "github.com/gliderlabs/ssh"
8 "github.com/spf13/cobra"
9)
10
11var (
12 // ErrUnauthorized is returned when the user is not authorized to perform action.
13 ErrUnauthorized = fmt.Errorf("Unauthorized")
14 // ErrRepoNotFound is returned when the repo is not found.
15 ErrRepoNotFound = fmt.Errorf("Repository not found")
16 // ErrFileNotFound is returned when the file is not found.
17 ErrFileNotFound = fmt.Errorf("File not found")
18
19 usageTemplate = `Usage:{{if .Runnable}}{{if .HasParent }}
20 {{.Parent.Use}} {{end}}{{.Use}}{{if .HasAvailableFlags }} [flags]{{end}}{{end}}{{if .HasAvailableSubCommands}}
21 {{if .HasParent }}{{.Parent.Use}} {{end}}{{.Use}} [command]{{end}}{{if gt (len .Aliases) 0}}
22
23Aliases:
24 {{.NameAndAliases}}{{end}}{{if .HasExample}}
25
26Examples:
27{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
28
29Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
30 {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
31
32Flags:
33{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
34
35Global Flags:
36{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
37
38Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
39 {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
40
41Use "{{.UseLine}} [command] --help" for more information about a command.{{end}}
42`
43)
44
45// RootCommand is the root command for the server.
46func RootCommand() *cobra.Command {
47 rootCmd := &cobra.Command{
48 Use: "ssh [-p PORT] HOST",
49 Long: "Soft Serve is a self-hostable Git server for the command line.",
50 Args: cobra.MinimumNArgs(1),
51 DisableFlagsInUseLine: true,
52 }
53 rootCmd.SetUsageTemplate(usageTemplate)
54 rootCmd.CompletionOptions.DisableDefaultCmd = true
55 rootCmd.AddCommand(
56 ReloadCommand(),
57 CatCommand(),
58 ListCommand(),
59 GitCommand(),
60 InternalCommand(),
61 )
62
63 return rootCmd
64}
65
66func fromContext(cmd *cobra.Command) (*appCfg.Config, ssh.Session) {
67 ctx := cmd.Context()
68 ac := ctx.Value("config").(*appCfg.Config)
69 s := ctx.Value("session").(ssh.Session)
70 return ac, s
71}