From 5c4c4efc57ab3ad4bb46d3a5ba16ad712bb2c6c8 Mon Sep 17 00:00:00 2001 From: Arun Ramanathan Date: Tue, 20 Aug 2024 21:49:41 +0530 Subject: [PATCH] provide default config --- .github/workflows/release.yml | 6 +++--- {network => cmd}/config.yaml | 0 cmd/root.go | 21 ++++++++++++++++++- go.mod | 2 +- scripts/darwin/build.sh | 29 -------------------------- scripts/darwin/build2.sh | 38 ----------------------------------- 6 files changed, 24 insertions(+), 72 deletions(-) rename {network => cmd}/config.yaml (100%) delete mode 100755 scripts/darwin/build.sh delete mode 100755 scripts/darwin/build2.sh diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1d83e01d..62547adf 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -66,7 +66,7 @@ jobs: - name: Setup run : | mkdir -p ${{ env.OUTPUT_DIR }} - cp ${{ env.SRC_DIR }}/network/config.yaml ${{ env.OUTPUT_DIR }} + cp ${{ env.SRC_DIR }}/cmd/config.yaml ${{ env.OUTPUT_DIR }} - name: Build Docker image for linux/amd64 run: | @@ -170,8 +170,8 @@ jobs: run : | mkdir -p ${{ env.OUTPUT_DIR }}/amd64 mkdir -p ${{ env.OUTPUT_DIR }}/arm64 - cp ${{ env.SRC_DIR }}/network/config.yaml ${{ env.OUTPUT_DIR }}/amd64/ - cp ${{ env.SRC_DIR }}/network/config.yaml ${{ env.OUTPUT_DIR }}/arm64/ + cp ${{ env.SRC_DIR }}/cmd/config.yaml ${{ env.OUTPUT_DIR }}/amd64/ + cp ${{ env.SRC_DIR }}/cmd/config.yaml ${{ env.OUTPUT_DIR }}/arm64/ - name: Build Docker image run: | diff --git a/network/config.yaml b/cmd/config.yaml similarity index 100% rename from network/config.yaml rename to cmd/config.yaml diff --git a/cmd/root.go b/cmd/root.go index d45b2636..85998a12 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,16 +1,19 @@ package cmd import ( + _ "embed" "encoding/json" "fmt" "io/ioutil" "os" "path/filepath" + "strings" "github.com/0chain/gosdk/core/conf" "github.com/0chain/gosdk/core/logger" "github.com/spf13/cobra" + "github.com/spf13/viper" "github.com/0chain/gosdk/zboxcore/blockchain" "github.com/0chain/zboxcli/util" @@ -34,6 +37,9 @@ var allocUnderRepair bool var walletJSON string +//go:embed config.yaml +var configStr string + var rootCmd = &cobra.Command{ Use: "zbox", Short: "zbox is a decentralized storage application written on the 0Chain platform", @@ -77,7 +83,20 @@ func initConfig() { cfg, err := conf.LoadConfigFile(filepath.Join(configDir, cfgFile)) if err != nil { fmt.Println("Can't read config:", err) - os.Exit(1) + fmt.Println("WARNING: using default config...") + fmt.Printf("config: %v", configStr) + v := viper.New() + v.SetConfigType("yaml") + err := v.ReadConfig(strings.NewReader(configStr)) + if err != nil { + fmt.Println("error reading default config:", err) + os.Exit(1) + } + cfg, err = conf.LoadConfig(v) + if err != nil { + fmt.Println("error loading default config:", err) + os.Exit(1) + } } if networkFile == "" { diff --git a/go.mod b/go.mod index 536876ae..393af58c 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/olekukonko/tablewriter v0.0.5 github.com/spf13/cobra v1.6.0 github.com/spf13/pflag v1.0.5 + github.com/spf13/viper v1.15.0 gopkg.in/cheggaaa/pb.v1 v1.0.28 ) @@ -57,7 +58,6 @@ require ( github.com/spf13/afero v1.9.3 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect - github.com/spf13/viper v1.15.0 // indirect github.com/subosito/gotenv v1.4.2 // indirect github.com/tinylib/msgp v1.1.6 // indirect github.com/tklauser/go-sysconf v0.3.11 // indirect diff --git a/scripts/darwin/build.sh b/scripts/darwin/build.sh deleted file mode 100755 index 88a4c378..00000000 --- a/scripts/darwin/build.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash - -set -e - -# apt-get install -y clang cmake git patch python-is-python3 libssl-dev lzma-dev libxml2-dev xz-utils bzip2 cpio libbz2-dev zlib1g-dev libc++-dev libc++abi-dev build-essential -# dpkg -L libc++-dev -# dpkg -L libc++abi-dev - -export OSXCROSS_ROOT=$HOME/osxcross -export PATH=$OSXCROSS_ROOT/target/bin:$PATH -export SDKROOT=$OSXCROSS_ROOT/target/SDK/MacOSX11.3.sdk -# export CFLAGS="-isysroot $SDKROOT -stdlib=libc++" -# export CXXFLAGS="-isysroot $SDKROOT -stdlib=libc++" -# export LDFLAGS="-isysroot $SDKROOT -stdlib=libc++" -# Set up Go environment -export GO111MODULE=on -export CGO_ENABLED=1 - -VERSION=1.16.0 - -# Cross-compile for Darwin/amd64 -echo "Building for Darwin/amd64..." -CC=o64-clang CXX=o64-clang++ GOOS=darwin GOARCH=amd64 ~/sdk/go1.21.8/bin/go build -x -v -tags bn256 -ldflags "-X main.VersionStr=${VERSION}" -o zboxcli-darwin-amd64 . - -# Cross-compile for Darwin/arm64 -echo "Building for Darwin/arm64..." -CC=o64-clang CXX=o64-clang++ GOOS=darwin GOARCH=arm64 ~/sdk/go1.21.8/bin/go build -x -v -tags bn256 -ldflags "-X main.VersionStr=${VERSION}" -o zboxcli-darwin-arm64 . - -echo "Build completed." diff --git a/scripts/darwin/build2.sh b/scripts/darwin/build2.sh deleted file mode 100755 index 808f0885..00000000 --- a/scripts/darwin/build2.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash - -set -e - -SRC_DIR=$PWD -HOME_DIR=$HOME - -echo "PWD=$SRC_DIR , HOME=$HOME_DIR" - -# sudo apt-get update -sudo apt-get install -y clang cmake git patch python libssl-dev lzma-dev libxml2-dev xz-utils bzip2 cpio libbz2-dev zlib1g-dev - -cd $HOME_DIR -rm -rf $HOME_DIR/osxcross -git clone https://github.com/tpoechtrager/osxcross.git -cd osxcross/tarballs -wget https://github.com/phracker/MacOSX-SDKs/releases/download/11.3/MacOSX11.3.sdk.tar.xz -cd .. -export CC=clang -export CXX=clang++ -UNATTENDED=yes ./build.sh - -export OSXCROSS_ROOT=$PWD -export PATH=$OSXCROSS_ROOT/target/bin:$PATH -export GO111MODULE=on -export CGO_ENABLED=1 - -# Cross-compile for Darwin/amd64 -echo "Building for Darwin/amd64..." -CC=o64-clang CXX=o64-clang++ GOOS=darwin GOARCH=amd64 go build -x -v -tags bn256 -ldflags "-X main.VersionStr=${VERSION} -linkmode 'external' -extldflags '-static'" -o zboxcli-darwin-amd64 . - -# Cross-compile for Darwin/arm64 -echo "Building for Darwin/arm64..." -CC=o64-clang CXX=o64-clang++ GOOS=darwin GOARCH=arm64 go build -x -v -tags bn256 -ldflags "-X main.VersionStr=${VERSION} -linkmode 'external' -extldflags '-static'" -o zboxcli-darwin-arm64 . - -echo "Build completed." - -echo "osxcross setup success!" \ No newline at end of file