Reputation:
I’m creating the following repo which use Go command line tools cobra
to generate some artifacts , while run the command
go run main.go mzr convert toJSON ./
the program take the yaml file and
generate from it json file.
Now I want that my repo will Behave like the following command lines tool (user can install it and run help and use the tool supported commands)
That the user will be able to install mzr
and will be able to run the
command's inside like in the dep
repository when user run dep init
,
In my case user should run
mzr convert toJSON path/to/yaml/
This is my repository
https://github.com/NinaWatcher/mzr
I’ve the file.yaml
inside the root (and the output json for testing only) but the user should provide the path to the file.
The logic is inside the file: cmd/commands/convert.go
I try to do it with creating make.sh file (see the results in build folder) which create executable files for several OS but when I take the files and try to run it on mac and windows its not working either, what should I do here ?
Upvotes: 2
Views: 713
Reputation: 3245
I went through the code base and identified the following should change in-order for this tool to be platform independent.
Users should execute the following commanad as you've mentioned
mzr convert toJSON path/to/yaml/
path/to/yaml should be OS independent and it must be the absolute/relative path to the file. For this you can do the following change to convert function. And remove the github.com/mitchellh/go-homedir
dependency from converter.go
func convert(args []string) {
//read file
// TODO: handle number of arguments and output proper messages.
yamlFile, err := ioutil.ReadFile(args[0])
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
//Format the yaml to json
fmt.Println("Start converting: " + strings.Join(args, " "))
jsonOut, err := YAML.YAMLToJSON(yamlFile) // TODO: handle error
fmt.Println(string(jsonOut))
err = ioutil.WriteFile("jsonFile.json", jsonOut, 0644) // TODO: handle error
}
There are 2 ways to use this tool.
Users who have setup a working golang environment
Change for the main.go file(in git diff notation).
package main
-import "mzr/cmd/commands"
+import "github.com/NinaWatcher/mzr/cmd/commands"
Once you do this users can install the tool with the following command smilar to how godep
works
go get github.com/NinaWatcher/mzr
Users who don't have golang installed.
Build OS specific distributions as you are doing with the make.sh file
Hope this solved your tool distribution issue. On a side note though, there is lot that can be improved in the code base specially error handling, package hierarchy etc..Better to pay attention to them before distributing this to the users.
Upvotes: 5
Reputation: 820
Hope this can help:
Create a simple shell script named mzr:
#!/bin/bash -
exec go run main.go mzr "$@"
Fit your own path, and have luck ;D
Upvotes: 1
Reputation:
I try to do it with creating make.sh file (see the results in build folder) which create executable files for several OS but when I take the files and try to run it on mac and windows its not working either, what should I do here ?
I better have to redirect you to already provided answer than to try to repeat theirs.
https://superuser.com/questions/517894/what-is-the-unix-path-variable-and-how-do-i-add-to-it
https://en.wikipedia.org/wiki/PATH_(variable)
tldr: when you copy the executable on the target machines, the directory containing that binary does not exist within the PATH variable, thus, when you call for it, the OS is not capable to locate the file on the file system.
i took a bit of time to rewrite the program in, what i believe to be, a more appropriate way.
Please check https://github.com/mh-cbon/demotest/tree/master/help
The caveat around your implementation goal (take any yaml input and convert it to json) is that go is not well suited to do that easily.
As it is a strongly typed language it is way more easier to provide a predefined data structure.
That being said, i don t doubt a solution exist to convert any input to json, it is just more complex and out of the scope, imho.
Upvotes: 1
Reputation: 163
The problem may be because you are building for all three platforms on one platform.
Go build can only build for the current platform. i.e. the platform on which the build is running.
You can however use goreleaser to automate builds for platforms independently.
Reference: https://goreleaser.com/#quick_start
Upvotes: 2