~hristoast/obpmg

4089e0e2f94e626e4778437d4ba0649db89e5a87 — Hristos N. Triantafillou 4 years ago c97de7d
Sort output by default, make the nosort flag work as expected
1 files changed, 14 insertions(+), 11 deletions(-)

M obpmg.go
M obpmg.go => obpmg.go +14 -11
@@ 5,6 5,7 @@ import (
	"io/ioutil"
	"log"
	"os"
	"sort"
	"time"

	"github.com/urfave/cli/v2"


@@ 37,7 38,7 @@ func cleanYamlData() {
	//
}

func getYamlData(filePath string) ([]yamlData, interface{}) {
func getYamlData(filePath string, nosort bool) ([]yamlData, interface{}) {
	file, err := ioutil.ReadFile(filePath)
	if err != nil {
		log.Fatal(err)


@@ 54,6 55,7 @@ func getYamlData(filePath string) ([]yamlData, interface{}) {
}

func cliApp() *cli.App {
	var nosort bool
	var yamlFile string

	app := &cli.App{


@@ 73,28 75,29 @@ func cliApp() *cli.App {
				Required:    true,
			},
			&cli.BoolFlag{
				Name:    "nosort",
				Usage:   "Don't sort menu items by label; retain the order of the given yaml file.",
				Aliases: []string{"n"},
				EnvVars: []string{"OBPMG_NOSORT"},
				Name:        "nosort",
				Usage:       "Don't sort menu items by label; retain the order of the given yaml file.",
				Destination: &nosort,
				Aliases:     []string{"n"},
				EnvVars:     []string{"OBPMG_NOSORT"},
			},
		},
		Action: func(c *cli.Context) error {
			// cli.ShowVersion(c)
			if c.Bool("nosort") {
				//TODO: don't sort output
			}

			c.App.Setup()

			yamlFile := c.String("yaml-file")

			//TODO: validate the yaml data
			y, err := getYamlData(yamlFile)
			y, err := getYamlData(yamlFile, nosort)
			if err != nil {
				log.Fatal(err)
			}

			if nosort == false {
				// No special condition is needed, so just return true and sort everything.
				sort.SliceStable(y, func(i, j int) bool { return true })
			}

			x := &xmlOpenboxPipeMenu{}

			for _, data := range y {