From 23631df2662f0c6554207dbdc76a27cd2aa7760c Mon Sep 17 00:00:00 2001 From: Nikhil Saraf <1028334+nikhilsaraf@users.noreply.github.com> Date: Fri, 12 Feb 2021 20:02:02 +0530 Subject: [PATCH] clean up root.go basic kelp binary invocation logic, closes #568 (#658) * 1 - remove windows startup bat file and copy script * 2 - add isGuiBinary flag from build.sh script and recognize in code * 3 - force set noElectron=false for windows * 4 - add comment in server_noop.go about the purpose of that file * 5 - use (buildType string) instead of (isGuiBinary bool) because of error building compiling ... # github.com/stellar/kelp github.com/stellar/kelp/cmd.isGuiBinary: cannot set with -X: not a var of type string (type.bool) --- cmd/root.go | 10 ++++++++-- cmd/server_amd64.go | 9 +++++++-- cmd/server_noop.go | 4 ++-- cmd/version.go | 1 + gui/windows-bat-file/kelp-start.bat | 1 - scripts/build.sh | 24 +++++++++++++++--------- 6 files changed, 33 insertions(+), 16 deletions(-) delete mode 100755 gui/windows-bat-file/kelp-start.bat diff --git a/cmd/root.go b/cmd/root.go index 873b6537b..3de61f70a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/spf13/cobra" + "github.com/stellar/kelp/support/networking" "github.com/stellar/kelp/support/sdk" "github.com/stellar/kelp/support/utils" @@ -21,6 +22,7 @@ var buildDate string var env string var amplitudeAPIKey string var goarm string +var buildType string // set from the build script, cli or gui const envRelease = "release" const envDev = "dev" @@ -49,13 +51,17 @@ var RootCmd = &cobra.Command{ ` fmt.Println(intro) - if hasUICapability { + if buildType == "gui" { + // if this is the GUI binary then we want to start off with the server command serverCmd.Run(ccmd, args) - } else { + } else if buildType == "cli" { + // else start off with the help command e := ccmd.Help() if e != nil { panic(e) } + } else { + panic(fmt.Sprintf("unrecognized buildType: %s", buildType)) } }, } diff --git a/cmd/server_amd64.go b/cmd/server_amd64.go index d5c0eccb4..4e979b554 100644 --- a/cmd/server_amd64.go +++ b/cmd/server_amd64.go @@ -70,8 +70,6 @@ type serverInputs struct { } func init() { - hasUICapability = true - options := serverInputs{} options.port = serverCmd.Flags().Uint16P("port", "p", 8000, "port on which to serve") options.dev = serverCmd.Flags().Bool("dev", false, "run in dev mode for hot-reloading of JS code") @@ -126,6 +124,13 @@ func init() { } } + if runtime.GOOS == "windows" { + if *options.noElectron { + log.Printf("input options had specified noElectron=true for winndows, but that is not supported on windows yet. force setting noElectron=false for windows.\n") + *options.noElectron = false + } + } + // create a latch to trigger the browser opening once the backend server is loaded openBrowserWg := &sync.WaitGroup{} openBrowserWg.Add(1) diff --git a/cmd/server_noop.go b/cmd/server_noop.go index e1d5f76f5..144eb7011 100644 --- a/cmd/server_noop.go +++ b/cmd/server_noop.go @@ -6,8 +6,8 @@ import ( "github.com/spf13/cobra" ) -var hasUICapability = false - +// load the basic serverCmd for all architectures. Default action is a noop action. +// This is overriden for platforms that support this command, example: server_amd64.go var serverCmd = &cobra.Command{ Use: "server", Short: "Serves the Kelp GUI", diff --git a/cmd/version.go b/cmd/version.go index da8cfa083..8c71ca650 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -16,6 +16,7 @@ var versionCmd = &cobra.Command{ fmt.Printf(" git branch: %s\n", gitBranch) fmt.Printf(" git hash: %s\n", gitHash) fmt.Printf(" build date: %s\n", buildDate) + fmt.Printf(" build type: %s\n", buildType) fmt.Printf(" env: %s\n", env) fmt.Printf(" GOOS: %s\n", runtime.GOOS) fmt.Printf(" GOARCH: %s\n", runtime.GOARCH) diff --git a/gui/windows-bat-file/kelp-start.bat b/gui/windows-bat-file/kelp-start.bat deleted file mode 100755 index 8f2ec86f6..000000000 --- a/gui/windows-bat-file/kelp-start.bat +++ /dev/null @@ -1 +0,0 @@ -Kelp.exe server --no-electron \ No newline at end of file diff --git a/scripts/build.sh b/scripts/build.sh index 5e86285d1..2d7033cea 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -281,9 +281,12 @@ then gen_bind_files echo "" + # manually set buildType for CLI + DYNAMIC_LDFLAGS="$LDFLAGS -X github.com/stellar/kelp/cmd.buildType=cli" + # cannot set goarm because not accessible (need to figure out a way) echo -n "compiling ... " - go build -ldflags "$LDFLAGS" -o $OUTFILE + go build -ldflags "$DYNAMIC_LDFLAGS" -o $OUTFILE check_build_result $? echo "successful: $OUTFILE" echo "" @@ -320,11 +323,13 @@ do BINARY="$OUTFILE.exe" fi - DYNAMIC_LDFLAGS="$LDFLAGS" + # manually set buildType for CLI + DYNAMIC_LDFLAGS="$LDFLAGS -X github.com/stellar/kelp/cmd.buildType=cli" + if [[ "$GOARM" != "" ]] then GOARM_FLAGS="-X github.com/stellar/kelp/cmd.goarm=$GOARM" - echo "adding GOARM_FLAGS to ldflags: $GOARM_FLAGS" + echo "adding GOARM_FLAGS to DYNAMIC_LDFLAGS: $GOARM_FLAGS" DYNAMIC_LDFLAGS="$DYNAMIC_LDFLAGS $GOARM_FLAGS" fi @@ -406,20 +411,21 @@ do # generate bundler.json for platform gen_bundler_json -p $GOOS + # manually set buildType for GUI build + DYNAMIC_LDFLAGS="$LDFLAGS -X github.com/stellar/kelp/cmd.buildType=gui" + # manually set buildType for GUI build + DYNAMIC_LDFLAGS_UI="$LDFLAGS_UI -ldflags X:github.com/stellar/kelp/cmd.buildType=gui" + if [[ $GOOS == "windows" ]] then gen_bind_files # compile # need to use cli tool for windows because building a GUI version will trigger the command prompt to open every time we invoke a "bash -c" command which is too frequent echo -n "compiling UI for windows using cli tool instead of using astilectron-bundler (GOOS=$GOOS, GOARCH=$GOARCH) ... " - env GOOS=$GOOS GOARCH=$GOARCH GOARM=$GOARM go build -ldflags "$LDFLAGS" -o $ARCHIVE_DIR_SOURCE_UI/$GOOS-$GOARCH/kelp.exe + env GOOS=$GOOS GOARCH=$GOARCH GOARM=$GOARM go build -ldflags "$DYNAMIC_LDFLAGS" -o $ARCHIVE_DIR_SOURCE_UI/$GOOS-$GOARCH/kelp.exe check_build_result $? echo "successful" - echo -n "copying over kelp-start.bat file to the windows build ..." - cp $KELP/gui/windows-bat-file/kelp-start.bat $ARCHIVE_DIR_SOURCE_UI/$GOOS-$GOARCH/ - echo "done" - # set paths needed for unzipping the vendor and ccxt files VENDOR_FILENAME="" CCXT_FILENAME="ccxt-rest_linux-x64.zip" @@ -429,7 +435,7 @@ do # compile echo "no need to generate bind files separately since we build using astilectron bundler directly for GUI" echo -n "compiling UI for $GOOS via astilectron-bundler (GOOS=$GOOS, GOARCH=$GOARCH) ... " - astilectron-bundler $FLAG -o $ARCHIVE_DIR_SOURCE_UI $LDFLAGS_UI + astilectron-bundler $FLAG -o $ARCHIVE_DIR_SOURCE_UI $DYNAMIC_LDFLAGS_UI check_build_result $? echo "successful"