Skip to content
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

Add the ability to initialise a compute project from a specific branch #14

Merged
merged 2 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions pkg/compute/compute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestInit(t *testing.T) {
DeleteBackendFn: deleteBackendOK,
DeleteDomainFn: deleteDomainOK,
},
wantError: "error fetching package template: repository not found",
wantError: "error fetching package template:",
},
{
name: "create service error",
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestInit(t *testing.T) {
wantOutput: []string{
"Initializing...",
"Fetching package template...",
"Updating package manifest..",
"Updating package manifest...",
},
manifestIncludes: `name = "test"`,
},
Expand All @@ -134,7 +134,7 @@ func TestInit(t *testing.T) {
wantOutput: []string{
"Initializing...",
"Fetching package template...",
"Updating package manifest..",
"Updating package manifest...",
},
manifestIncludes: `description = "test"`,
},
Expand All @@ -152,10 +152,27 @@ func TestInit(t *testing.T) {
wantOutput: []string{
"Initializing...",
"Fetching package template...",
"Updating package manifest..",
"Updating package manifest...",
},
manifestIncludes: `authors = ["test@example.com"]`,
},
{
name: "with from repository and branch",
args: []string{"compute", "init", "--from", "https://github.com/fastly/fastly-template-rust-default.git", "--branch", "master"},
configFile: config.File{Token: "123"},
api: mock.API{
GetTokenSelfFn: tokenOK,
GetUserFn: getUserOk,
CreateServiceFn: createServiceOK,
CreateDomainFn: createDomainOK,
CreateBackendFn: createBackendOK,
},
wantOutput: []string{
"Initializing...",
"Fetching package template...",
"Updating package manifest...",
},
},
{
name: "default",
args: []string{"compute", "init"},
Expand All @@ -181,8 +198,11 @@ func TestInit(t *testing.T) {
},
wantOutput: []string{
"Initializing...",
"Creating service...",
"Creating domain...",
"Creating backend...",
"Fetching package template...",
"Updating package manifest..",
"Updating package manifest...",
},
},
} {
Expand Down
21 changes: 17 additions & 4 deletions pkg/compute/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/fastly"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
)

type template struct {
Expand All @@ -30,6 +31,7 @@ type template struct {

const (
defaultTemplate = "https://github.com/fastly/fastly-template-rust-default.git"
defaultTemplateBranch = "0.2.0-alpha3"
defaultTopLevelDomain = "edgecompute.app"
manageServiceBaseURL = "https://manage.fastly.com/configure/services/"
)
Expand All @@ -54,6 +56,7 @@ type InitCommand struct {
description string
author string
from string
branch string
path string
domain string
backend string
Expand All @@ -68,6 +71,7 @@ func NewInitCommand(parent common.Registerer, globals *config.Data) *InitCommand
c.CmdClause.Flag("description", "Description of the package").Short('d').StringVar(&c.description)
c.CmdClause.Flag("author", "Author of the package").Short('a').StringVar(&c.author)
c.CmdClause.Flag("from", "Git repository containing package template").Short('f').StringVar(&c.from)
c.CmdClause.Flag("branch", "Git branch name to clone from package template repository").Hidden().StringVar(&c.branch)
c.CmdClause.Flag("path", "Destination to write the new package, defaulting to the current directory").Short('p').StringVar(&c.path)
c.CmdClause.Flag("domain", "The name of the domain associated to the package").StringVar(&c.path)
c.CmdClause.Flag("backend", "A hostname, IPv4, or IPv6 address for the package backend").StringVar(&c.path)
Expand Down Expand Up @@ -144,7 +148,7 @@ func (c *InitCommand) Exec(in io.Reader, out io.Writer) (err error) {
var defaultEmail string
if email := c.Globals.File.Email; email != "" {
defaultEmail = email
label = fmt.Sprintf("%s [%s]", label, email)
label = fmt.Sprintf("%s[%s]", label, email)
}

c.author, err = text.Input(out, label, in)
Expand Down Expand Up @@ -259,10 +263,19 @@ func (c *InitCommand) Exec(in io.Reader, out io.Writer) (err error) {
}
defer os.RemoveAll(tempdir)

var ref plumbing.ReferenceName
if c.from == defaultTemplate {
ref = plumbing.NewBranchReferenceName(defaultTemplateBranch)
}
if c.branch != "" {
ref = plumbing.NewBranchReferenceName(c.branch)
}
phamann marked this conversation as resolved.
Show resolved Hide resolved

if _, err := git.PlainClone(tempdir, false, &git.CloneOptions{
URL: c.from,
Depth: 1,
Progress: progress,
URL: c.from,
ReferenceName: ref,
Depth: 1,
Progress: progress,
}); err != nil {
return fmt.Errorf("error fetching package template: %w", err)
}
Expand Down