How to provide Go cmd application as a productive application

Im creating the following repo, which uses the Go cobra command-line tools to generate some artifacts, during the go run main.go mzr convert toJSON ./ program accepts the yaml file and generate a json file from it.

Now I want my repo to run as the next command line tool (the user can install it and run help and use the commands supported by the tool)

https://github.com/golang/dep

So that the user can install mzr and can run the command inside, as in the dep repository, when the user runs dep init , in my case the user must run

mzr convert toJSON path/to/yaml/

This is my store

https://github.com/NinaWatcher/mzr

Ive file.yaml inside the root (and json output for testing), but the user must specify the file path.

The logic is inside the file: cmd/commands/convert.go

I try to do this with the creation of a make.sh file (see results in the build folder) that create executable files for several OSs, but when I take the files and try to run it on o n mac and windows , it also does not work, what should I do here ?

+3
command-line bash shell go go-cobra
source share
4 answers

I went through the code base and determined the following to reorder so that this tool is platform independent.

Users must perform the following combination, as you mentioned

mzr convert toJSON path/to/yaml/

path / to / yaml must be OS independent and must be an absolute / relative path to the file. For this you can do the following function conversion. And remove the dependency github.com/mitchellh/go-homedir from convert.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 two ways to use this tool.

  • Users who set up the golang workspace

    • Make your package available.

Modify the main.go file (in git diff format).

  package main -import "mzr/cmd/commands" +import "github.com/NinaWatcher/mzr/cmd/commands" 

After that, users can install this tool with the following smilar command, how godep works

go get github.com/NinaWatcher/mzr

  1. Users who do not have golang are installed.

    Create custom OS distributions that you make with the make.sh file

Hope this solves the problem of distributing your tool. However, on the side of the note, there are many that can be improved in the code base, especially when handling errors, package hierarchies, etc. Delete them before distributing them to users.

+6
source share

The problem may be that you are building for all three platforms on the same platform.

Go build can only build for the current platform. that is, the platform on which the assembly is performed.

However, you can use goreleaser to automate builds for platforms independently.

Link: https://goreleaser.com/#quick_start

+3
source share

To the question

I try to do this with the creation of a make.sh file (see results in the assembly) that create executable files for several OSs, but when I take the files and try to run it on Mac and Windows and it doesn’t work, what should I do here?

I'd rather redirect you to an already provided answer than try to repeat them.

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)

TL; DR: when copying the executable file on target computers, the directory containing this binary does not exist in the PATH variable, so when it is called, the OS cannot find the file in the file system.

To the aspect of programming your question

It took me a little time to rewrite the program, which I think is more appropriate.

Please check https://github.com/mh-cbon/demotest/tree/master/help

The caveat around your implementation goal (accept any yaml input and convert it to json) is that go is not well suited for this.

As it is a strongly typed language, it is easier to provide a predefined data structure.

At the same time, I have no doubt that there is a solution for converting any input to json, it is simply more complex and goes beyond the scope, imho.

+1
source share

Hope this helps:

Create a simple shell script called mzr:

#! / bin / bash -

exec go run main.go mzr "$ @"

Set your own path and good luck, D

+1
source share

All Articles