Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide Go cmd app as productive app

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 ?


2 Answers

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.

like image 119
Anuruddha Avatar answered Jan 23 '26 14:01

Anuruddha


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

like image 25
nkprince007 Avatar answered Jan 23 '26 12:01

nkprince007