脚本无法更改项目结构

I have the following file make.sh which 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 is working!

#!/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 run the ‘make.sh’ it 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 the make.sh inside the server folder as is and it create the tar but its not valid…any idea what should I change the script here to adopt to the new project structure?

EDIT1

Before the structure which is generated is like following

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

After trying the script in the answer of Charles Duffy I got the following

tmp 
  myapp
    myapp
    myapp_mac
    myapp_win64.exe

The tar file is missing, any idea ?

To change your script, add the line:

cd src/server || exit

...before any action which needs to care about the source. Thus:

#!/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

That said, I would suggest instead writing this as:

#!/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're operating relative to the script's location, so it works even if run from a different directory (you can run ./myapp/build, and it'll set builddir to the path to ./myapp).
  • We're not using /tmp (if we wanted to do so safely, then we'd have something like builddir=$(mktemp -t -d myapp-build.XXXXXX), and then rm -rf "$builddir" at the end).
  • We're using a #!/bin/bash shebang, which permits extensions such as [[ ]] and $BASH_SOURCE, instead of #!/bin/sh.
  • Repeated operations are encapsulated in a function.
  • We always die on errors we don't expect (which includes failing to copy resources if resources doesn't exist), but don't die on errors we do expect (such as filing to copy resources because it doesn't exist).
  • The exit status of the script is always the exit status of the last command, so there's no need for explicit error handling there.

Your script executes go build without specifying the packages it needs to compile. By default the go tool will look for Go source files in the current directory. There are no, since you have moved them elsewhere.

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

See the documentation here.

this 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/