@@ -2,9 +2,9 @@ package client
22
33import (
44 "fmt"
5+ "strings"
56
67 "github.com/pkg/errors"
7-
88 channels "github.com/replicatedhq/replicated/gen/go/v1"
99 "github.com/replicatedhq/replicated/pkg/types"
1010)
@@ -88,26 +88,41 @@ func (c *Client) CreateChannel(appID string, appType string, appSlug string, nam
8888 return nil , errors .New ("unknown app type" )
8989}
9090
91- func (c * Client ) GetOrCreateChannelByName (appID string , appType string , appSlug string , name string , description string , createIfAbsent bool ) (* types.Channel , error ) {
92- allChannels , err := c .ListChannels (appID , appType , appSlug , name )
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 )
93108 if err != nil {
94109 return nil , err
95110 }
96111
97- foundChannel , numMatching , err := c .findChannel (allChannels , name )
112+ foundChannel , numMatching , err := c .findChannel (allChannels , nameOrID )
98113
99114 if numMatching == 0 && createIfAbsent {
100- updatedListOfChannels , err := c .CreateChannel (appID , appType , appSlug , name , description )
115+ updatedListOfChannels , err := c .CreateChannel (appID , appType , appSlug , nameOrID , description )
101116 if err != nil {
102- return nil , errors .Wrapf (err , "create channel %q " , name )
117+ return nil , errors .Wrapf (err , "create channel %q " , nameOrID )
103118 }
104119 // for some reason CreateChannel returns the list of all channels,
105120 // so now we gotta go find the channel we just created
106- channel , _ , err := c .findChannel (updatedListOfChannels , name )
107- 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 )
108123 }
109124
110- return foundChannel , errors .Wrapf (err , "find channel %q" , name )
125+ return foundChannel , errors .Wrapf (err , "find channel %q" , nameOrID )
111126}
112127
113128func (c * Client ) GetChannelByName (appID string , appType string , appSlug string , name string ) (* types.Channel , error ) {
0 commit comments