How to save configuration settings against a command line tool - objective-c

I've built a command line tool in Objective-C using Xcode 5.0.2. The target OS being OSX and Linux.
At the moment the command line tool takes an argument which is an API key. The API key will be unique for every user that installs the tool. It is obviously very tedious inputting the API key each time the tool is used, so i need to find a solution to this problem.
What I would like to do is allow the user the ability to save the API key against the tool somehow. Something like:
$ mytool config api.key 123456
I've not done this before, so i would like to know what options I have and what are the best practices for doing so.
My gut feeling was to have the tool write to a config file inside the user's home directory called .mytoolconfig and then have the application load this file if it exists, extract the API key and then know not to prompt for it.
Is this approach reasonably sensible or...?

yes. that's how many UNIX CLI save their configs: they write a . file in the home dir ...
if it were ONLY for osx I'd say .. go for ~/Library/Preferences or so but for *nix it is fine to do it like you proposed.
use HSHomeDirectory() and NSFileManager to achieve that
you could (with some modifications) use NSUserDefaults too but for a CLI I wouldn't
NOTE: personal preference maybe :D

Related

How can I bundle a command line utility in os x application on Mac App Store (using sandbox entitlement)

I have a c++ command line application that I have already compiled into an executable and have added it into my Xcode project. I have also added the "Copy Files" section to the Build Phases tab of the project properties and added my executable with the "Executables" destination. When I build my application I see it in the test.app/Contents/MacOS folder when I View package contents on the test.app that is built.
I also have App Sandbox enabled on the Capabilities tab of the project (so that I can distribute my application through the mac app store.
How can I expose this command line executable that is bundled with my application to the user so that they can run it from the command line (terminal)? I have not been able to find anything on search engines or on StackOverflow about how to get this file (or a symlink to this file) into the users PATH. I tried using an NSTask to create a symlink, but that only works if I disable the App Sandbox (which makes sense). Has anyone done this before? How did you get it to work? Or can these executables only be executed by code within your application?
I don't see a good way to do this. First, a clarification: the PATH is a list of directories that contain executables, not a list of executables; there's no way to add a single executable to the PATH. Instead, what you'd need to do is either put your executable into one of the directories in the user's PATH, or add the directory your executable is in into the PATH.
On OS X, the default PATH is /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin. The first 4 directories shouldn't be modified from the system default, so only /usr/local/bin is a possibility. But creating it (it doesn't exist by default) would require admin (actually root) rights, which isn't allowed by App Store policies. So that's out.
That leaves modifying the user's PATH. The "right" way to do that system-wide is by placing a file in /etc/paths.d, which requires admin (/root) rights, so that's out too. Technically modifying the /etc/paths file would work, but that has the same permissions problem plus it's the wrong way to do customization.
The next possibility is to modify (/create) the user's shell initialization script(s). This'll work, but doing it at all right is going to be messy, because there are several shells the user might use, each with several different possible initialization scripts that the user might or might not have created...
Let's take a very simple case: a user who only ever uses bash, and who doesn't already have any initialization scripts. When a "login" instance of bash starts, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile (in that order), and runs the first one it finds. But your app doesn't know which shell he uses, so you'd better create ~/.profile so zsh and ksh will use it as well. So, your app creates ~/.profile, and puts this in it:
PATH="$PATH:/Applications/MyApp.app/Contents/Helpers"
Great, right? Yup, great, until the user runs something else that wants to set their PATH, it creates ~/.bash_profile, and this overrides your setup. After that, your executable will be in the PATH of zsh and ksh, but not bash. Whee.
And then one day the user decides to use tcsh instead, and it (and csh) have a completely different but equally messy pile of possible init files...

How to access file with unacceptable file name

I don't know is this site a good place to ask this question... A long time ago, my operating system was linux. On linux I made a file with name \/:*?"<>|. Then I installed windows instead of linux, but now I cannot access or delete this file. I tried to delete it using Unlocker, ProceXP, Command Prompt and many other programs, but I couldn't. Also, I tried all commands in Command Prompt which can be used for deleting undeletable files, but this file is still here. If I try to rename it, process explorer.exe crashes. Then I installed linux again and this file become accessable.
Now I have windows and another file with name \/:*?"<>|. Is it possible to access this file without installing linux? Is there a way to access place on filesystem where this file name is stored and manualy change it to any acceptable file name? If yes, can you explain which program is best for it?
Try using DeleteDoctor. I've used it under similar situations as yours with great success. You can download a copy here:
http://www.download25.com/delete-doctor-download.html

Operating system agnostic way to get config file in clojure

I have a few clojure applications that load the sensitive info off of .properties file in /etc/ and this has worked well so far.
Recently, I have had to deal with a few windows machines added into our server collection and I need to run the clojure applications on there as well. Windows doesn't obviously have or understand /etc/ path and I got around that fact by looking at /etc/ and if that's missing then looking at d:\configs.
But I don't quite like this way of doing it, because, if there is another windows developer looking into it and he doesn't have d:\ or prefers elsewhere for configs it would get messy.
Is there any way I can load a file from clojure, no matter what operating system it is? My initial thoughts were of saving a key-path in the Environment variable and accessing it from clojure.
I am just wondering if there is a better way of doing it.
Thanks.
Have a look at environ. It offers some flexibility when it comes to configuring your Clojure app, letting you choose between a number of options:
environment variables: This seems to be the way to go in Clojureland, so I'd say your initial thought wasn't the worst;
in ~/.lein/profiles.clj: You can store them in the :user profile as Clojure data - that sounds quite nice, I guess;
Java CLI properties: Finally, you can pass them to the java executable directly via the command line.
environ will collect data from all these places.

Why can't I access to Rails via multiples shells bash and zsh on OSX Lion

I installed Ruby on Rails 3 using bash on OSX Lion.
When I wanted to use zsh and do a "rails -v" command I had "Rails is not currently installed on this system".
I just do not understand why. To me it's like "you can access all the files you want with bash not with zsh".
If I want to switch to zsh, will I have to install one more time Rails? delete the old one?
Is it secure and clever to use two shells when developing Rails applications?
What are your best practices?
Thanks in advance.
PS: I am new to programming and my installation is working fine. I even created one app.
EDIT: If you encounter the same problem I would encourage you to read the second answer first to get the rationale then the first one. Thanks to both of them.
While you are running bash, type at the command line echo $PATH.
Now start up your zsh command line, and type echo $PATH.
I bet the Rails path is not included in your zsh, right?
Look in your $HOME dir for .bash* files. See which one has the Rails PATH added.
Find your zsh .* env files and copy/paste the line you found above into the approriate file.
When I say .bash* and .* files, I mean hidden files in your home dir that you'll only find by doing ls -la. If you find a .profile file, that would be a good place to insert this.
I hope this helps.
P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.
I just do not understand why. To me it's like "you can access all the
files you want with bash not with zsh".
As shellter said, you must have directories added to your path in order to run commands from the command line. For more information on PATHs take a look at the Intro to Linux guide. I know you are not using Linux but that section (and lots of others) is very much applicable.
If I want to switch to zsh, will I have to install one more time
Rails? delete the old one?
No, see note on paths above and take a look at the guide.
Is it secure and clever to use two shells when developing Rails
applications? What are your best practices?
It's not really something special to use two shells at once. It depends what you do in each. I often have 6 and 7 shells open, not all for the same thing. That is really up to you and how you work. The number of shells you use is more of a workflow thing than a "best-practices" issue.
I normally have 3 shells open when I do RoR work. One running autotest in the background, one running rails -server, and one where I actually edit files and run emacs. But again, this is not best practice as it is workflow. Do whatever makes you effective.
The only trouble you could get into occasionally running two shells is say, for example, you open a file in one and delete it in another. However, when you do this it is typically very obvious.
PS: I am new to programming and my installation is working fine. I
even created one app.
For Rails, I found this tutorial very helpful.
Also, you said you were new to programming. I don't know if this is the case, but I would suggest starting with basic Ruby, before jumping into Perl. Although the tutorial I linked above covers Rails-flavored Ruby.

Build and Debug application outside the default package

If I try to build an application with the application class outside the default package, so the application file path is /app/AppClass.mxml instead of /AppClass.mxml (as would normally be the case), Flash builder cannot launch the application for debugging because it is looking for the SWF in debug/app/AppClass.swf and the SWF is being output to debug/AppClass.swf instead. Changing the output folder to debug/app makes it put the swf in debug/app, but then it puts the application configuration file "AppClass-app.xml" in /debug/app/app and then that can't be found.
Is there a way to change only the SWF output folder, or the location of the xml configuration file in the run-configuration?
You may use symbolic link to created swf file - http://en.wikipedia.org/wiki/Symbolic_link
for example for Windows :
cd project/path/bin-debug/package/path/
MKLINK ClassName.swf project/path/bin-debug/ClassName.swf
and it's work
or you can use symbolic link for folder:
cd project/path/bin-debug/package/
MKLINK path project/path/bin-debug/ /D
I think I remember this worked for me. But it was long time ago. And, yes, it is a known problem, I also recall Adobe people mentioning it as a limitation of FB.
In my Ant script, you'll need to do the adjustments to reflect your actual file names and directory structure. Also note that it will make it more cumbersome to debug it from FB. You'll need to use the debugging target in Ant, and then connect the debugger to the running application (so that some info, especially on the startup) will be lost. The only way you would be able to debug it, though I've never tried it, is with the commandline tools (I'm not sure of adl syntax for breakpoints / printing / stack frames, so idk how to do it.
Also, for the released application you will probably want to change the signing mechanism.