@@ -2,13 +2,14 @@ package client
22
33import (
44 "fmt"
5- "github.com/pkg/errors "
5+ "strings "
66
7+ "github.com/pkg/errors"
78 channels "github.com/replicatedhq/replicated/gen/go/v1"
89 "github.com/replicatedhq/replicated/pkg/types"
910)
1011
11- func (c * Client ) ListChannels (appID string , appType string , appSlug string ) ([]types.Channel , error ) {
12+ func (c * Client ) ListChannels (appID string , appType string , appSlug string , channelName string ) ([]types.Channel , error ) {
1213
1314 if appType == "platform" {
1415 platformChannels , err := c .PlatformClient .ListChannels (appID )
@@ -33,7 +34,7 @@ func (c *Client) ListChannels(appID string, appType string, appSlug string) ([]t
3334 } else if appType == "ship" {
3435 return c .ShipClient .ListChannels (appID )
3536 } else if appType == "kots" {
36- return c .KotsHTTPClient .ListChannels (appID , appSlug )
37+ return c .KotsHTTPClient .ListChannels (appID , appSlug , channelName )
3738 }
3839
3940 return nil , errors .New ("unknown app type" )
@@ -71,7 +72,7 @@ func (c *Client) CreateChannel(appID string, appType string, appSlug string, nam
7172 if err := c .PlatformClient .CreateChannel (appID , name , description ); err != nil {
7273 return nil , err
7374 }
74- return c .ListChannels (appID , appType , appSlug )
75+ return c .ListChannels (appID , appType , appSlug , name )
7576 } else if appType == "ship" {
7677 if _ , err := c .ShipClient .CreateChannel (appID , name , description ); err != nil {
7778 return nil , err
@@ -81,32 +82,47 @@ func (c *Client) CreateChannel(appID string, appType string, appSlug string, nam
8182 if _ , err := c .KotsClient .CreateChannel (appID , name , description ); err != nil {
8283 return nil , err
8384 }
84- return c .KotsHTTPClient .ListChannels (appID , appSlug )
85+ return c .KotsHTTPClient .ListChannels (appID , appSlug , name )
8586 }
8687
8788 return nil , errors .New ("unknown app type" )
8889}
8990
90- func (c * Client ) GetOrCreateChannelByName (appID string , appType string , appSlug string , name string , description string , createIfAbsent bool ) (* types.Channel , error ) {
91- allChannels , err := c .ListChannels (appID , appType , appSlug )
91+ func (c * Client ) GetOrCreateChannelByName (appID string , appType string , appSlug string , nameOrID string , description string , createIfAbsent bool ) (* types.Channel , error ) {
92+
93+ gqlNotFoundErr := fmt .Sprintf ("channel %s not found" , nameOrID )
94+ channel , _ , err := c .GetChannel (appID , appType , nameOrID )
95+ if err == nil {
96+ return & types.Channel {
97+ ID : channel .Id ,
98+ Name : channel .Name ,
99+ Description : channel .Description ,
100+ ReleaseSequence : channel .ReleaseSequence ,
101+ ReleaseLabel : channel .ReleaseLabel ,
102+ }, nil
103+ } else if ! strings .Contains (err .Error (), gqlNotFoundErr ) {
104+ return nil , errors .Wrap (err , "get channel" )
105+ }
106+
107+ allChannels , err := c .ListChannels (appID , appType , appSlug , nameOrID )
92108 if err != nil {
93109 return nil , err
94110 }
95111
96- foundChannel , numMatching , err := c .findChannel (allChannels , name )
112+ foundChannel , numMatching , err := c .findChannel (allChannels , nameOrID )
97113
98114 if numMatching == 0 && createIfAbsent {
99- updatedListOfChannels , err := c .CreateChannel (appID , appType , appSlug , name , description )
115+ updatedListOfChannels , err := c .CreateChannel (appID , appType , appSlug , nameOrID , description )
100116 if err != nil {
101- return nil , errors .Wrapf (err , "create channel %q " , name )
117+ return nil , errors .Wrapf (err , "create channel %q " , nameOrID )
102118 }
103119 // for some reason CreateChannel returns the list of all channels,
104120 // so now we gotta go find the channel we just created
105- channel , _ , err := c .findChannel (updatedListOfChannels , name )
106- return channel , errors .Wrapf (err , "find channel %q" , name )
121+ channel , _ , err := c .findChannel (updatedListOfChannels , nameOrID )
122+ return channel , errors .Wrapf (err , "find channel %q" , nameOrID )
107123 }
108124
109- return foundChannel , errors .Wrapf (err , "find channel %q" , name )
125+ return foundChannel , errors .Wrapf (err , "find channel %q" , nameOrID )
110126}
111127
112128func (c * Client ) GetChannelByName (appID string , appType string , appSlug string , name string ) (* types.Channel , error ) {
0 commit comments