github.com/simpleiot/simpleiot@v0.18.3/envsetup.sh (about)

     1  #!/bin/sh
     2  
     3  if [ -f local.sh ]; then
     4  	echo "reading local settings"
     5  	. ./local.sh
     6  fi
     7  
     8  RECOMMENDED_ELM_VERSION=0.19.1
     9  
    10  # map tools from project go modules
    11  
    12  air() {
    13  	go run github.com/cosmtrek/air "$@"
    14  }
    15  
    16  siot_install_proto_gen_go() {
    17  	cd ~ && go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
    18  	cd - || exit
    19  }
    20  
    21  siot_install_frontend_deps() {
    22  	(cd "frontend" && npm install)
    23  	(cd "frontend" && npx elm-tooling install)
    24  	(cd "frontend/lib" && npm ci)
    25  }
    26  
    27  siot_check_elm() {
    28  	# this no longer works with the way we are installing elm
    29  	if ! npx elm --version >/dev/null 2>&1; then
    30  		echo "Please install elm >= 0.19"
    31  		echo "https://guide.elm-lang.org/install.html"
    32  		return 1
    33  	fi
    34  
    35  	version=$(npx elm --version)
    36  	if [ "$version" != "$RECOMMENDED_ELM_VERSION" ]; then
    37  		echo "found elm $version, recommend elm version $RECOMMENDED_ELM_VERSION"
    38  		echo "not sure what will happen otherwise"
    39  	fi
    40  
    41  	return 0
    42  }
    43  
    44  siot_check_go() {
    45  	# Get the installed Go version
    46  	go_version=$(go version | awk '{print $3}' | sed 's/go//g')
    47  
    48  	# Split the version into major, minor, and patch components
    49  	major=$(echo "$go_version" | awk -F'.' '{print $1}')
    50  	minor=$(echo "$go_version" | awk -F'.' '{print $2}')
    51  	patch=$(echo "$go_version" | awk -F'.' '{print $3}')
    52  
    53  	# Check if the version is greater than 1.22
    54  	if [ "$major" -gt 1 ] || { [ "$major" -eq 1 ] && [ "$minor" -gt 22 ]; } || { [ "$major" -eq 1 ] && [ "$minor" -eq 22 ] && [ "$patch" -gt 0 ]; }; then
    55  		echo "Go version $go_version is greater than 1.22"
    56  		return 0
    57  	else
    58  		echo "Go version $go_version is not greater than 1.22"
    59  		return 1
    60  	fi
    61  }
    62  
    63  siot_setup() {
    64  	siot_check_go || return 1
    65  	siot_install_frontend_deps
    66  	# the following is to work around a race condition
    67  	# where the first time you run npx elm, you get an error:
    68  	# elm: Text file busy
    69  	(cd frontend && (npx elm || true))
    70  	# make sure elm-spa auto-generated stuff is set up
    71  	(cd frontend && npx elm-spa build)
    72  	return 0
    73  }
    74  
    75  siot_build_frontend() {
    76  	(cd "frontend" && npx elm-spa build) || return 1
    77  	gzip -f frontend/public/dist/elm.js
    78  	return 0
    79  }
    80  
    81  siot_version() {
    82  	git describe --tags HEAD
    83  }
    84  
    85  siot_build_backend() {
    86  	BINARY_NAME=siot
    87  	if [ "${GOOS}" = "windows" ]; then
    88  		BINARY_NAME=siot.exe
    89  	fi
    90  	CGO_ENABLED=0 go build -ldflags="-s -w -X main.version=$(siot_version)" -o $BINARY_NAME cmd/siot/main.go || return 1
    91  	return 0
    92  }
    93  
    94  siot_build() {
    95  	siot_build_frontend || return 1
    96  	siot_build_backend || return 1
    97  }
    98  
    99  siot_build_arm() {
   100  	siot_build_frontend || return 1
   101  	GOARCH=arm GOARM=7 go build -ldflags="-s -w -X main.version=$(siot_version)" -o siot_arm cmd/siot/main.go || return 1
   102  	return 0
   103  }
   104  
   105  siot_build_arm64() {
   106  	siot_build_frontend || return 1
   107  	GOARCH=arm64 go build -ldflags="-s -w -X main.version=$(siot_version)" -o siot_arm64 cmd/siot/main.go || return 1
   108  	return 0
   109  }
   110  
   111  siot_build_arm_debug() {
   112  	siot_build_frontend || return 1
   113  	GOARCH=arm GOARM=7 go build -ldflags="-s -w -X main.version=$(siot_version)" -o siot_arm cmd/siot/main.go || return 1
   114  	return 0
   115  }
   116  
   117  siot_deploy() {
   118  	siot_build_frontend || return 1
   119  	gcloud app deploy cmd/portal || return 1
   120  	return 0
   121  }
   122  
   123  siot_run() {
   124  	siot_build_frontend || return 1
   125  	go build -ldflags="-X main.version=$(siot_version)" -o siot -race cmd/siot/main.go || return 1
   126  	./siot "$@"
   127  	return 0
   128  }
   129  
   130  # run siot_mkcert first
   131  siot_run_tls() {
   132  	export SIOT_NATS_TLS_CERT=server-cert.pem
   133  	export SIOT_NATS_TLS_KEY=server-key.pem
   134  	siot_build_frontend || return 1
   135  	go run cmd/siot/main.go "$@" || return 1
   136  	return 0
   137  }
   138  
   139  # please install mkcert and run mkcert -install first
   140  siot_mkcert() {
   141  	mkcert -cert-file server-cert.pem -key-file server-key.pem localhost ::1
   142  }
   143  
   144  find_src_files() {
   145  	find . -not \( -path ./frontend/src/Spa/Generated -prune \) -not \( -path ./assets -prune \) -name "*.go"
   146  }
   147  
   148  siot_watch_go() {
   149  	echo "watch args: $*"
   150  	air serve -dev "$*"
   151  }
   152  
   153  siot_watch_elm() {
   154  	(cd frontend && npx elm-watch hot) || false
   155  }
   156  
   157  siot_watch() {
   158  	npx run-pty \
   159  		% /bin/sh -c ". ./envsetup.sh && siot_watch_elm" \
   160  		% /bin/sh -c ". ./envsetup.sh && siot_watch_go $*"
   161  }
   162  
   163  # TODO finish this and add to siot_test ...
   164  check_go_format() {
   165  	gofiles=$(find . -name "*.go")
   166  	unformatted=$(gofmt -l "$gofiles")
   167  	if [ -n "$unformatted" ]; then
   168  		return 1
   169  	fi
   170  	return 0
   171  }
   172  
   173  siot_test_frontend() {
   174  	(cd frontend && npx elm-test || return 1) || return 1
   175  	(cd frontend && npx elm-review || return 1) || return 1
   176  }
   177  
   178  siot_test_frontend_lib() {
   179  	(cd ./frontend/lib && npm run lint || return 1) || return 1
   180  	echo "Starting SimpleIOT..."
   181  	./siot serve --store siot_test_frontend_lib.sqlite --resetStore 2>/dev/null &
   182  	PID=$!
   183  	sleep 1
   184  	(cd ./frontend/lib && npm run test || return 1)
   185  	CODE=$?
   186  	echo "Stopping SimpleIOT..."
   187  	kill -s SIGINT $PID
   188  	wait $PID
   189  	echo "SimpleIOT Stopped"
   190  	if [ "$CODE" = "0" ]; then
   191  		rm siot_test_frontend_lib.sqlite
   192  	fi
   193  }
   194  
   195  siot_frontend_fix() {
   196  	(cd frontend && npx elm-review --fix-all)
   197  }
   198  
   199  # please run the following before pushing -- best if your editor can be set up
   200  # to do this automatically.
   201  siot_test() {
   202  	echo "Build frontend ..."
   203  	siot_build_frontend || return 1
   204  	echo "Test frontend ..."
   205  	siot_test_frontend || return 1
   206  	echo "Test backend ..."
   207  	go test -p=1 -race "$@" ./... || return 1
   208  	echo "Lint backend ..."
   209  	golangci-lint run || return 1
   210  	echo "Testing passed :-)"
   211  	return 0
   212  }
   213  
   214  # following can be used to set up influxdb for local testing
   215  siot_setup_influx() {
   216  	export SIOT_INFLUX_URL=http://localhost:8086
   217  	#export SIOT_INFLUX_USER=admin
   218  	#export SIOT_INFLUX_PASS=admin
   219  	export SIOT_INFLUX_DB=siot
   220  }
   221  
   222  siot_protobuf_go() {
   223  	protoc --proto_path=internal/pb internal/pb/*.proto --go_out=./ || return 1
   224  }
   225  
   226  siot_protobuf_js() {
   227  	protoc --proto_path=internal/pb internal/pb/*.proto --js_out=import_style=commonjs,binary:./frontend/lib/protobuf/ || return 1
   228  }
   229  
   230  siot_protobuf() {
   231  	echo "generating protobufs"
   232  	siot_protobuf_go
   233  	siot_protobuf_js
   234  }
   235  
   236  siot_edge_run() {
   237  	go run cmd/edge/main.go "$*"
   238  }
   239  
   240  # download goreleaser from https://github.com/goreleaser/goreleaser/releases/
   241  # and put in /usr/local/bin
   242  # This can be useful to test/debug the release process locally
   243  siot_goreleaser_build() {
   244  	goreleaser build --skip-validate --rm-dist
   245  }
   246  
   247  # before releasing, you need to tag the release
   248  # you need to provide GITHUB_TOKEN in env or ~/.config/goreleaser/github_token
   249  # generate tokens: https://github.com/settings/tokens/new
   250  # enable repo and workflow sections
   251  siot_release() {
   252  	VERSION=$1
   253  	if [ -z "$VERSION" ]; then
   254  		echo "must provide version in format vX.Y.Z"
   255  		return 1
   256  	fi
   257  
   258  	# update elm.js.gz
   259  	siot_build_frontend || return 1
   260  	git commit -m "update FE assets" frontend/public/dist/elm.js.gz || return 1
   261  	git push || return 1
   262  	git tag -f "$VERSION" || return 1
   263  	goreleaser release --clean || return 1
   264  	siot_deploy_docs || return 1
   265  	# refresh godocs site
   266  	wget "https://proxy.golang.org/github.com/simpleiot/simpleiot/@v/${VERSION}.info" || return 1
   267  	rm "${VERSION}.info"
   268  }
   269  
   270  # dblab keyboard shortcuts
   271  # - Ctrl+space execute query
   272  # - Ctrl+H,J,K,L move to panel left,below,above,right
   273  # see more keybindings here: https://github.com/danvergara/dblab#key-bindings
   274  siot_dblab() {
   275  	STORE=siot.sqlite
   276  	if [ "$1" != "" ]; then
   277  		STORE=$1
   278  	fi
   279  	go run github.com/danvergara/dblab@latest --db "$STORE" --driver sqlite3
   280  }
   281  
   282  siot_mdbook() {
   283  	mdbook serve -p 3333
   284  }
   285  
   286  siot_mdbook_cleanup() {
   287  	rm -rf book
   288  }
   289  
   290  siot_deploy_docs() {
   291  	(cd /scratch/bec/ops/ &&
   292  		ansible-playbook -i production all.yml --limit tmpdir --tags docs.simpleiot.org)
   293  }