@@ -2,13 +2,14 @@ package client
2
2
3
3
import (
4
4
"fmt"
5
- "github.com/pkg/errors "
5
+ "strings "
6
6
7
+ "github.com/pkg/errors"
7
8
channels "github.com/replicatedhq/replicated/gen/go/v1"
8
9
"github.com/replicatedhq/replicated/pkg/types"
9
10
)
10
11
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 ) {
12
13
13
14
if appType == "platform" {
14
15
platformChannels , err := c .PlatformClient .ListChannels (appID )
@@ -33,7 +34,7 @@ func (c *Client) ListChannels(appID string, appType string, appSlug string) ([]t
33
34
} else if appType == "ship" {
34
35
return c .ShipClient .ListChannels (appID )
35
36
} else if appType == "kots" {
36
- return c .KotsHTTPClient .ListChannels (appID , appSlug )
37
+ return c .KotsHTTPClient .ListChannels (appID , appSlug , channelName )
37
38
}
38
39
39
40
return nil , errors .New ("unknown app type" )
@@ -71,7 +72,7 @@ func (c *Client) CreateChannel(appID string, appType string, appSlug string, nam
71
72
if err := c .PlatformClient .CreateChannel (appID , name , description ); err != nil {
72
73
return nil , err
73
74
}
74
- return c .ListChannels (appID , appType , appSlug )
75
+ return c .ListChannels (appID , appType , appSlug , name )
75
76
} else if appType == "ship" {
76
77
if _ , err := c .ShipClient .CreateChannel (appID , name , description ); err != nil {
77
78
return nil , err
@@ -81,32 +82,47 @@ func (c *Client) CreateChannel(appID string, appType string, appSlug string, nam
81
82
if _ , err := c .KotsClient .CreateChannel (appID , name , description ); err != nil {
82
83
return nil , err
83
84
}
84
- return c .KotsHTTPClient .ListChannels (appID , appSlug )
85
+ return c .KotsHTTPClient .ListChannels (appID , appSlug , name )
85
86
}
86
87
87
88
return nil , errors .New ("unknown app type" )
88
89
}
89
90
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 )
92
108
if err != nil {
93
109
return nil , err
94
110
}
95
111
96
- foundChannel , numMatching , err := c .findChannel (allChannels , name )
112
+ foundChannel , numMatching , err := c .findChannel (allChannels , nameOrID )
97
113
98
114
if numMatching == 0 && createIfAbsent {
99
- updatedListOfChannels , err := c .CreateChannel (appID , appType , appSlug , name , description )
115
+ updatedListOfChannels , err := c .CreateChannel (appID , appType , appSlug , nameOrID , description )
100
116
if err != nil {
101
- return nil , errors .Wrapf (err , "create channel %q " , name )
117
+ return nil , errors .Wrapf (err , "create channel %q " , nameOrID )
102
118
}
103
119
// for some reason CreateChannel returns the list of all channels,
104
120
// 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 )
107
123
}
108
124
109
- return foundChannel , errors .Wrapf (err , "find channel %q" , name )
125
+ return foundChannel , errors .Wrapf (err , "find channel %q" , nameOrID )
110
126
}
111
127
112
128
func (c * Client ) GetChannelByName (appID string , appType string , appSlug string , name string ) (* types.Channel , error ) {
0 commit comments