Skip to content

Commit eee95ab

Browse files
authored
Merge pull request #30 from marccampbell/ship-channels
Ship and Platform clients
2 parents 4fd5772 + b9da1c8 commit eee95ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1305
-396
lines changed

CONTRIBUTING.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Contributing
2+
3+
All you need is a computer with Go installed, and a Replicatd account to test with.
4+
5+
### Design
6+
7+
Replicated apps can be "platform" or "ship". Avoid deep-in-the-callstack checks for app type. There's a common "Client" class that should handle the switch on appType, and call the appropriate implementation. We would like to avoid having this switch get lower in the call stack.
8+
9+
Sometimes, the two different app types require different parametes (promote release is an example, one takes "required" and one doesn't). Don't normalize these to the lowest common denominator. The goal of this CLI is to provide all functionality, with minimal internal knowledge to manage Replicated apps. The app schemas will continue to be a little different, this CLI should mask these differences while still providing access to all features of both appTypes.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ import (
101101
"log"
102102
"os"
103103

104-
"github.com/replicatedhq/replicated/client"
104+
"github.com/replicatedhq/replicated/pkg/platformclient"
105105
)
106106

107107
func main() {
108108
token := os.Getenv("REPLICATED_API_TOKEN")
109109
appSlugOrID := os.Getenv("REPLICATED_APP")
110110

111-
api := client.New(token)
111+
api := platformclient.New(token)
112112

113113
app, err := api.GetApp(appSlugOrID)
114114
if err != nil {

cli/cmd/channel_adoption.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (r *runners) channelAdoption(cmd *cobra.Command, args []string) error {
2323
}
2424
chanID := args[0]
2525

26-
appChan, _, err := r.api.GetChannel(r.appID, chanID)
26+
appChan, _, err := r.platformAPI.GetChannel(r.appID, chanID)
2727
if err != nil {
2828
return err
2929
}

cli/cmd/channel_counts.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (r *runners) channelCounts(cmd *cobra.Command, args []string) error {
2323
}
2424
chanID := args[0]
2525

26-
appChan, _, err := r.api.GetChannel(r.appID, chanID)
26+
appChan, _, err := r.platformAPI.GetChannel(r.appID, chanID)
2727
if err != nil {
2828
return err
2929
}

cli/cmd/channel_create.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"github.com/replicatedhq/replicated/cli/print"
5-
"github.com/replicatedhq/replicated/client"
65
"github.com/spf13/cobra"
76
)
87

@@ -26,11 +25,7 @@ func init() {
2625
}
2726

2827
func (r *runners) channelCreate(cmd *cobra.Command, args []string) error {
29-
opts := &client.ChannelOptions{
30-
Name: channelCreateName,
31-
Description: channelCreateDescription,
32-
}
33-
allChannels, err := r.api.CreateChannel(r.appID, opts)
28+
allChannels, err := r.api.CreateChannel(r.appID, channelCreateName, channelCreateDescription)
3429
if err != nil {
3530
return err
3631
}

cli/cmd/channel_inspect.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (r *runners) channelInspect(cmd *cobra.Command, args []string) error {
2525
}
2626
chanID := args[0]
2727

28-
appChan, _, err := r.api.GetChannel(r.appID, chanID)
28+
appChan, _, err := r.platformAPI.GetChannel(r.appID, chanID)
2929
if err != nil {
3030
return err
3131
}

cli/cmd/channel_releases.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (r *runners) channelReleases(cmd *cobra.Command, args []string) error {
2323
}
2424
chanID := args[0]
2525

26-
_, releases, err := r.api.GetChannel(r.appID, chanID)
26+
_, releases, err := r.platformAPI.GetChannel(r.appID, chanID)
2727
if err != nil {
2828
return err
2929
}

cli/cmd/channel_rm.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (r *runners) channelRemove(cmd *cobra.Command, args []string) error {
2424
}
2525
chanID := args[0]
2626

27-
if err := r.api.ArchiveChannel(r.appID, chanID); err != nil {
27+
if err := r.platformAPI.ArchiveChannel(r.appID, chanID); err != nil {
2828
return err
2929
}
3030

cli/cmd/release_create.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"io/ioutil"
66

7-
"github.com/replicatedhq/replicated/client"
87
"github.com/spf13/cobra"
98
)
109

@@ -55,8 +54,8 @@ func (r *runners) releaseCreate(cmd *cobra.Command, args []string) error {
5554

5655
promoteChannelIDs := make([]string, 0)
5756
for _, c := range channels {
58-
if c.Id == createReleasePromote || c.Name == createReleasePromote {
59-
promoteChannelIDs = append(promoteChannelIDs, c.Id)
57+
if c.ID == createReleasePromote || c.Name == createReleasePromote {
58+
promoteChannelIDs = append(promoteChannelIDs, c.ID)
6059
}
6160
}
6261

@@ -69,10 +68,7 @@ func (r *runners) releaseCreate(cmd *cobra.Command, args []string) error {
6968
promoteChanID = promoteChannelIDs[0]
7069
}
7170

72-
opts := &client.ReleaseOptions{
73-
YAML: createReleaseYaml,
74-
}
75-
release, err := r.api.CreateRelease(r.appID, opts)
71+
release, err := r.api.CreateRelease(r.appID, createReleaseYaml)
7672
if err != nil {
7773
return err
7874
}

cli/cmd/release_inspect.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strconv"
77

88
"github.com/replicatedhq/replicated/cli/print"
9-
vendorAPI "github.com/replicatedhq/replicated/client"
9+
"github.com/replicatedhq/replicated/pkg/platformclient"
1010
"github.com/spf13/cobra"
1111
)
1212

@@ -30,9 +30,9 @@ func (r *runners) releaseInspect(cmd *cobra.Command, args []string) error {
3030
return fmt.Errorf("Failed to parse sequence argument %s", args[0])
3131
}
3232

33-
release, err := r.api.GetRelease(r.appID, seq)
33+
release, err := r.platformAPI.GetRelease(r.appID, seq)
3434
if err != nil {
35-
if err == vendorAPI.ErrNotFound {
35+
if err == platformclient.ErrNotFound {
3636
return fmt.Errorf("No such release %d", seq)
3737
}
3838
return err

cli/cmd/release_promote.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (r *runners) releasePromote(cmd *cobra.Command, args []string) error {
4040
}
4141
chanID := args[1]
4242

43-
if err := r.api.PromoteRelease(r.appID, seq, releaseVersion, releaseNotes, !releaseOptional, chanID); err != nil {
43+
if err := r.platformAPI.PromoteRelease(r.appID, seq, releaseVersion, releaseNotes, !releaseOptional, chanID); err != nil {
4444
return err
4545
}
4646

cli/cmd/release_update.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io/ioutil"
77
"strconv"
88

9-
"github.com/replicatedhq/replicated/client"
109
"github.com/spf13/cobra"
1110
)
1211

@@ -43,8 +42,7 @@ func (r *runners) releaseUpdate(cmd *cobra.Command, args []string) error {
4342
return fmt.Errorf("invalid release sequence: %s", args[0])
4443
}
4544

46-
opts := &client.ReleaseOptions{YAML: updateReleaseYaml}
47-
if err := r.api.UpdateRelease(r.appID, seq, opts); err != nil {
45+
if err := r.platformAPI.UpdateRelease(r.appID, seq, updateReleaseYaml); err != nil {
4846
return fmt.Errorf("Failure setting new yaml config for release: %v", err)
4947
}
5048

cli/cmd/root.go

+35-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import (
66
"os"
77
"text/tabwriter"
88

9+
"github.com/replicatedhq/replicated/pkg/shipclient"
10+
911
"github.com/replicatedhq/replicated/client"
12+
"github.com/replicatedhq/replicated/pkg/platformclient"
1013
"github.com/spf13/cobra"
1114
)
1215

@@ -20,15 +23,21 @@ const (
2023

2124
var appSlugOrID string
2225
var apiToken string
23-
var apiOrigin = "https://api.replicated.com/vendor"
26+
var platformOrigin = "https://api.replicated.com/vendor"
27+
var shipOrigin = "https://g.replicated.com/graphql"
2428

2529
func init() {
2630
RootCmd.PersistentFlags().StringVar(&appSlugOrID, "app", "", "The app slug or app id to use in all calls")
2731
RootCmd.PersistentFlags().StringVar(&apiToken, "token", "", "The API token to use to access your app in the Vendor API")
2832

2933
originFromEnv := os.Getenv("REPLICATED_API_ORIGIN")
3034
if originFromEnv != "" {
31-
apiOrigin = originFromEnv
35+
platformOrigin = originFromEnv
36+
}
37+
38+
shipOriginFromEnv := os.Getenv("REPLICATED_SHIP_ORIGIN")
39+
if shipOriginFromEnv != "" {
40+
shipOrigin = shipOriginFromEnv
3241
}
3342
}
3443

@@ -102,18 +111,38 @@ func Execute(stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
102111
return errors.New("Please provide your API token")
103112
}
104113
}
105-
api := client.NewHTTPClient(apiOrigin, apiToken)
106-
runCmds.api = api
114+
platformAPI := platformclient.NewHTTPClient(platformOrigin, apiToken)
115+
runCmds.platformAPI = platformAPI
116+
117+
shipAPI := shipclient.NewGraphQLClient(shipOrigin, apiToken)
118+
runCmds.shipAPI = shipAPI
119+
120+
commonAPI := client.NewClient(platformOrigin, shipOrigin, apiToken)
121+
runCmds.api = commonAPI
107122

108123
if appSlugOrID == "" {
109124
appSlugOrID = os.Getenv("REPLICATED_APP")
110125
}
111126

112-
app, err := api.GetApp(appSlugOrID)
127+
appType, err := commonAPI.GetAppType(appSlugOrID)
113128
if err != nil {
114129
return err
115130
}
116-
runCmds.appID = app.Id
131+
runCmds.appType = appType
132+
133+
if appType == "platform" {
134+
app, err := platformAPI.GetApp(appSlugOrID)
135+
if err != nil {
136+
return err
137+
}
138+
runCmds.appID = app.Id
139+
} else if appType == "ship" {
140+
app, err := shipAPI.GetApp(appSlugOrID)
141+
if err != nil {
142+
return err
143+
}
144+
runCmds.appID = app.ID
145+
}
117146

118147
return nil
119148
}

cli/cmd/runner.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ import (
44
"io"
55
"text/tabwriter"
66

7+
"github.com/replicatedhq/replicated/pkg/shipclient"
8+
79
"github.com/replicatedhq/replicated/client"
10+
"github.com/replicatedhq/replicated/pkg/platformclient"
811
)
912

1013
// Runner holds the I/O dependencies and configurations used by individual
1114
// commands, which are defined as methods on this type.
1215
type runners struct {
13-
appID string
14-
api client.Client
15-
stdin io.Reader
16-
w *tabwriter.Writer
16+
appID string
17+
appType string
18+
api client.Client
19+
platformAPI platformclient.Client
20+
shipAPI shipclient.Client
21+
stdin io.Reader
22+
w *tabwriter.Writer
1723
}

cli/cmd/shiprelease.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66

77
// releaseCmd represents the release command
88
var shipReleaseCommand = &cobra.Command{
9-
Use: "shiprelease",
10-
Short: "Manage ship releases",
11-
Long: `The shiprelease command allows vendors to create, display, modify, and archive their Ship releases.`,
9+
Use: "shiprelease",
10+
Short: "Manage ship releases",
11+
Long: `The shiprelease command allows vendors to create, display, modify, and archive their Ship releases.`,
12+
Hidden: true,
1213
}
1314

1415
func init() {

cli/cmd/shiprelease_create.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import (
66
"io/ioutil"
77
"net/url"
88

9+
"os"
10+
"strings"
11+
912
"github.com/go-kit/kit/log"
1013
"github.com/mitchellh/cli"
1114
"github.com/pkg/errors"
1215
"github.com/replicatedhq/ship/pkg/e2e"
1316
"github.com/spf13/cobra"
1417
"github.com/spf13/viper"
15-
"os"
16-
"strings"
1718
)
1819

1920
func init() {
@@ -117,10 +118,7 @@ func (r *Releaser) getParams() (token, specContents, semver, channelID, gqlAddr
117118
return
118119
}
119120

120-
func (r *Releaser) Release(
121-
ctx context.Context,
122-
) error {
123-
121+
func (r *Releaser) Release(ctx context.Context) error {
124122
token, specContents, semver, channelID, gqlAddr, err := r.getParams()
125123
if err != nil {
126124
return errors.Wrap(err, "load params")

cli/print/apps.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package print
2+
3+
import (
4+
"text/tabwriter"
5+
"text/template"
6+
7+
apps "github.com/replicatedhq/replicated/gen/go/v1"
8+
)
9+
10+
var appsTmplSrc = `ID NAME SCHEDULER
11+
{{ range . -}}
12+
{{ .ID }} {{ .Name }} {{ .Scheduler }}
13+
{{ end }}`
14+
15+
var appsTmpl = template.Must(template.New("apps").Funcs(funcs).Parse(appsTmplSrc))
16+
17+
func Apps(w *tabwriter.Writer, apps []apps.AppAndChannels) error {
18+
as := make([]map[string]interface{}, len(apps))
19+
20+
for i, a := range apps {
21+
as[i] = map[string]interface{}{
22+
"ID": a.App.Id,
23+
"Name": a.App.Name,
24+
"Scheduler": a.App.Scheduler,
25+
}
26+
}
27+
28+
if err := appsTmpl.Execute(w, as); err != nil {
29+
return err
30+
}
31+
32+
return w.Flush()
33+
}

cli/print/channels.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@ import (
44
"text/tabwriter"
55
"text/template"
66

7-
channels "github.com/replicatedhq/replicated/gen/go/v1"
7+
platformChannels "github.com/replicatedhq/replicated/gen/go/v1"
8+
"github.com/replicatedhq/replicated/pkg/types"
89
)
910

1011
var channelsTmplSrc = `ID NAME RELEASE VERSION
1112
{{ range . -}}
12-
{{ .Id }} {{ .Name }} {{ if ge .ReleaseSequence 1 }}{{ .ReleaseSequence }}{{else}} {{end}} {{ .ReleaseLabel }}
13+
{{ .ID }} {{ .Name }} {{ if ge .ReleaseSequence 1 }}{{ .ReleaseSequence }}{{else}} {{end}} {{ .ReleaseLabel }}
1314
{{ end }}`
1415

1516
var channelsTmpl = template.Must(template.New("channels").Parse(channelsTmplSrc))
1617

17-
func Channels(w *tabwriter.Writer, channels []channels.AppChannel) error {
18+
func Channels(w *tabwriter.Writer, channels []types.Channel) error {
19+
if err := channelsTmpl.Execute(w, channels); err != nil {
20+
return err
21+
}
22+
return w.Flush()
23+
}
24+
25+
func PlatformChannels(w *tabwriter.Writer, channels []platformChannels.AppChannel) error {
1826
if err := channelsTmpl.Execute(w, channels); err != nil {
1927
return err
2028
}

cli/print/releases.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"text/template"
77
"time"
88

9-
releases "github.com/replicatedhq/replicated/gen/go/v1"
9+
"github.com/replicatedhq/replicated/pkg/types"
1010
)
1111

1212
var releasesTmplSrc = `SEQUENCE CREATED EDITED ACTIVE_CHANNELS
@@ -16,7 +16,7 @@ var releasesTmplSrc = `SEQUENCE CREATED EDITED ACTIVE_CHANNELS
1616

1717
var releasesTmpl = template.Must(template.New("Releases").Funcs(funcs).Parse(releasesTmplSrc))
1818

19-
func Releases(w *tabwriter.Writer, appReleases []releases.AppReleaseInfo) error {
19+
func Releases(w *tabwriter.Writer, appReleases []types.ReleaseInfo) error {
2020
rs := make([]map[string]interface{}, len(appReleases))
2121

2222
for i, r := range appReleases {

0 commit comments

Comments
 (0)