package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/ceph/go-ceph/rados"
)



func makeMonQuery(cephConn *cephConnection, query map[string]string) []byte {
	monJson, err := json.Marshal(query)
	if err != nil {
		log.Fatalf("Can't marshal json mon query. Error: %v", err)
	}

	monRawAnswer, _, err := cephConn.conn.MonCommand(monJson)
	if err != nil {
		log.Fatalf("Failed exec monCommand. Error: %v", err)
	}
	return monRawAnswer
}

func getCommands(cephConn *cephConnection) []byte {
	monRawAnswer := makeMonQuery(cephConn, map[string]string{"prefix": "get_command_descriptions"})
	return monRawAnswer

}

func CloseConn(cephConn *cephConnection) {
	fmt.Println("Closing connection to cluster")
	cephConn.conn.Shutdown()
}
func main() {
	var connect cephConnection
	var err error
	// var poolInfo Poolinfo
	var monAnswer interface{}

	fmt.Println("Creating connection object")
	connect.conn,err = rados.NewConn()
	if err != nil {
		log.Fatal("Cannot create Connection object\n")
	}
	connect.conn.ReadConfigFile("/etc/ceph/ceph.conf")
	connect.conn.Connect()
	defer CloseConn(&connect)

	// monAnswer = getCommands(&connect)

	err = json.Unmarshal(getCommands(&connect), &monAnswer)
	if err != nil {
		log.Fatalln("Cannot unmarshal")
	}

	prettyForm, err := json.MarshalIndent(monAnswer,""," ")
	if err != nil {
		log.Fatalln("Second unmarshallin not successful")
	}

	fmt.Println(string(prettyForm))
	// hum,_ := json.MarshalIndent(monAnswer, "", "  ")
	// fmt.Println(monAnswer)

}

