This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
74 lines (61 loc) · 1.96 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package main
import (
"context"
"io"
"log"
"os"
"github.com/mitchellh/cli"
cmdExec "github.com/hashicorp/consul-api-gateway/internal/commands/exec"
cmdServer "github.com/hashicorp/consul-api-gateway/internal/commands/server"
cmdVersion "github.com/hashicorp/consul-api-gateway/internal/commands/version"
"github.com/hashicorp/consul-api-gateway/internal/version"
)
func main() {
ui := &cli.BasicUi{Writer: os.Stdout, ErrorWriter: os.Stderr}
os.Exit(run(os.Args[1:], ui, os.Stdout))
}
func run(args []string, ui cli.Ui, logOutput io.Writer) int {
c := cli.NewCLI("consul-api-gateway", version.GetHumanVersion())
c.Args = args
c.Commands = initializeCommands(ui, logOutput)
c.HelpFunc = helpFunc(c.Commands)
c.HelpWriter = logOutput
exitStatus, err := c.Run()
if err != nil {
log.Println(err)
}
return exitStatus
}
func initializeCommands(ui cli.Ui, logOutput io.Writer) map[string]cli.CommandFactory {
commands := map[string]cli.CommandFactory{
"server": func() (cli.Command, error) {
return cmdServer.New(context.Background(), ui, logOutput), nil
},
"exec": func() (cli.Command, error) {
return cmdExec.New(context.Background(), ui, logOutput), nil
},
"version": func() (cli.Command, error) {
return &cmdVersion.Command{UI: ui, Version: version.GetHumanVersion()}, nil
},
}
return commands
}
func helpFunc(commands map[string]cli.CommandFactory) cli.HelpFunc {
// This should be updated for any commands we want to hide for any reason.
// Hidden commands can still be executed if you know the command, but
// aren't shown in any help output. We use this for prerelease functionality
// or advanced features.
hidden := map[string]struct{}{
"exec": {},
"server": {},
}
var include []string
for k := range commands {
if _, ok := hidden[k]; !ok {
include = append(include, k)
}
}
return cli.FilteredHelpFunc(include, cli.BasicHelpFunc("consul-api-gateway"))
}