2019-07-21 02:00:04 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"text/template"
|
2019-08-02 16:03:43 +00:00
|
|
|
|
2019-08-10 20:51:36 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
|
2019-08-02 16:03:43 +00:00
|
|
|
"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",
|
2019-08-10 21:19:37 +00:00
|
|
|
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) {
|
2019-08-02 16:03:43 +00:00
|
|
|
client, err := common.GetClient()
|
2019-08-03 13:51:11 +00:00
|
|
|
common.CheckError(err)
|
2019-07-21 02:00:04 +00:00
|
|
|
|
2019-08-02 17:10:59 +00:00
|
|
|
respBody, err := client.GetStatus()
|
2019-08-03 13:51:11 +00:00
|
|
|
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"))
|
2019-08-03 13:51:11 +00:00
|
|
|
common.CheckError(templateParsingErr)
|
2019-07-21 02:00:04 +00:00
|
|
|
templateExecutionErr := template.Execute(os.Stdout, respBody)
|
2019-08-03 13:51:11 +00:00
|
|
|
common.CheckError(templateExecutionErr)
|
2019-07-21 02:00:04 +00:00
|
|
|
fmt.Println()
|
|
|
|
} else {
|
|
|
|
// Print status fields as a table
|
2019-08-06 04:04:10 +00:00
|
|
|
writer, newTabWriterErr := common.NewTabWriter([]string{
|
2019-07-21 02:00:04 +00:00
|
|
|
"VERSION",
|
|
|
|
"AUTHENTICATION",
|
|
|
|
"ENDPOINT MANAGEMENT",
|
|
|
|
"ANALYTICS",
|
|
|
|
})
|
2019-08-03 13:51:11 +00:00
|
|
|
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,
|
|
|
|
))
|
2019-08-03 13:51:11 +00:00
|
|
|
common.CheckError(printingErr)
|
2019-07-21 02:00:04 +00:00
|
|
|
|
|
|
|
flushErr := writer.Flush()
|
2019-08-03 13:51:11 +00:00
|
|
|
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"))
|
2019-08-10 20:51:36 +00:00
|
|
|
|
|
|
|
statusCmd.SetUsageTemplate(statusCmd.UsageTemplate() + common.GetFormatHelp(portainer.Status{}))
|
2019-07-21 02:00:04 +00:00
|
|
|
}
|