1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-05 03:07:38 +00:00

add one more project structure recommendation

This commit is contained in:
Gerasimos (Makis) Maropoulos
2021-11-19 14:13:42 +02:00
parent d62392bebc
commit 226a4cb064
15 changed files with 1166 additions and 1 deletions

View File

@@ -0,0 +1,57 @@
package cmd
import (
"github.com/username/project/api"
"github.com/spf13/cobra"
)
const defaultConfigFilename = "server.yml"
var serverConfig api.Configuration
// New returns a new CLI app.
// Build with:
// $ go build -ldflags="-s -w"
func New(buildRevision, buildTime string) *cobra.Command {
configFile := defaultConfigFilename
rootCmd := &cobra.Command{
Use: "project",
Short: "Command line interface for project.",
Long: "The root command will start the HTTP server.",
Version: "v0.0.1",
SilenceErrors: true,
SilenceUsage: true,
TraverseChildren: true,
SuggestionsMinimumDistance: 1,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// Read configuration from file before any of the commands run functions.
return serverConfig.BindFile(configFile)
},
RunE: func(cmd *cobra.Command, args []string) error {
return startServer()
},
}
helpTemplate := HelpTemplate{
BuildRevision: buildRevision,
BuildTime: buildTime,
ShowGoRuntimeVersion: true,
}
rootCmd.SetHelpTemplate(helpTemplate.String())
// Shared flags.
flags := rootCmd.PersistentFlags()
flags.StringVar(&configFile, "config", configFile, "--config=server.yml a filepath which contains the YAML config format")
// Subcommands here.
// rootCmd.AddCommand(...)
return rootCmd
}
func startServer() error {
srv := api.NewServer(serverConfig)
return srv.Start()
}

View File

@@ -0,0 +1,43 @@
package cmd
import (
"fmt"
"runtime"
"strconv"
"strings"
"time"
)
// HelpTemplate is the structure which holds the necessary information for the help command.
type HelpTemplate struct {
BuildTime string
BuildRevision string
ShowGoRuntimeVersion bool
Template fmt.Stringer
}
func (h HelpTemplate) String() string {
tmpl := `{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}
{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
if h.BuildRevision != "" {
buildTitle := ">>>> build" // if we ever want an emoji, there is one: \U0001f4bb
tab := strings.Repeat(" ", len(buildTitle))
n, _ := strconv.ParseInt(h.BuildTime, 10, 64)
buildTimeStr := time.Unix(n, 0).Format(time.UnixDate)
buildTmpl := fmt.Sprintf("\n%s\n", buildTitle) +
fmt.Sprintf("%s revision %s\n", tab, h.BuildRevision) +
fmt.Sprintf("%s datetime %s\n", tab, buildTimeStr)
if h.ShowGoRuntimeVersion {
buildTmpl += fmt.Sprintf("%s runtime %s\n", tab, runtime.Version())
}
tmpl += buildTmpl
}
return tmpl
}