github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/get/get_chat.go (about) 1 package get 2 3 import ( 4 "strings" 5 6 "github.com/olli-ai/jx/v2/pkg/cmd/helper" 7 8 "github.com/jenkins-x/jx-logging/pkg/log" 9 "github.com/olli-ai/jx/v2/pkg/chats" 10 "github.com/olli-ai/jx/v2/pkg/util" 11 "github.com/spf13/cobra" 12 13 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 14 "github.com/olli-ai/jx/v2/pkg/cmd/templates" 15 ) 16 17 // GetChatOptions the command line options 18 type GetChatOptions struct { 19 Options 20 21 Kind string 22 Dir string 23 } 24 25 var ( 26 getChatLong = templates.LongDesc(` 27 Display the chat server URLs. 28 29 `) 30 31 getChatExample = templates.Examples(` 32 # List all registered chat server URLs 33 jx get chat 34 `) 35 ) 36 37 // NewCmdGetChat creates the command 38 func NewCmdGetChat(commonOpts *opts.CommonOptions) *cobra.Command { 39 options := &GetChatOptions{ 40 Options: Options{ 41 CommonOptions: commonOpts, 42 }, 43 } 44 45 cmd := &cobra.Command{ 46 Use: "chat [flags]", 47 Short: "Display the current registered chat service URLs", 48 Long: getChatLong, 49 Example: getChatExample, 50 Aliases: []string{"slack"}, 51 Run: func(cmd *cobra.Command, args []string) { 52 options.Cmd = cmd 53 options.Args = args 54 err := options.Run() 55 helper.CheckErr(err) 56 }, 57 } 58 cmd.Flags().StringVarP(&options.Kind, "kind", "k", "", "Filters the chats by the kinds: "+strings.Join(chats.ChatKinds, ", ")) 59 return cmd 60 } 61 62 // Run implements this command 63 func (o *GetChatOptions) Run() error { 64 authConfigSvc, err := o.CreateChatAuthConfigService(o.Kind) 65 if err != nil { 66 return err 67 } 68 config := authConfigSvc.Config() 69 70 if len(config.Servers) == 0 { 71 log.Logger().Infof("No chat servers registered. To register a new chat servers use: %s", util.ColorInfo("jx create chat server")) 72 return nil 73 } 74 filterKind := o.Kind 75 76 table := o.CreateTable() 77 if filterKind == "" { 78 table.AddRow("Name", "Kind", "URL") 79 } else { 80 table.AddRow(strings.ToUpper(filterKind), "URL") 81 } 82 83 for _, s := range config.Servers { 84 kind := s.Kind 85 if filterKind == "" || filterKind == kind { 86 table.AddRow(s.Name, kind, s.URL) 87 } else if filterKind == kind { 88 table.AddRow(s.Name, s.URL) 89 } 90 } 91 table.Render() 92 return nil 93 }