psu/cmd/status.go

62 lines
1.5 KiB
Go
Raw Normal View History

2019-07-21 02:00:04 +00:00
package cmd
import (
"fmt"
"os"
"text/template"
"github.com/greenled/portainer-stack-utils/common"
"github.com/spf13/cobra"
"github.com/spf13/viper"
2019-07-21 02:00:04 +00:00
)
// statusCmd represents the status command
var statusCmd = &cobra.Command{
Use: "status",
Short: "Check Portainer status",
Run: func(cmd *cobra.Command, args []string) {
client, err := common.GetClient()
common.CheckError(err)
2019-07-21 02:00:04 +00:00
respBody, err := client.GetStatus()
common.CheckError(err)
2019-07-21 02:00:04 +00:00
if viper.GetString("status.format") != "" {
// Print stack fields formatted
template, templateParsingErr := template.New("statusTpl").Parse(viper.GetString("status.format"))
common.CheckError(templateParsingErr)
2019-07-21 02:00:04 +00:00
templateExecutionErr := template.Execute(os.Stdout, respBody)
common.CheckError(templateExecutionErr)
2019-07-21 02:00:04 +00:00
fmt.Println()
} else {
// Print status fields as a table
writer, newTabWriterErr := common.NewTabWriter([]string{
2019-07-21 02:00:04 +00:00
"VERSION",
"AUTHENTICATION",
"ENDPOINT MANAGEMENT",
"ANALYTICS",
})
common.CheckError(newTabWriterErr)
2019-07-21 02:00:04 +00:00
_, printingErr := fmt.Fprintln(writer, fmt.Sprintf(
"%s\t%v\t%v\t%v",
respBody.Version,
respBody.Authentication,
respBody.EndpointManagement,
respBody.Analytics,
))
common.CheckError(printingErr)
2019-07-21 02:00:04 +00:00
flushErr := writer.Flush()
common.CheckError(flushErr)
2019-07-21 02:00:04 +00:00
}
},
}
func init() {
rootCmd.AddCommand(statusCmd)
statusCmd.Flags().String("format", "", "format output using a Go template")
viper.BindPFlag("status.format", statusCmd.Flags().Lookup("format"))
}