Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Clone recipes into a local cache folder * Fix broken test * Copy the cached repo to a new folder * Copy specific integration and langauge for recipes * User can select all integrations * Start cleaning up * Let users select their integration and copy all the files over * Re-order some functions * Add comments * Add os test file * Add comment for exported functions * Add afero to os operations * Add tests for os and recipes, move git functions to separate package * Have a boolean for checking an integration * Remove some unused go packages * Fix some broken tests * Rename all things recipes => samples (#96) * Setup samples commands (#118) * Clone recipes into a local cache folder * Fix broken test * Copy the cached repo to a new folder * Copy specific integration and langauge for recipes * User can select all integrations * Start cleaning up * Let users select their integration and copy all the files over * Re-order some functions * Add comments * Add os test file * Add comment for exported functions * Add afero to os operations * Add tests for os and recipes, move git functions to separate package * Have a boolean for checking an integration * Remove some unused go packages * Fix some broken tests * Rename all things recipes => samples (#96) * Add samples create and list subcommands * Update comment for samples list * Fix linter errors * Actually fix linter errors (needed newlines between imports) * Run go mod tidy * Add missing newlines for sample data * Change to existing pattern of returning the cmd struct * Add comments to exported structs * Configure .env for samples (#117) * Clone recipes into a local cache folder * Fix broken test * Copy the cached repo to a new folder * Copy specific integration and langauge for recipes * User can select all integrations * Start cleaning up * Let users select their integration and copy all the files over * Re-order some functions * Add comments * Add os test file * Add comment for exported functions * Add afero to os operations * Add tests for os and recipes, move git functions to separate package * Have a boolean for checking an integration * Remove some unused go packages * Fix some broken tests * Rename all things recipes => samples (#96) * Add samples create and list subcommands * Move .env file to the correct location * Write .env file with the api key and signing secret * Update comment for samples list * Update static dir path for the .env * Modify the server files to point to the correct dotenv location (#132) * Set publishable key when creating a sample (#135)
- Loading branch information
1 parent
e3eb35d
commit 31fc5e1
Showing
15 changed files
with
1,406 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,27 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/stripe/stripe-cli/pkg/cmd/samples" | ||
) | ||
|
||
type samplesCmd struct { | ||
cmd *cobra.Command | ||
} | ||
|
||
func newSamplesCmd() *samplesCmd { | ||
samplesCmd := &samplesCmd{ | ||
cmd: &cobra.Command{ | ||
// TODO: fixtures subcommand | ||
Use: "samples", | ||
Short: `Sample integrations built by Stripe`, | ||
Long: ``, | ||
}, | ||
} | ||
|
||
samplesCmd.cmd.AddCommand(samples.NewCreateCmd(&Config).Cmd) | ||
samplesCmd.cmd.AddCommand(samples.NewListCmd().Cmd) | ||
|
||
return samplesCmd | ||
} |
This file contains 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,110 @@ | ||
package samples | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/stripe/stripe-cli/pkg/ansi" | ||
"github.com/stripe/stripe-cli/pkg/config" | ||
gitpkg "github.com/stripe/stripe-cli/pkg/git" | ||
"github.com/stripe/stripe-cli/pkg/samples" | ||
"github.com/stripe/stripe-cli/pkg/validators" | ||
|
||
"gopkg.in/src-d/go-git.v4" | ||
) | ||
|
||
// CreateCmd wraps the `create` command for samples which generates a new | ||
// project | ||
type CreateCmd struct { | ||
cfg *config.Config | ||
Cmd *cobra.Command | ||
} | ||
|
||
// NewCreateCmd creates and returns a create command for samples | ||
func NewCreateCmd(config *config.Config) *CreateCmd { | ||
createCmd := &CreateCmd{ | ||
cfg: config, | ||
} | ||
createCmd.Cmd = &cobra.Command{ | ||
Use: "create", | ||
Args: validators.ExactArgs(1), | ||
ValidArgs: samples.Names(), | ||
Short: "create a Stripe sample", | ||
RunE: createCmd.runCreateCmd, | ||
} | ||
|
||
return createCmd | ||
} | ||
|
||
func (cc *CreateCmd) runCreateCmd(cmd *cobra.Command, args []string) error { | ||
sample := samples.Samples{ | ||
Config: cc.cfg, | ||
Fs: afero.NewOsFs(), | ||
Git: gitpkg.Operations{}, | ||
} | ||
selectedSample := args[0] | ||
|
||
spinner := ansi.StartSpinner(fmt.Sprintf("Downloading %s", selectedSample), os.Stdout) | ||
// Initialize the selected sample in the local cache directory. | ||
// This will either clone or update the specified sample, | ||
// depending on whether or not it's. Additionally, this | ||
// identifies if the sample has multiple integrations and what | ||
// languages it supports. | ||
err := sample.Initialize(selectedSample) | ||
if err != nil { | ||
switch e := err.Error(); e { | ||
case git.NoErrAlreadyUpToDate.Error(): | ||
// Repo is already up to date. This isn't a program | ||
// error to continue as normal | ||
break | ||
case git.ErrRepositoryAlreadyExists.Error(): | ||
// If the repository already exists and we don't pull | ||
// for some reason, that's fine as we can use the existing | ||
// repository | ||
break | ||
default: | ||
ansi.StopSpinner(spinner, "An error occurred.", os.Stdout) | ||
return err | ||
} | ||
} | ||
ansi.StopSpinner(spinner, "Finished downloading.", os.Stdout) | ||
|
||
// Once we've initialized the sample in the local cache | ||
// directory, the user needs to select which integration they | ||
// want to work with (if selectedSamplelicable) and which language they | ||
// want to copy | ||
err = sample.SelectOptions() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Create the target folder to copy the sample in to. We do | ||
/// this here in case any of the steps above fail, minimizing | ||
// the change that we create a dangling empty folder | ||
targetPath, err := sample.MakeFolder(selectedSample) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Perform the copy of the sample given the selected options | ||
// from the selections above | ||
err = sample.Copy(targetPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = sample.ConfigureDotEnv(targetPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = sample.PointToDotEnv(targetPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains 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,42 @@ | ||
package samples | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/stripe/stripe-cli/pkg/samples" | ||
"github.com/stripe/stripe-cli/pkg/validators" | ||
) | ||
|
||
// ListCmd prints a list of all the available sample projects that users can | ||
// generate | ||
type ListCmd struct { | ||
Cmd *cobra.Command | ||
} | ||
|
||
// NewListCmd creates and returns a list command for samples | ||
func NewListCmd() *ListCmd { | ||
listCmd := &ListCmd{} | ||
listCmd.Cmd = &cobra.Command{ | ||
Use: "list", | ||
Args: validators.NoArgs, | ||
Short: "list available Stripe samples", | ||
Long: `A list of available Stripe Sample integrations`, | ||
Run: listCmd.runListCmd, | ||
} | ||
|
||
return listCmd | ||
} | ||
|
||
func (lc *ListCmd) runListCmd(cmd *cobra.Command, args []string) { | ||
fmt.Println("A list of available Stripe Sample integrations:") | ||
fmt.Println() | ||
|
||
for _, sample := range samples.List { | ||
fmt.Println(sample.BoldName()) | ||
fmt.Println(sample.Description) | ||
fmt.Println(fmt.Sprintf("Repo: %s", sample.URL)) | ||
fmt.Println() | ||
} | ||
} |
This file contains 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 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 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,50 @@ | ||
package git | ||
|
||
import "gopkg.in/src-d/go-git.v4" | ||
|
||
// Operations contains the behaviors of the internal git package | ||
type Operations struct{} | ||
|
||
// Interface defines the behaviors of the internal git package | ||
type Interface interface { | ||
Clone(string, string) error | ||
Pull(string) error | ||
} | ||
|
||
// Clone clones a repo locally, returns an error if it fails | ||
func (g Operations) Clone(appCachePath, app string) error { | ||
_, err := git.PlainClone(appCachePath, false, &git.CloneOptions{ | ||
URL: app, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Pull will update the changes for the provided repo or fails | ||
func (g Operations) Pull(appCachePath string) error { | ||
repo, err := git.PlainOpen(appCachePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
repo.Fetch(&git.FetchOptions{ | ||
Force: true, | ||
}) | ||
|
||
worktree, err := repo.Worktree() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = worktree.Reset(&git.ResetOptions{ | ||
Mode: git.HardReset, | ||
}) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.