bashhub-server/cmd/root.go

129 lines
3.3 KiB
Go
Raw Normal View History

/*
*
* Copyright © 2020 nicksherron <nsherron90@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
2020-02-07 16:02:18 +08:00
package cmd
import (
"fmt"
2020-02-08 00:14:22 +08:00
"log"
2020-02-07 16:02:18 +08:00
"os"
2020-02-08 00:14:22 +08:00
"path/filepath"
2020-02-10 15:31:44 +08:00
"strings"
2020-02-07 16:02:18 +08:00
"github.com/fatih/color"
2020-02-08 00:14:22 +08:00
"github.com/nicksherron/bashhub-server/internal"
2020-02-07 16:02:18 +08:00
"github.com/spf13/cobra"
)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
2020-02-10 15:31:44 +08:00
var (
noWarning bool
rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cmd.Flags().Parse(args)
if !noWarning {
checkBhEnv()
}
startupMessage()
internal.Run()
},
}
)
2020-02-10 14:54:50 +08:00
// Execute runs root command
2020-02-07 16:02:18 +08:00
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize()
2020-02-10 15:31:44 +08:00
rootCmd.PersistentFlags().StringVar(&internal.LogFile, "log", "", `Set filepath for HTTP log. "" logs to stderr`)
2020-02-08 00:14:22 +08:00
rootCmd.PersistentFlags().StringVar(&internal.DbPath, "db", dbPath(), "DB location (sqlite or postgres)")
2020-02-10 15:31:44 +08:00
rootCmd.PersistentFlags().StringVarP(&internal.Addr, "addr", "a", listenAddr(), "Ip and port to listen and serve on")
rootCmd.PersistentFlags().BoolVarP(&noWarning, "warning-disable", "w", false, `Disable BH_URL env variable startup warning`)
2020-02-07 16:02:18 +08:00
2020-02-08 00:14:22 +08:00
}
2020-02-10 07:30:05 +08:00
// StartupMessage prints startup banner
func startupMessage() {
banner := fmt.Sprintf(`
_ _ _ _
| | | | | | | | version: %v
| |__ __ _ ___| |__ | |__ _ _| | address: %v
| '_ \ / _' / __| '_ \| '_ \| | | | '_ \
| |_) | (_| \__ \ | | | | | | |_| | |_) |
|_.__/ \__,_|___/_| |_|_| |_|\__,_|_.__/
___ ___ _ ____ _____ _ __
/ __|/ _ \ '__\ \ / / _ \ '__|
\__ \ __/ | \ V / __/ |
|___/\___|_| \_/ \___|_|
`, Version, internal.Addr)
color.HiGreen(banner)
fmt.Print("\n")
log.Printf("Listening and serving HTTP on %v", internal.Addr)
2020-02-10 15:31:44 +08:00
fmt.Print("\n")
}
2020-02-10 07:30:05 +08:00
func listenAddr() string {
var a string
2020-02-10 17:37:41 +08:00
if os.Getenv("BH_SERVER_URL") != "" {
a = os.Getenv("BH_SERVER_URL")
2020-02-10 07:30:05 +08:00
return a
}
2020-02-10 16:30:17 +08:00
a = "http://0.0.0.0:8080"
2020-02-10 07:30:05 +08:00
return a
}
2020-02-08 00:14:22 +08:00
func dbPath() string {
dbFile := "data.db"
f := filepath.Join(appDir(), dbFile)
return f
}
func appDir() string {
cfgDir, err := os.UserConfigDir()
if err != nil {
log.Fatal(err)
}
2020-02-08 07:48:32 +08:00
ch := filepath.Join(cfgDir, "bashhub-server")
2020-02-08 00:14:22 +08:00
err = os.MkdirAll(ch, 0755)
if err != nil {
log.Fatal(err)
}
2020-02-07 16:02:18 +08:00
2020-02-08 00:14:22 +08:00
return ch
2020-02-07 16:02:18 +08:00
}
2020-02-10 15:31:44 +08:00
func checkBhEnv() {
bhURL := os.Getenv("BH_URL")
if strings.Contains(bhURL, "https://bashhub.com") {
msg := fmt.Sprintf(`
2020-02-10 17:37:41 +08:00
WARNING: BH_URL is set to https://bashhub.com on this machine
2020-02-10 15:31:44 +08:00
If you will be running bashhub-client locally be sure to add
2020-02-10 17:37:41 +08:00
export BH_URL=%v to your .bashrc or .zshrc`, internal.Addr)
2020-02-10 15:31:44 +08:00
fmt.Println(msg, "\n")
}
}