Script does not work with changing proj structure

I have the following make.sh file that is working on the following project:

 myapp utils run.go auth.go server.go make.sh 

When I run this script , it creates the expected tar and everything works!

 #!/bin/sh go get ./... rm -r /tmp/myapp rm /tmp/myapp.tar.gz mkdir /tmp/myapp go build -o /tmp/myapp/myapp_mac env GOOS=windows GOARH=amd64 go build -o /tmp/myapp/myapp_win64.exe env GOOS=linux GOARCH=amd64 go build -o /tmp/myapp/myapp cp -R ./resources /tmp/myapp/ cd /tmp tar -czf myapp.tar.gz myapp 

Now I needed to change the project structure to the following:

 myapp make.sh src utils run.go auth.go server server.go 

Now, when I ran make.sh, I got an error:

 can't load package: package myapp: no Go files in /Users/i023333/go/src/myapp 

Any idea how to adapt it?

I try to put make.sh inside the server folder as it is, and it creates tar, but its invalid ... any idea that I have to modify the script here to accept the new project structure?

EDIT1

Before the structure that is generated looks like the following

 tmp myapp myapp myapp_mac myapp_win64.exe myapp.tar.gz 

Having tried the script in Charles Duffy answer, I got the following

 tmp myapp myapp myapp_mac myapp_win64.exe 

The tar file is missing, any idea?

+3
source share
3 answers

To change the script, add the line:

 cd src/server || exit 

... before any action that should take care of the source. In this way:

 #!/bin/sh cd src/server || exit # <--- ADD THIS LINE go get ./... || exit rm -rf /tmp/myapp # aside: using hardcoded names in /tmp is a Really Bad Idea. rm -f /tmp/myapp.tar.gz mkdir -p /tmp/myapp go build -o /tmp/myapp/myapp_mac || exit GOOS=windows GOARH=amd64 go build -o /tmp/myapp/myapp_win64.exe || exit GOOS=linux GOARCH=amd64 go build -o /tmp/myapp/myapp || exit cp -R ./resources /tmp/myapp/ || exit cd /tmp || exit tar -czf /tmp/myapp.tar.gz myapp 

However, I would suggest writing the following instead:

 #!/bin/bash basedir=$(cd -- "${BASH_SOURCE%/*}" && pwd) || exit rm -rf -- "$basedir/build" || exit mkdir -p -- "$basedir/"{build,dist} || exit build() (cd src/server && GOOS=$1 GOARCH=$2 exec go build -o "$basedir/build/$3") build darwin amd64 myapp_mac || exit build linux amd64 myapp || exit build windows amd64 myapp_win64 || exit if [[ -d "$basedir/resources" ]]; then cp -PR -- "$basedir/resources/." "$basedir/build/resources" || exit fi tar -czf "$basedir/dist/myapp.tar.gz" -C "$basedir/build" . 

Note:

  • We are working on the location of the script, so it works even when launched from another directory (you can run ./myapp/build and it will set builddir to the path to ./myapp ).
  • We do not use /tmp (if we want to do this safely, then we will have something like builddir=$(mktemp -t -d myapp-build.XXXXXX) and then rm -rf "$builddir" at the end).
  • We use shebang #!/bin/bash , which allows extensions such as [[ ]] and $BASH_SOURCE , instead of #!/bin/sh .
  • Repeated operations are encapsulated in a function.
  • We always die from errors that we do not expect (which includes the inability to copy resources if resources do not exist), but do not die from errors that we expect (for example, when copying resources because it does not exist).
  • The exit status of a script is always the exit status of the last command, so there is no need for explicit error handling.
+4
source

Your script runs go build without specifying the packages that it should compile. By default, the go tool will search for Go source files in the current directory. They are not there because you have moved them to another place.

Try adding a list of packages at the end of the go build . And note that package names must include the full path relative to the GOPATH directory.

See the documentation here .

+1
source

it should do it i believe

 #!/bin/sh cd src/server go get ./... rm -rf /tmp/myapp rm -f /tmp/myapp.tar.gz mkdir -p /tmp/myapp go build -o /tmp/myapp/myapp_mac env GOOS=windows GOARH=amd64 go build -o /tmp/myapp/myapp_win64.exe env GOOS=linux GOARCH=amd64 go build -o /tmp/myapp/myapp cd ../.. cp -R ./resources /tmp/myapp/ cd /tmp tar -czf myapp.tar.gz myapp/ 
+1
source

Source: https://habr.com/ru/post/1273612/


All Articles