psu/cmd/status.go

78 lines
2.0 KiB
Go
Raw Normal View History

2019-07-21 02:00:04 +00:00
package cmd
import (
2019-08-14 05:55:39 +00:00
"encoding/json"
2019-07-21 02:00:04 +00:00
"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
2019-08-14 05:55:39 +00:00
switch viper.GetString("status.format") {
case "table":
// Print status in a table format
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-08-14 05:55:39 +00:00
case "json":
// Print status in a json format
statusJsonBytes, err := json.Marshal(respBody)
common.CheckError(err)
fmt.Println(string(statusJsonBytes))
default:
// Print status in a custom format
template, templateParsingErr := template.New("statusTpl").Parse(viper.GetString("status.format"))
common.CheckError(templateParsingErr)
templateExecutionErr := template.Execute(os.Stdout, respBody)
common.CheckError(templateExecutionErr)
fmt.Println()
2019-07-21 02:00:04 +00:00
}
},
}
func init() {
rootCmd.AddCommand(statusCmd)
2019-08-14 05:55:39 +00:00
statusCmd.Flags().String("format", "table", `Output format. Can be "table", "json" or 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
}