Skip to content

Commit

Permalink
resolve merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrisyhjiang committed Jun 28, 2024
2 parents 405fb0e + e7ff07b commit 4cf6a43
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 51 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ jobs:
working-directory: ./pkgs/npm

steps:
- name: Update Windows s.defang.io/defang_win_amd64.zip short link
run: |
curl --request POST \
--url https://api.short.io/links/$DEFANG_WIN_AMD64_LNK \
--header "Authorization: $SHORTIO_PK" \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data "{\"originalURL\":\"https://github.com/DefangLabs/defang/releases/download/${TAG}/defang_${TAG#v}_windows_amd64.zip\"}"
env:
SHORTIO_PK: ${{ secrets.SHORTIO_PK }}
TAG: ${{ github.ref_name }}
DEFANG_WIN_AMD64_LNK: "lnk_4vSQ_CDukZ5POEE4o0mMDysr2U"

- name: Trigger CLI Autodoc
uses: peter-evans/repository-dispatch@v3
with:
Expand Down
27 changes: 16 additions & 11 deletions src/cmd/cli/command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func Execute(ctx context.Context) error {
if code == connect.CodeFailedPrecondition && (strings.Contains(err.Error(), "EULA") || strings.Contains(err.Error(), "terms")) {
printDefangHint("Please use the following command to see the Defang terms of service:", "terms")
}

return ExitCode(code)
}

Expand Down Expand Up @@ -439,7 +438,7 @@ var generateCmd = &cobra.Command{
}
return cli.InitFromSamples(cmd.Context(), "", []string{sample})
}

sampleList, fetchSamplesErr := cli.FetchSamples(cmd.Context())
if sample == "" {
if err := survey.AskOne(&survey.Select{
Message: "Choose the language you'd like to use:",
Expand All @@ -449,17 +448,16 @@ var generateCmd = &cobra.Command{
}, &language); err != nil {
return err
}

// Fetch the list of samples from the Defang repository
if samples, err := cli.FetchSamples(cmd.Context()); err != nil {
term.Debug("unable to fetch samples:", err)
} else if len(samples) > 0 {
if fetchSamplesErr != nil {
term.Debug("unable to fetch samples:", fetchSamplesErr)
} else if len(sampleList) > 0 {
const generateWithAI = "Generate with AI"

lang := strings.ToLower(language)
sampleNames := []string{generateWithAI}
sampleDescriptions := []string{"Generate a sample from scratch using a language prompt"}
for _, sample := range samples {
for _, sample := range sampleList {
if slices.Contains(sample.Languages, lang) {
sampleNames = append(sampleNames, sample.Name)
sampleDescriptions = append(sampleDescriptions, sample.ShortDescription)
Expand Down Expand Up @@ -510,6 +508,13 @@ var generateCmd = &cobra.Command{

if sample != "" {
qs = qs[1:] // user picked a sample, so we skip the description question
sampleExists := slices.ContainsFunc(sampleList, func(s cli.Sample) bool {
return s.Name == sample
})

if !sampleExists {
return cli.ErrSampleNotFound
}
}

prompt := struct {
Expand Down Expand Up @@ -546,19 +551,19 @@ var generateCmd = &cobra.Command{
}

// Check if the current folder is empty
if empty, err := pkg.IsDirEmpty("."); !empty || err != nil {
term.Warn("The folder is not empty. We recommend running this command in an empty folder.")
if empty, err := pkg.IsDirEmpty(prompt.Folder); !os.IsNotExist(err) && !empty {
term.Warnf("The folder %q is not empty. We recommend running this command in an empty folder.", prompt.Folder)
}

if sample != "" {
term.Info("Fetching sample from the Defang repository...")
err := cli.InitFromSamples(cmd.Context(), "", []string{sample})
err := cli.InitFromSamples(cmd.Context(), prompt.Folder, []string{sample})
if err != nil {
return err
}
} else {
term.Info("Working on it. This may take 1 or 2 minutes...")
_, err := cli.GenerateWithAI(cmd.Context(), client, language, prompt.Description)
_, err := cli.GenerateWithAI(cmd.Context(), client, language, prompt.Folder, prompt.Description)
if err != nil {
return err
}
Expand Down
12 changes: 3 additions & 9 deletions src/cmd/cli/command/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"context"
"encoding/json"
"errors"
"net/http"
"strings"

"github.com/DefangLabs/defang/src/pkg/http"
"golang.org/x/mod/semver"
)

var httpClient = http.DefaultClient

func isNewer(current, comparand string) bool {
version, ok := normalizeVersion(current)
if !ok {
Expand All @@ -38,16 +36,12 @@ func GetCurrentVersion() string {
}

func GetLatestVersion(ctx context.Context) (string, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.github.com/repos/DefangLabs/defang/releases/latest", nil)
if err != nil {
return "", err
}
resp, err := httpClient.Do(req)
resp, err := http.GetWithContext(ctx, "https://api.github.com/repos/DefangLabs/defang/releases/latest")
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode != 200 {
// The primary rate limit for unauthenticated requests is 60 requests per hour, per IP.
return "", errors.New(resp.Status)
}
Expand Down
6 changes: 5 additions & 1 deletion src/cmd/cli/command/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/http"
"net/http/httptest"
"testing"

ourHttp "github.com/DefangLabs/defang/src/pkg/http"
)

func TestIsNewer(t *testing.T) {
Expand Down Expand Up @@ -75,7 +77,9 @@ func TestGetLatestVersion(t *testing.T) {
rec.Header().Add("Content-Type", "application/json")
rec.WriteString(fmt.Sprintf(`{"tag_name":"%v"}`, version))

httpClient = &http.Client{Transport: &mockRoundTripper{
client := ourHttp.DefaultClient
t.Cleanup(func() { ourHttp.DefaultClient = client })
ourHttp.DefaultClient = &http.Client{Transport: &mockRoundTripper{
method: http.MethodGet,
url: "https://api.github.com/repos/DefangLabs/defang/releases/latest",
resp: rec.Result(),
Expand Down
2 changes: 1 addition & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/digitalocean/godo v1.111.0
github.com/docker/docker v25.0.5+incompatible
github.com/google/uuid v1.6.0
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/miekg/dns v1.1.59
github.com/moby/patternmatcher v0.6.0
github.com/muesli/termenv v0.15.2
Expand All @@ -44,7 +45,6 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
Expand Down
8 changes: 6 additions & 2 deletions src/pkg/cli/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/term"
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
)

func GenerateWithAI(ctx context.Context, client client.Client, language string, description string) ([]string, error) {
func GenerateWithAI(ctx context.Context, client client.Client, language, dir, description string) ([]string, error) {
if DoDryRun {
term.Warn("Dry run, not generating files")
return nil, ErrDryRun
Expand Down Expand Up @@ -38,11 +39,14 @@ func GenerateWithAI(ctx context.Context, client client.Client, language string,

// Write each file to disk
term.Info("Writing files to disk...")
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
}
for _, file := range response.Files {
// Print the files that were generated
fmt.Println(" -", file.Name)
// TODO: this will overwrite existing files
if err = os.WriteFile(file.Name, []byte(file.Content), 0644); err != nil {
if err = os.WriteFile(filepath.Join(dir, file.Name), []byte(file.Content), 0644); err != nil {
return nil, err
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/pkg/cli/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand All @@ -15,6 +16,8 @@ import (
"github.com/DefangLabs/defang/src/pkg/term"
)

var ErrSampleNotFound = errors.New("sample not found")

type Sample struct {
Name string `json:"name"`
Title string `json:"title"`
Expand All @@ -24,6 +27,7 @@ type Sample struct {
ShortDescription string `json:"shortDescription"`
Tags []string `json:"tags"`
Languages []string `json:"languages"`
Configs []string `json:"configs"`
}

func FetchSamples(ctx context.Context) ([]Sample, error) {
Expand Down Expand Up @@ -63,6 +67,9 @@ func InitFromSamples(ctx context.Context, dir string, names []string) error {
defer tarball.Close()
tarReader := tar.NewReader(tarball)
term.Info("Writing files to disk...")

sampleFound := false

for {
h, err := tarReader.Next()
if err != nil {
Expand All @@ -73,13 +80,17 @@ func InitFromSamples(ctx context.Context, dir string, names []string) error {
}

for _, name := range names {
// Create a subdirectory for each sample when there is more than one sample requested
// Create the sample directory or subdirectory for each sample when there is more than one sample requested
subdir := ""
if len(names) > 1 {
subdir = name
}
if err := os.MkdirAll(filepath.Join(dir, subdir), 0755); err != nil {
return err
}
prefix := fmt.Sprintf("%s-%s/samples/%s/", repo, branch, name)
if base, ok := strings.CutPrefix(h.Name, prefix); ok && len(base) > 0 {
sampleFound = true
fmt.Println(" -", base)
path := filepath.Join(dir, subdir, base)
if h.FileInfo().IsDir() {
Expand All @@ -94,6 +105,9 @@ func InitFromSamples(ctx context.Context, dir string, names []string) error {
}
}
}
if !sampleFound {
return ErrSampleNotFound
}
return nil
}

Expand Down
17 changes: 17 additions & 0 deletions src/pkg/cli/new_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cli

import (
"context"
"errors"
"testing"
)

func TestInitFromSamples(t *testing.T) {
err := InitFromSamples(context.Background(), t.TempDir(), []string{"nonexisting"})
if err == nil {
t.Fatal("Expected test to fail")
}
if !errors.Is(err, ErrSampleNotFound) {
t.Errorf("Expected error to be %v, got %v", ErrSampleNotFound, err)
}
}
20 changes: 20 additions & 0 deletions src/pkg/http/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package http

import (
"github.com/DefangLabs/defang/src/pkg/term"
"github.com/hashicorp/go-retryablehttp"
)

var DefaultClient = newClient().StandardClient()

type termLogger struct{}

func (termLogger) Printf(format string, args ...interface{}) {
term.Debugf(format, args...)
}

func newClient() *retryablehttp.Client {
c := retryablehttp.NewClient() // default client retries 4 times: 1+2+4+8 = 15s max
c.Logger = termLogger{}
return c
}
8 changes: 4 additions & 4 deletions src/pkg/http/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import (
type Header = http.Header

func GetWithContext(ctx context.Context, url string) (*http.Response, error) {
hreq, err := http.NewRequestWithContext(ctx, "GET", url, nil)
hreq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
return http.DefaultClient.Do(hreq)
return DefaultClient.Do(hreq)
}

func GetWithHeader(ctx context.Context, url string, header http.Header) (*http.Response, error) {
hreq, err := http.NewRequestWithContext(ctx, "GET", url, nil)
hreq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
hreq.Header = header
return http.DefaultClient.Do(hreq)
return DefaultClient.Do(hreq)
}

func GetWithAuth(ctx context.Context, url, auth string) (*http.Response, error) {
Expand Down
3 changes: 1 addition & 2 deletions src/pkg/http/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package http
import (
"fmt"
"io"
"net/http"
"net/url"
)

// PostForValues issues a POST to the specified URL and returns the response body as url.Values.
func PostForValues(_url, contentType string, body io.Reader) (url.Values, error) {
resp, err := http.Post(_url, contentType, body)
resp, err := DefaultClient.Post(_url, contentType, body)
if err != nil {
return nil, err
}
Expand Down
14 changes: 2 additions & 12 deletions src/pkg/http/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"io"
"net/http"
"net/url"
)

// Put issues a PUT to the specified URL.
Expand All @@ -19,19 +18,10 @@ import (
// See the Client.Do method documentation for details on how redirects
// are handled.
func Put(ctx context.Context, url string, contentType string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, "PUT", url, body)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return http.DefaultClient.Do(req)
}

func RemoveQueryParam(qurl string) string {
u, err := url.Parse(qurl)
if err != nil {
return qurl
}
u.RawQuery = ""
return u.String()
return DefaultClient.Do(req)
}
Loading

0 comments on commit 4cf6a43

Please sign in to comment.