Moving server files to ./server
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"github.com/cortezaproject/corteza-server/pkg/version"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
rootCommand = &cobra.Command{
|
||||
Use: "corteza-server",
|
||||
Aliases: []string{"corteza", "server"},
|
||||
TraverseChildren: true,
|
||||
}
|
||||
|
||||
serveApiCommand = &cobra.Command{
|
||||
Use: "serve-api",
|
||||
Aliases: []string{"serve"},
|
||||
|
||||
Short: "Start HTTP server with REST API",
|
||||
}
|
||||
|
||||
upgradeCommand = &cobra.Command{
|
||||
Use: "upgrade",
|
||||
Short: "Upgrade tasks",
|
||||
}
|
||||
|
||||
provisionCommand = &cobra.Command{
|
||||
Use: "provision",
|
||||
Short: "Provision tasks",
|
||||
}
|
||||
|
||||
versionCommand = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Version",
|
||||
}
|
||||
|
||||
// List of commands that do not
|
||||
// run initialization (db connection, etc...)
|
||||
light = map[string]bool{
|
||||
"help": true,
|
||||
versionCommand.Use: true,
|
||||
}
|
||||
)
|
||||
|
||||
// RootCommand creates root command with a simple persistent-pre-run callback
|
||||
//
|
||||
// Callback is called when not executed with help subcommand
|
||||
func RootCommand(ppRunEfn func() error) *cobra.Command {
|
||||
rootCommand.PersistentPreRunE = func(cmd *cobra.Command, args []string) (err error) {
|
||||
if light[cmd.Name()] {
|
||||
// Do not run hooks on parts on help command
|
||||
return nil
|
||||
}
|
||||
|
||||
if ppRunEfn != nil {
|
||||
return ppRunEfn()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return rootCommand
|
||||
}
|
||||
|
||||
func ServeCommand(runEfn func() error) *cobra.Command {
|
||||
serveApiCommand.RunE = func(cmd *cobra.Command, args []string) (err error) {
|
||||
return runEfn()
|
||||
}
|
||||
|
||||
return serveApiCommand
|
||||
}
|
||||
|
||||
func UpgradeCommand(dbfn func() error) *cobra.Command {
|
||||
upgradeCommand.RunE = func(cmd *cobra.Command, args []string) (err error) {
|
||||
return dbfn()
|
||||
}
|
||||
|
||||
return upgradeCommand
|
||||
}
|
||||
|
||||
func ProvisionCommand(cffn func() error) *cobra.Command {
|
||||
provisionCommand.RunE = func(cmd *cobra.Command, args []string) (err error) {
|
||||
return cffn()
|
||||
}
|
||||
|
||||
return provisionCommand
|
||||
}
|
||||
|
||||
func VersionCommand() *cobra.Command {
|
||||
versionCommand.Run = func(cmd *cobra.Command, args []string) {
|
||||
cmd.Println(version.Version)
|
||||
}
|
||||
|
||||
return versionCommand
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
ctxwrap "github.com/SentimensRG/ctx"
|
||||
"github.com/SentimensRG/ctx/sigctx"
|
||||
)
|
||||
|
||||
// Context is small wrapper that returns sig-term bound context
|
||||
//
|
||||
// This can be used as (proper) background context that properly terminates
|
||||
// all subroutines.
|
||||
func Context() context.Context {
|
||||
return ctxwrap.AsContext(sigctx.New())
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"sort"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/errors"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// LoadEnv loads env-variables (KEY=VAL) from provided files and paths (by searching for .env file in the given path)
|
||||
//
|
||||
// Please note that loaded values
|
||||
// DO NOT OVERRIDE the existing
|
||||
// environmental variables
|
||||
func LoadEnv(pp ...string) error {
|
||||
// preparse the input and try to figure out if .env should be appended
|
||||
var checked = make([]string, 0, len(pp))
|
||||
for _, p := range pp {
|
||||
if s, err := os.Stat(p); err != nil {
|
||||
return err
|
||||
} else if s.IsDir() {
|
||||
chk := path.Join(p, ".env")
|
||||
if _, err = os.Stat(chk); err == nil {
|
||||
// make sure only .env files
|
||||
checked = append(checked, chk)
|
||||
} else if !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
checked = append(checked, p)
|
||||
}
|
||||
}
|
||||
|
||||
if len(checked) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return godotenv.Load(checked...)
|
||||
}
|
||||
|
||||
// EnvCommand outputs all loaded env variables
|
||||
func EnvCommand() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "env",
|
||||
Long: "Outputs list (sorted by key) of of all environmental variables. Can be used for diagnosis and debugging",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
kv := os.Environ()
|
||||
sort.Strings(kv)
|
||||
for _, l := range kv {
|
||||
cmd.Println(l)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func HandleError(err error) {
|
||||
if err = unwrapGeneric(err); err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprintln(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func unwrapGeneric(err error) error {
|
||||
for {
|
||||
g, ok := err.(interface{ IsGeneric() bool })
|
||||
if ok && g != nil && g.IsGeneric() {
|
||||
err = errors.Unwrap(err)
|
||||
continue
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user