@@ -2,9 +2,9 @@ package client
2
2
3
3
import (
4
4
"fmt"
5
+ "strings"
5
6
6
7
"github.com/pkg/errors"
7
-
8
8
channels "github.com/replicatedhq/replicated/gen/go/v1"
9
9
"github.com/replicatedhq/replicated/pkg/types"
10
10
)
@@ -88,26 +88,41 @@ func (c *Client) CreateChannel(appID string, appType string, appSlug string, nam
88
88
return nil , errors .New ("unknown app type" )
89
89
}
90
90
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 )
93
108
if err != nil {
94
109
return nil , err
95
110
}
96
111
97
- foundChannel , numMatching , err := c .findChannel (allChannels , name )
112
+ foundChannel , numMatching , err := c .findChannel (allChannels , nameOrID )
98
113
99
114
if numMatching == 0 && createIfAbsent {
100
- updatedListOfChannels , err := c .CreateChannel (appID , appType , appSlug , name , description )
115
+ updatedListOfChannels , err := c .CreateChannel (appID , appType , appSlug , nameOrID , description )
101
116
if err != nil {
102
- return nil , errors .Wrapf (err , "create channel %q " , name )
117
+ return nil , errors .Wrapf (err , "create channel %q " , nameOrID )
103
118
}
104
119
// for some reason CreateChannel returns the list of all channels,
105
120
// 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 )
108
123
}
109
124
110
- return foundChannel , errors .Wrapf (err , "find channel %q" , name )
125
+ return foundChannel , errors .Wrapf (err , "find channel %q" , nameOrID )
111
126
}
112
127
113
128
func (c * Client ) GetChannelByName (appID string , appType string , appSlug string , name string ) (* types.Channel , error ) {
0 commit comments