psu/cmd/status.go

71 lines
1.8 KiB
Go
Raw Permalink Normal View History

2019-07-21 02:00:04 +00:00
package cmd
import (
"fmt"
"os"
"text/template"
portainer "github.com/portainer/portainer/api"
"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",
Example: ` Print status in a table format:
psu status
Print version of Portainer server:
psu status --format "{{ .Version }}"`,
2019-07-21 02:00:04 +00:00
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)
2019-08-06 22:51:56 +00:00
statusCmd.Flags().String("format", "", "Format output using a Go template.")
2019-07-21 02:00:04 +00:00
viper.BindPFlag("status.format", statusCmd.Flags().Lookup("format"))
statusCmd.SetUsageTemplate(statusCmd.UsageTemplate() + common.GetFormatHelp(portainer.Status{}))
2019-07-21 02:00:04 +00:00
}