11package shipclient
22
33import (
4+ "fmt"
5+ "github.com/pkg/errors"
46 channels "github.com/replicatedhq/replicated/gen/go/v1"
57 "github.com/replicatedhq/replicated/pkg/graphql"
68 "github.com/replicatedhq/replicated/pkg/types"
@@ -16,6 +18,15 @@ type GraphQLResponseGetChannel struct {
1618 Errors []graphql.GQLError `json:"errors,omitempty"`
1719}
1820
21+ type GraphQLResponseCreateChannel struct {
22+ Data * ShipCreateChannelData `json:"data,omitempty"`
23+ Errors []graphql.GQLError `json:"errors,omitempty"`
24+ }
25+
26+ type ShipCreateChannelData struct {
27+ ShipChannel * ShipChannel `json:"createChannel"`
28+ }
29+
1930type ShipGetChannelData struct {
2031 ShipChannel * ShipChannel `json:"getChannel"`
2132}
@@ -111,8 +122,8 @@ mutation createChannel($appId: String!, $channelName: String!, $description: Str
111122 }
112123}`
113124
114- func (c * GraphQLClient ) CreateChannel (appID string , name string , description string ) error {
115- response := graphql. ResponseErrorOnly {}
125+ func (c * GraphQLClient ) CreateChannel (appID string , name string , description string ) ( * types. Channel , error ) {
126+ response := GraphQLResponseCreateChannel {}
116127
117128 request := graphql.Request {
118129 Query : createChannelQuery ,
@@ -124,15 +135,52 @@ func (c *GraphQLClient) CreateChannel(appID string, name string, description str
124135 }
125136
126137 if err := c .ExecuteRequest (request , & response ); err != nil {
127- return err
138+ return nil , err
128139 }
140+ return & types.Channel {
141+ ID : response .Data .ShipChannel .ID ,
142+ Name : response .Data .ShipChannel .Name ,
143+ Description : response .Data .ShipChannel .Description ,
144+ ReleaseSequence : response .Data .ShipChannel .CurrentSequence ,
145+ ReleaseLabel : response .Data .ShipChannel .CurrentVersion ,
146+ }, nil
147+ }
129148
130- return nil
149+ func (c * GraphQLClient ) GetChannelByName (appID string , name string , description string , create bool ) (* types.Channel , error ) {
150+ allChannels , err := c .ListChannels (appID )
151+ if err != nil {
152+ return nil , err
153+ }
131154
132- }
155+ matchingChannels := make ([]* types.Channel , 0 )
156+ for _ , channel := range allChannels {
157+ if channel .ID == name || channel .Name == name {
158+ matchingChannels = append (matchingChannels , & types.Channel {
159+ ID : channel .ID ,
160+ Name : channel .Name ,
161+ Description : channel .Description ,
162+ ReleaseSequence : channel .ReleaseSequence ,
163+ ReleaseLabel : channel .ReleaseLabel ,
164+ })
165+ }
166+ }
167+
168+ if len (matchingChannels ) == 0 {
169+ if create {
170+ channel , err := c .CreateChannel (appID , name , description )
171+ if err != nil {
172+ return nil , errors .Wrapf (err , "create channel %q " , name )
173+ }
174+ return channel , nil
175+ }
176+
177+ return nil , fmt .Errorf ("could not find channel %q" , name )
178+ }
133179
134- func ArchiveChannel (appID string , channelID string ) error {
135- return nil
180+ if len (matchingChannels ) > 1 {
181+ return nil , fmt .Errorf ("channel %q is ambiguous, please use channel ID" , name )
182+ }
183+ return matchingChannels [0 ], nil
136184}
137185
138186var getShipChannel = `
0 commit comments