-
Notifications
You must be signed in to change notification settings - Fork 26
CLI using cobra #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// channelCmd represents the channel command | ||
var channelCmd = &cobra.Command{ | ||
Use: "channel", | ||
Short: "Manage and review channels", | ||
Long: "Manage and review channels", | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(channelCmd) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/replicatedhq/replicated/cli/print" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var channelCreateCmd = &cobra.Command{ | ||
Use: "create", | ||
Short: "Create a new channel in your app", | ||
Long: `Create a new channel in your app and print the full set of channels in the app on success. | ||
|
||
Example: | ||
replicated channel create --name Beta --description 'New features subject to change'`, | ||
} | ||
|
||
var channelCreateName string | ||
var channelCreateDescription string | ||
|
||
func init() { | ||
channelCmd.AddCommand(channelCreateCmd) | ||
|
||
channelCreateCmd.Flags().StringVar(&channelCreateName, "name", "", "The name of this channel") | ||
channelCreateCmd.Flags().StringVar(&channelCreateDescription, "description", "", "A longer description of this channel") | ||
} | ||
|
||
func (r *runners) channelCreate(cmd *cobra.Command, args []string) error { | ||
allChannels, err := r.api.CreateChannel(r.appID, channelCreateName, channelCreateDescription) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return print.Channels(r.w, allChannels) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/replicatedhq/replicated/cli/print" | ||
) | ||
|
||
// channelInspectCmd represents the channelInspect command | ||
var channelInspectCmd = &cobra.Command{ | ||
Use: "inspect", | ||
Short: "Show full details for a channel", | ||
Long: "Show full details for a channel", | ||
} | ||
|
||
func init() { | ||
channelCmd.AddCommand(channelInspectCmd) | ||
} | ||
|
||
func (r *runners) channelInspect(cmd *cobra.Command, args []string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. im concerned about how parseable this output is. a lot of ppl will use cli for automation |
||
if len(args) != 1 { | ||
return fmt.Errorf(cmd.UsageString()) | ||
} | ||
chanID := args[0] | ||
|
||
appChan, releases, err := r.api.GetChannel(r.appID, chanID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = print.ChannelAttrs(r.w, appChan); err != nil { | ||
return err | ||
} | ||
|
||
if _, err = fmt.Fprint(r.w, "\nADOPTION\n"); err != nil { | ||
return err | ||
} | ||
if err = print.ChannelAdoption(r.w, &appChan.Adoption); err != nil { | ||
return err | ||
} | ||
|
||
if _, err = fmt.Fprint(r.w, "\nLICENSE_COUNTS\n"); err != nil { | ||
return err | ||
} | ||
if err = print.LicenseCounts(r.w, &appChan.LicenseCounts); err != nil { | ||
return err | ||
} | ||
|
||
if _, err = fmt.Fprint(r.w, "\nRELEASES\n"); err != nil { | ||
return err | ||
} | ||
if err = print.ChannelReleases(r.w, releases); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/replicatedhq/replicated/cli/print" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// channelLsCmd represents the channelLs command | ||
var channelLsCmd = &cobra.Command{ | ||
Use: "ls", | ||
Short: "List all channels in your app", | ||
Long: "List all channels in your app", | ||
} | ||
|
||
func init() { | ||
channelCmd.AddCommand(channelLsCmd) | ||
} | ||
|
||
func (r *runners) channelList(cmd *cobra.Command, args []string) error { | ||
channels, err := r.api.ListChannels(r.appID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return print.Channels(r.w, channels) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// channelRmCmd represents the channelRm command | ||
var channelRmCmd = &cobra.Command{ | ||
Use: "rm CHANNEL_ID", | ||
Short: "Remove (archive) a channel", | ||
Long: "Remove (archive) a channel", | ||
} | ||
|
||
func init() { | ||
channelCmd.AddCommand(channelRmCmd) | ||
} | ||
|
||
func (r *runners) channelRemove(cmd *cobra.Command, args []string) error { | ||
if len(args) != 1 { | ||
return fmt.Errorf("channel ID is required") | ||
} | ||
chanID := args[0] | ||
|
||
if err := r.api.ArchiveChannel(r.appID, chanID); err != nil { | ||
return err | ||
} | ||
|
||
// ignore the error since operation was successful | ||
fmt.Fprintf(r.w, "Channel %s successfully archived\n", chanID) | ||
r.w.Flush() | ||
|
||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cmd | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/base64" | ||
"io" | ||
"testing" | ||
) | ||
|
||
// generate a random string or call t.Fatal | ||
func token(t *testing.T, n int) string { | ||
if n == 0 { | ||
n = 256 | ||
} | ||
data := make([]byte, int(n)) | ||
if _, err := io.ReadFull(rand.Reader, data); err != nil { | ||
t.Fatal(err) | ||
} | ||
return base64.RawURLEncoding.EncodeToString(data) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// releaseCmd represents the release command | ||
var releaseCmd = &cobra.Command{ | ||
Use: "release", | ||
Short: "Manage app releases", | ||
Long: `The release command allows vendors to create, display, modify, and archive their releases.`, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(releaseCmd) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var createReleaseYaml string | ||
|
||
var releaseCreateCmd = &cobra.Command{ | ||
Use: "create", | ||
Short: "Create a new release", | ||
Long: `Create a new release by providing YAML configuration for the next release in | ||
your sequence.`, | ||
} | ||
|
||
func init() { | ||
releaseCmd.AddCommand(releaseCreateCmd) | ||
|
||
releaseCreateCmd.Flags().StringVar(&createReleaseYaml, "yaml", "", "The YAML config for this release") | ||
} | ||
|
||
func (r *runners) releaseCreate(cmd *cobra.Command, args []string) error { | ||
if createReleaseYaml == "" { | ||
return fmt.Errorf("yaml is required") | ||
} | ||
|
||
// API does not accept yaml in create operation, so first create then udpate | ||
release, err := r.api.CreateRelease(r.appID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := fmt.Fprintf(r.w, "SEQUENCE: %d\n", release.Sequence); err != nil { | ||
return err | ||
} | ||
r.w.Flush() | ||
|
||
if err := r.api.UpdateRelease(r.appID, release.Sequence, createReleaseYaml); err != nil { | ||
return fmt.Errorf("Failure setting yaml config for release: %v", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be awesome if printers supported formats eg tabs, json, yaml