2

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)

https://github.com/golang/dep

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 ?

  • What did you expect? Your Cli-App doesn't require any flags or options so it'll just run the `Action`. – tkausl Dec 16 '17 at 17:44
  • if i m correct, you are confused about binary name, and its availability in PATH and the command line argument, sub command. In your current work presented here, you have a binary named greet available in the path. https://github.com/urfave/cli#subcommands –  Dec 16 '17 at 17:55
  • I think what you're looking for is a [Subcommand](https://github.com/urfave/cli#subcommands) – tkausl Dec 16 '17 at 17:56
  • you cant do 'interactive' with this package. To do interactive CLI this package is more suitable https://github.com/nsf/termbox-go. –  Dec 16 '17 at 18:26
  • you really need to read the doc. friendly speaking RTFM. to help you a bit more, run `go build`, the resulting binary is self contained, ready to copy paste. If you need to give it to someone with a different OS than yours, `GOOS=YYY go build`, give him the resulting binary. –  Dec 16 '17 at 18:43
  • the short answer is to add the directory containing the binary to the PATH of user, of put hte binary into PATH of user. Longer answer might be to generate an install-able package that does this for him. search about PATH at first before going forward into packaging, imho. –  Dec 16 '17 at 19:02
  • possible duplicate of https://stackoverflow.com/questions/35827147/cobra-viper-golang-how-to-test-subcommands – nkprince007 Dec 17 '17 at 02:03
  • @mh-cbon - I've update my question with the full code.. –  Dec 24 '17 at 20:03
  • @tkausl - I've update my question with the full code.. –  Dec 24 '17 at 20:04
  • @nkprince007 - I've update my question with the full code..duplicate this is not :) –  Dec 24 '17 at 20:04
  • @Ninawatcher Have you tried my solution? I've tested it both in Ubuntu and windows. Worked perfectly. Let me know if you are getting errors with that. – Anuruddha Dec 30 '17 at 01:54

4 Answers4

5

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.

  1. Users who have setup a working golang environment

    • Make your package go gettable.

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

  1. 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.

Anuruddha
  • 3,187
  • 5
  • 31
  • 47
  • Thanks 1+, It will be great if you can `clone` the project and make it `productive` , I've provided a 100 bounty points because I'm currently stuck, the project is just an example to my bigger project (and the git repo is fairly small) , and this will help to learn how to do it better since in fairly new to golang... –  Jan 01 '18 at 09:49
  • Sure take your time. please pay attention also to the 'make.sh' . thanks! –  Jan 01 '18 at 10:10
  • Hi, any news :-) ? –  Jan 03 '18 at 06:58
  • @Ninawatcher submit the changes yesterday. Please check the github issues section. – Anuruddha Jan 03 '18 at 06:59
2

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

nkprince007
  • 153
  • 10
1

To the question

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.

To the programming aspect of your question

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.

1

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

Community
  • 1
  • 1
Qinsi
  • 780
  • 9
  • 15