Skip to content

Commit

Permalink
Support encore app link with no encore.app file
Browse files Browse the repository at this point in the history
To simplify linking an existing repository to an Encore app,
fix `encore app link` to create the `encore.app` file if it
does not already exist.
  • Loading branch information
eandre committed Aug 16, 2023
1 parent ff73cb4 commit afb6b2f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
17 changes: 15 additions & 2 deletions cli/cmd/encore/app/link.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"bytes"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -40,13 +41,25 @@ func init() {
}

func linkApp(appID string, force bool) {
root, _ := cmdutil.AppRoot()
// Determine the app root.
root, _, err := cmdutil.MaybeAppRoot()
if errors.Is(err, cmdutil.ErrNoEncoreApp) {
root, err = os.Getwd()
}
if err != nil {
cmdutil.Fatal(err)
}

filePath := filepath.Join(root, "encore.app")
data, err := os.ReadFile(filePath)
if err != nil {
if err != nil && !errors.Is(err, fs.ErrNotExist) {
cmdutil.Fatal(err)
os.Exit(1)
}
if len(bytes.TrimSpace(data)) == 0 {
// Treat missing and empty files as an empty object.
data = []byte("{}")
}

val, err := hujson.Parse(data)
if err != nil {
Expand Down
39 changes: 27 additions & 12 deletions cli/cmd/encore/cmdutil/cmdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cmdutil

import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
Expand All @@ -16,41 +18,54 @@ import (
"encr.dev/pkg/errlist"
)

// AppRoot determines the app root by looking for the "encore.app" file,
var (
ErrNoEncoreApp = errors.New("no encore.app found in directory (or any of the parent directories)")
ErrEncoreAppIsDir = errors.New("encore.app is a directory, not a file")
)

// MaybeAppRoot determines the app root by looking for the "encore.app" file,
// initially in the current directory and then recursively in parent directories
// up to the filesystem root.
//
// It reports the absolute path to the app root, and the
// relative path from the app root to the working directory.
//
// On errors it prints an error message and exits.
func AppRoot() (appRoot, relPath string) {
func MaybeAppRoot() (appRoot, relPath string, err error) {
dir, err := os.Getwd()
if err != nil {
Fatal(err)
return "", "", err
}
rel := "."
for {
path := filepath.Join(dir, "encore.app")
fi, err := os.Stat(path)
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
dir2 := filepath.Dir(dir)
if dir2 == dir {
Fatal("no encore.app found in directory (or any of the parent directories).")
return "", "", ErrNoEncoreApp
}
rel = filepath.Join(filepath.Base(dir), rel)
dir = dir2
continue
} else if err != nil {
Fatal(err)
return "", "", err
} else if fi.IsDir() {
Fatal("encore.app is a directory, not a file")
return "", "", ErrEncoreAppIsDir
} else {
return dir, rel
return dir, rel, nil
}
}
}

// AppRoot is like MaybeAppRoot but instead of returning an error
// it prints it to stderr and exits.
func AppRoot() (appRoot, relPath string) {
appRoot, relPath, err := MaybeAppRoot()
if err != nil {
Fatal(err)
}
return appRoot, relPath
}

// AppSlug reports the current app's app slug.
// It throws a fatal error if the app is not connected with the Encore Platform.
func AppSlug() string {
Expand All @@ -75,8 +90,8 @@ func Fatal(args ...any) {
}

red := color.New(color.FgRed)
red.Fprint(os.Stderr, "error: ")
red.Fprintln(os.Stderr, args...)
_, _ = red.Fprint(os.Stderr, "error: ")
_, _ = red.Fprintln(os.Stderr, args...)
os.Exit(1)
}

Expand Down

0 comments on commit afb6b2f

Please sign in to comment.