Running an app from Terminal with a path - go

If I can run an app (built in Go but not really necessary) like so from its own directory:
./some_app
How can I run it from a different directory?

You should just be able to add the path to the app you are trying to run to your command. For example if the app is stored in /Users/example/apps/some_app, then you can just directly run "/Users/example/apps/some_app". You can find which directory your app is sitting in (if you are currently in that directory) with the pwd command.

Say you are in the home directory, and the app is under ~/app/some_app then you can run it with app/some_app
If you want to be able to call it with only some_app, it has to be accessible in your $PATH variable.
In go, when you do go install ./... the executable will end up in $GOPATH/bin/ which is normally part of your path if you have a correct configuration.

Related

how to build go outside of source directory?

My go application directory structure is like this:
/app
go.mod
go.sum
main.go
When I build the app I usually cd into that directory and build.
cd app
go build
I wonder if I can build without cd in to app directory.
When I go go build /app, it prints go: go.mod file not found in current directory or any parent directory; see 'go help modules'.
See https://golang.org/ref/mod#commands-outside :
go build needs to be run from a module directory.
The simplest way is to cd into your module directory (cd /app) to run your go build command.
(there probably is some way to create a phony local go.mod file, and reference your /app module from there, but I wasn't able to devise a hack to do this)
Go now can change directory before build with the help of flag -C:
go build -C app
"The go subcommands now accept -C to change directory to before performing the command"
Source: https://tip.golang.org/doc/go1.20
(You don't need to run cd .. after this command. Shell stays in the same directory)
I'm pretty sure to build you will need to be in the app directory.
As a workaround if you are just wanting to be on the command line in a different directory and want to run the build with one command you can just chain the cd and go build commands together like this:
cd app; go build ; cd ..
This is the same amount of typing but could be in your command history just a couple presses of the up arrow away.
Note this is for bash or similar UNIX style shell. If using Windows cmd then I think it would be something like this (Not tested as I don’t have readily available access to a Windows machine right now):
cd app & go build & cd ..

How execute "rackup" from a remote path?

Running a rack service written in ruby, I need to execute "rackup" from an external path, without browsing into the code's directory previously.
classically:
cd /Volumes/Code/my_service; rackup
i'd like to:
rackup cd /Volumes/Code/my_service
which obv does not work because rackup doesnt not take this parameter.
You can specify the config file to use as an argument to rackup. If you don’t include one it will default to config.ru and will just look in the current directory.
In your case you should be able to run:
rackup /Volumes/Code/my_service/config.ru
You will need to make sure your app doesn’t assume that the current directory is the same as the apps directory, apart from that everything should work okay.

Location of db/config.yml on a ruby app using standalone-migrations

I'm working on an command-line application that uses the standalone_migrations gem. I have the db/config.yml file and everything works fine when I run the app from the root dir, but when I run it from other dirs (e.g. directly running a script in the /bin directory), the gem cannot find the db/config.yml.
I looked at the gem's source, specifically in the lib/configurator.rb file, but couldn't find a way to set the correct .yml path.
Any help? Thanks.
StandaloneMigrations::Configurator uses relative paths, it loads configuration files on line #23.
This is a bug in the code. A workaround could be to change the working directory to the root before you execute the script. You didn't mention what kind of script you have under /bin, but for example if it is a Bash script, you can do something like:
cd /project/directory && rake db:migrate ...

Trying to access the "current dir" in a packaged Shoes app

I need to build an app which reads a file which is external to the Shoes package I'll be distributing it in.
In my app, Dir.pwd points to the temp dir (at least in Windows) where the script is unpacked to be ran. I've been trying to get the directory where the exe is running from, that is, the package I'm distributing.
The app needs to read a file which is distributed besides this package and then write another one in the same directory.
It seems that the Shoes runtime does not set any env variable with this directory either. Any pointers?
nachokb
I run into this problem a while ago and hadn't file a straight solution. I ended up with two workarounds:
At application installation time, put the files at a directory inside of the user home dir ~/ (the home indirection also works in windows)
Package the files together with the shoes generated executable so that it is unzip at the same place as the app and therefore accessible with Dir.pwd
If you find another way, please point it out.
Have you tried something like the following:
DIR = File.expand_path __FILE__
__FILE__ is a reference to the current file and File.expand_path gives you the full system path. Works in my shoes apps :-)
Shoes on!
Tobi

How execute a self contained rake build?

We have an application which is compiled using Rake (on windows). We have a new requirement that one of our clients needs to compile the source code in their own environment using a bat file.
So I need to find a way to execute a rake build without installing anything on the host environment (i.e. everything required to do the build needs to be in the source directory, ruby, gems, etc...) from a batch file.
Anyone have any clues how I could get started with this?
Download and install ruby to a folder inside your project (do not add it to your PATH). After go to this folder and delete any "uninstall" file. Go to the folder again with the console (cmd and then use cd path\to\ruby\folder) and run gem install ... to install everything you need. After add a .bat file to run your app. Something like:
#echo off
rubyfolder\bin\ruby.exe myscript.rb
This is a fully portable ruby installation, you can put it in any computer and it will work as well. (I use it as a portable ruby in my pendrive to let me play everywhere with ruby!)
PS.: rake is a script from bin, you can open it with:
rubyfolder\bin\ruby.exe rubyfolder\bin\rake

Resources