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

feat(compute/build): allow user to specify project directory to build #1043

Merged
merged 2 commits into from
Oct 16, 2023
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
8 changes: 8 additions & 0 deletions DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,11 @@ The file is added to `.gitignore` to avoid it being added to the git repository.
When compiling the CLI for a new release, it will execute [`./scripts/config.sh`](./scripts/config.sh). The script uses [`./.fastly/config.toml`](./.fastly/config.toml) as a template file to then dynamically inject a list of starter kits (pulling their data from their public repositories).

The resulting configuration is then saved to disk at `./pkg/config/config.toml` and embedded into the CLI when compiled.

### Running Compute commands locally

If you need to test the Fastly CLI locally while developing a Compute feature, then use the `--dir` flag (exposed on `compute build`, `compute deploy`, `compute serve` and `compute publish`) to ensure the CLI doesn't attempt to treat the repository directory as your project directory.

```shell
go run cmd/fastly/main.go compute deploy --verbose --dir ../../test-projects/testing-fastly-cli
```
7 changes: 1 addition & 6 deletions pkg/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,7 @@ func Content(flagval string) string {
content := flagval
if path, err := filepath.Abs(flagval); err == nil {
if _, err := os.Stat(path); err == nil {
// gosec flagged this:
// G304 (CWE-22): Potential file inclusion via variable
//
// Disabling as we require a user to configure their own environment.
/* #nosec */
if data, err := os.ReadFile(path); err == nil {
if data, err := os.ReadFile(path); err == nil /* #nosec */ {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: Latest gosec (released today) changed the way #nosec is parsed. It has to be on the affected line.

content = string(data)
}
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/commands/authtoken/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ func (c *DeleteCommand) constructInputBatch() (*fastly.BatchDeleteTokensInput, e

if path, err = filepath.Abs(c.file); err == nil {
if _, err = os.Stat(path); err == nil {
// gosec flagged this:
// G304 (CWE-22): Potential file inclusion via variable
// Disabling as we trust the source of the path variable.
/* #nosec */
if file, err = os.Open(path); err == nil {
if file, err = os.Open(path); err == nil /* #nosec */ {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
tokens = append(tokens, &fastly.BatchToken{ID: scanner.Text()})
Expand Down
47 changes: 36 additions & 11 deletions pkg/commands/compute/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const CustomPostScriptMessage = "This project has a custom post_%s script define

// Flags represents the flags defined for the command.
type Flags struct {
Dir string
IncludeSrc bool
Lang string
PackageName string
Expand All @@ -50,11 +51,12 @@ type BuildCommand struct {
func NewBuildCommand(parent cmd.Registerer, g *global.Data, m manifest.Data) *BuildCommand {
var c BuildCommand
c.Globals = g
c.Manifest = m
c.Manifest = m // TODO: Stop passing a non-mutable 'copy' in any commands.
c.CmdClause = parent.Command("build", "Build a Compute@Edge package locally")

// NOTE: when updating these flags, be sure to update the composite commands:
// `compute publish` and `compute serve`.
c.CmdClause.Flag("dir", "Project directory to build (default: current directory)").Short('C').StringVar(&c.Flags.Dir)
c.CmdClause.Flag("include-source", "Include source code in built package").BoolVar(&c.Flags.IncludeSrc)
c.CmdClause.Flag("language", "Language type").StringVar(&c.Flags.Lang)
c.CmdClause.Flag("package-name", "Package name").StringVar(&c.Flags.PackageName)
Expand All @@ -72,6 +74,25 @@ func (c *BuildCommand) Exec(in io.Reader, out io.Writer) (err error) {
out = io.Discard
}

var projectDir string
if c.Flags.Dir != "" {
projectDir, err = filepath.Abs(c.Flags.Dir)
if err != nil {
return fmt.Errorf("failed to construct absolute path to directory '%s': %w", c.Flags.Dir, err)
}
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current working directory: %w", err)
}
if err := os.Chdir(projectDir); err != nil {
return fmt.Errorf("failed to change working directory to '%s': %w", projectDir, err)
}
defer os.Chdir(wd)
if c.Globals.Verbose() {
text.Info(out, "Changed project directory to '%s'\n\n", projectDir)
}
}

spinner, err := text.NewSpinner(out)
if err != nil {
return err
Expand All @@ -84,7 +105,11 @@ func (c *BuildCommand) Exec(in io.Reader, out io.Writer) (err error) {
}(c.Globals.ErrLog)

err = spinner.Process("Verifying fastly.toml", func(_ *text.SpinnerWrapper) error {
err = c.Manifest.File.ReadError()
if projectDir == "" {
err = c.Globals.Manifest.File.ReadError()
} else {
err = c.Globals.Manifest.File.Read(filepath.Join(projectDir, manifest.Filename))
}
if err != nil {
if errors.Is(err, os.ErrNotExist) {
err = fsterr.ErrReadingManifest
Expand Down Expand Up @@ -219,8 +244,8 @@ func packageName(c *BuildCommand) (string, error) {
switch {
case c.Flags.PackageName != "":
name = c.Flags.PackageName
case c.Manifest.File.Name != "":
name = c.Manifest.File.Name // use the project name as a fallback
case c.Globals.Manifest.File.Name != "":
name = c.Globals.Manifest.File.Name // use the project name as a fallback
default:
return "", fsterr.RemediationError{
Inner: fmt.Errorf("package name is missing"),
Expand All @@ -242,8 +267,8 @@ func identifyToolchain(c *BuildCommand) (string, error) {
switch {
case c.Flags.Lang != "":
toolchain = c.Flags.Lang
case c.Manifest.File.Language != "":
toolchain = c.Manifest.File.Language
case c.Globals.Manifest.File.Language != "":
toolchain = c.Globals.Manifest.File.Language
default:
return "", fmt.Errorf("language cannot be empty, please provide a language")
}
Expand All @@ -260,7 +285,7 @@ func language(toolchain string, c *BuildCommand, in io.Reader, out io.Writer, sp
Name: "assemblyscript",
SourceDirectory: AsSourceDirectory,
Toolchain: NewAssemblyScript(
&c.Manifest.File,
&c.Globals.Manifest.File,
c.Globals,
c.Flags,
in,
Expand All @@ -273,7 +298,7 @@ func language(toolchain string, c *BuildCommand, in io.Reader, out io.Writer, sp
Name: "go",
SourceDirectory: GoSourceDirectory,
Toolchain: NewGo(
&c.Manifest.File,
&c.Globals.Manifest.File,
c.Globals,
c.Flags,
in,
Expand All @@ -286,7 +311,7 @@ func language(toolchain string, c *BuildCommand, in io.Reader, out io.Writer, sp
Name: "javascript",
SourceDirectory: JsSourceDirectory,
Toolchain: NewJavaScript(
&c.Manifest.File,
&c.Globals.Manifest.File,
c.Globals,
c.Flags,
in,
Expand All @@ -299,7 +324,7 @@ func language(toolchain string, c *BuildCommand, in io.Reader, out io.Writer, sp
Name: "rust",
SourceDirectory: RustSourceDirectory,
Toolchain: NewRust(
&c.Manifest.File,
&c.Globals.Manifest.File,
c.Globals,
c.Flags,
in,
Expand All @@ -311,7 +336,7 @@ func language(toolchain string, c *BuildCommand, in io.Reader, out io.Writer, sp
language = NewLanguage(&LanguageOptions{
Name: "other",
Toolchain: NewOther(
&c.Manifest.File,
&c.Globals.Manifest.File,
c.Globals,
c.Flags,
in,
Expand Down
Loading
Loading