8000 Merge pull request #467 from symfony-cli/book-versions · symfony-cli/symfony-cli@252d076 · GitHub
[go: up one dir, main page]

Skip to content

Commit 252d076

Browse files
authored
Merge pull request #467 from symfony-cli/book-versions
Add available versions for the book
2 parents 29e9053 + 0637c3d commit 252d076

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

book/clone.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
package book
2121

2222
import (
23+
"encoding/json"
2324
"fmt"
25+
"net/http"
2426
"os"
2527
"os/exec"
28+
"strings"
2629

2730
"github.com/pkg/errors"
2831
"github.com/symfony-cli/terminal"
@@ -41,6 +44,25 @@ func (b *Book) Clone(version string) error {
4144
}
4245

4346
ui.Section("Cloning the Repository")
47+
48+
// check that version exists on Github via the API
49+
resp, err := http.Get(fmt.Sprintf("https://api.github.com/repos/the-fast-track/book-%s", version))
50+
if err != nil {
51+
return errors.Wrap(err, "unable to get version on Github")
52+
}
53+
defer resp.Body.Close()
54+
if resp.StatusCode != http.StatusOK {
55+
versions, err := Versions()
56+
if err != nil {
57+
return errors.Wrap(err, "unable to get book versions")
58+
}
59+
terminal.Println("The version you requested does not exist; available versions:")
60+
for _, v := range versions {
61+
terminal.Println(fmt.Sprintf(" - %s", v))
62+
}
63+
return errors.New("please choose a valid version")
64+
}
65+
4466
cmd := exec.Command("git", "clone", fmt.Sprintf("https://github.com/the-fast-track/book-%s", version), b.Dir)
4567
cmd.Env = os.Environ()
4668
cmd.Stdout = os.Stdout
@@ -63,3 +85,25 @@ func (b *Book) Clone(version string) error {
6385
}
6486
return nil
6587
}
88+
89+
func Versions() ([]string, error) {
90+
resp, err := http.Get("https://api.github.com/orgs/the-fast-track/repos")
91+
if err != nil {
92+
return nil, errors.Wrap(err, "unable to get repositories from Github")
93+
}
94+
defer resp.Body.Close()
95+
if resp.StatusCode != http.StatusOK {
96+
return nil, errors.New("failed to get repositories from Github")
97+
}
98+
var repos []struct {
99+
Name string `json:"name"`
100+
}
101+
if err := json.NewDecoder(resp.Body).Decode(&repos); err != nil {
102+
return nil, errors.Wrap(err, "failed to decode response body")
103+
}
104+
versions := []string{}
105+
for _, repo := range repos {
106+
versions = append(versions, strings.Replace(repo.Name, "book-", "", 1))
107+
}
108+
return versions, nil
109+
}

commands/local_new.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,15 @@ var localNewCmd = &console.Command{
113113

114114
if c.Bool("book") {
115115
if symfonyVersion == "" {
116-
return console.Exit("The --version flag is required for the Symfony book", 1)
116+
versions, err := book.Versions()
117+
if err != nil {
118+
return errors.Wrap(err, "unable to get book versions")
119+
}
120+
terminal.Println("The --version flag is required for the Symfony book; available versions:")
121+
for _, v := range versions {
122+
terminal.Println(fmt.Sprintf(" - %s", v))
123+
}
124+
return console.Exit("", 1)
117125
}
118126

119127
book := &book.Book{

0 commit comments

Comments
 (0)
0