Skip to content

Commit

Permalink
more config cmd changes to server
Browse files Browse the repository at this point in the history
  • Loading branch information
mindhash committed Oct 3, 2018
1 parent ac92946 commit 6b41136
Show file tree
Hide file tree
Showing 20 changed files with 285 additions and 245 deletions.
Binary file added bin/hflserver
Binary file not shown.
15 changes: 6 additions & 9 deletions src/hyperview.in/server/base/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@ package base

import (
"os"
"fmt"
)



func SetEnv() {
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", "/Users/apple/MyProjects/creds/hw-storage-75d060e8419a.json")
os.Setenv("GOOGLE_STORAGE_BUCKET", "hyperview001")
}



func SetEnvVar(name, value string) error {
os.SetEnv(name, value)
os.Setenv(name, value)
fmt.Println("Environment value: ", name, os.Getenv(name))

return nil
}

func GetEnvVar(name string) string {
return os.GetEnv(name)
return os.Getenv(name)
}

func GetEnv(name string) string{
Expand Down
5 changes: 2 additions & 3 deletions src/hyperview.in/server/base/home_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package base


import(
"os"
"fmt"
"runtime"
)


func HomeDir() (string, error) {
var home string
if runtime.GOOS = "windows" {
if runtime.GOOS == "windows" {
return "TODO", nil
} else {
return unixHome()
Expand All @@ -19,7 +18,7 @@ func HomeDir() (string, error) {
}



// expand ~ in home directory
func unixHome() (string, error) {
home := GetEnv("HOME")
if home != "" {
Expand Down
101 changes: 77 additions & 24 deletions src/hyperview.in/server/cmd/hflserver/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import(
"os"
"github.com/spf13/cobra"

"hyperview.in/server/base"
"hyperview.in/server/rest"
"hyperview.in/server/config"
)
Expand All @@ -21,7 +22,7 @@ func RootCmd() (*cobra.Command, error) {
Long: `hyperflow Server Command line`,
Run: func(cmd *cobra.Command, args []string) {
base.Info("Starting Server... ")
rest.ServerMain(addr)
rest.StartServer(addr)
},
}

Expand All @@ -46,6 +47,7 @@ func configCmd() (*cobra.Command) {
var gcs_bucket string

var log_path string
var log_level string

config_cmd := &cobra.Command {
Use: "set",
Expand All @@ -55,14 +57,17 @@ func configCmd() (*cobra.Command) {

if config_path != "" {
config.SetConfigPath(config_path)
exitWithSuccess()
}

var c *config.Config
c, err := config.GetConfig()

if err != nil {
exitWithError(err)
exitWithError(err.Error())
}


if storage_opt != "" {
c.StorageOption = storage_opt
}
Expand All @@ -76,17 +81,17 @@ func configCmd() (*cobra.Command) {
}
err = config.UpdateConfig(c)
if err != nil {
exitWithError(err)
exitWithError(err.Error())
}
}
},
}

// variable sets HFLOW_CONFIG_PATH variable
config_cmd.PersistentFlags().StringVarP(&config_path, "config", "c", "", "config path")
config_cmd.Flags().StringVarP(&config_path, "config", "c", "", "config path")

config_cmd.PersistentFlags().StringVarP(&storage_opt, "storage", "sb", "", "Storage backend. e.g. GCS or S3")
config_cmd.PersistentFlags().StringVarP(&log_path, "log-path", "", "", "log path")
config_cmd.PersistentFlags().StringVarP(&log_level, "log-level", "", "5", "log level")
config_cmd.Flags().StringVar(&storage_opt, "storage-option", "", "Storage backend. e.g. GCS or S3")
config_cmd.Flags().StringVar(&log_path, "log-path", "", "log path")
config_cmd.Flags().StringVar(&log_level, "log-level", "5", "log level")

s3_cmd := &cobra.Command {
Use: "s3",
Expand All @@ -97,7 +102,7 @@ func configCmd() (*cobra.Command) {
var c *config.Config
c, err := config.GetConfig()
if err != nil {
exitWithError(err)
exitWithError(err.Error())
}

if c.S3 == nil {
Expand All @@ -122,12 +127,12 @@ func configCmd() (*cobra.Command) {

err = config.UpdateConfig(c)
if err != nil {
exitWithError(err)
exitWithError(err.Error())
}
}
},
}

s3_cmd.Flags().StringVarP(&s3_creds, "cred-path", "", "", "S3 storage credentials Path")
s3_cmd.Flags().StringVarP(&s3_creds, "file", "f", "", "S3 storage credentials Path")
s3_cmd.Flags().StringVarP(&s3_access_key, "access-key", "", "", "S3 storage Access key")
s3_cmd.Flags().StringVarP(&s3_secret_key, "secret-key", "", "", "S3 storage Secret key")
s3_cmd.Flags().StringVarP(&s3_bucket, "bucket", "", "", "S3 storage Bucket")
Expand All @@ -143,7 +148,7 @@ func configCmd() (*cobra.Command) {
var c *config.Config
c, err := config.GetConfig()
if err != nil {
exitWithError(err)
exitWithError(err.Error())
}

if c.Gcs == nil {
Expand All @@ -160,24 +165,72 @@ func configCmd() (*cobra.Command) {

err = config.UpdateConfig(c)
if err != nil {
exitWithError(err)
exitWithError(err.Error())
}
}
},
}

gcs_cmd.Flags().StringVarP(&gcs_creds, "credentials", "", "", "GCS storage credentials")
gcs_cmd.Flags().StringVarP(&gcs_bucket, "bucket", "", "", "GCS storage Bucket")
gcs_cmd.Flags().StringVarP(&gcs_creds, "file", "f", "", "GCS storage credentials")
gcs_cmd.Flags().StringVarP(&gcs_bucket, "bucket", "", "hyperflow001", "GCS storage Bucket")

config_cmd.AddCommand(gcs_cmd)

return config_cmd
}
var db_name string
var db_type string
var db_user string
var db_pass string
var db_file string

db_cmd := &cobra.Command {
Use: "db",
Short: "Set DB Config default variables for hyperflow",
Long: `Set DB Config default variables for hyperflow`,
Run: func(cmd *cobra.Command, args []string) {
var c *config.Config
c, err := config.GetConfig()
if err != nil {
exitWithError(err.Error())
}

if c.DB == nil {
c.DB = &config.DbConfig {}
}

if db_driver != "" {
c.DB.Driver = db_driver
}

if db_name != "" {
c.DB.Name = db_name
}

if db_user != "" {
c.DB.User = db_user
}

if db_pass != "" {
c.DB.Pass = db_pass
}

if db_file != "" {
c.DB.File = db_file
}

func exitWithError(format string, args ...interface{}) {
if errString := strings.TrimSpace(fmt.Sprintf(format, args...)); errString != "" {
fmt.Fprintf(os.Stderr, "%s\n", errString)
err = config.UpdateConfig(c)
if err != nil {
exitWithError(err.Error())
}
},
}
os.Exit(1)
}
db_cmd.Flags().StringVarP(&db_driver, "Driver", "t", "POSTGRES", "Type of database. eg. POSTGRES")
db_cmd.Flags().StringVarP(&db_name, "name", "n", "", "Name of database")
db_cmd.Flags().StringVarP(&db_user, "user", "u", "", "Username of database")
db_cmd.Flags().StringVarP(&db_pass, "pass", "p", "", "Name of database")
db_cmd.Flags().StringVarP(&db_file, "file", "f", "", "file that contains connection string")

config_cmd.AddCommand(db_cmd)

return config_cmd
}


21 changes: 19 additions & 2 deletions src/hyperview.in/server/cmd/hflserver/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
package main
import(
"os"
"fmt"
"strings"

)


func exitWithError(format string, args ...interface{}) {
if errString := strings.TrimSpace(fmt.Sprintf(format, args...)); errString != "" {
fmt.Fprintf(os.Stderr, "%s\n", errString)
}
os.Exit(1)
}

func exitWithSuccess() {
os.Exit(0)
}

func main() {
var err error

rootCommand, err := cmd.RootCmd()
rootCommand, err := RootCmd()
if err != nil {
cmd.ExitWithError(err.Error())
exitWithError(err.Error())
}

rootCommand.Execute()
Expand Down
Loading

0 comments on commit 6b41136

Please sign in to comment.