8000 Detect main.go in subdir when running air init with defaults by nesselchen · Pull Request #736 · air-verse/air · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Detect main.go in subdir when running air init with defaults #736

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 24 additions & 2 deletions runner/config.go
D90F
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"flag"
"fmt"
"io/fs"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -204,8 +205,9 @@ func readConfByName(name string) (*Config, error) {
}

func defaultConfig() Config {
entrypoint := inferEntrypoint()
build := cfgBuild{
Cmd: "go build -o ./tmp/main .",
Cmd: "go build -o ./tmp/main " + entrypoint,
Bin: "./tmp/main",
Log: "build-errors.log",
IncludeExt: []string{"go", "tpl", "tmpl", "html"},
Expand All @@ -223,7 +225,7 @@ func defaultConfig() Config {
}
if runtime.GOOS == PlatformWindows {
build.Bin = `tmp\main.exe`
build.Cmd = "go build -o ./tmp/main.exe ."
build.Cmd = "go build -o ./tmp/main.exe " + entrypoint
}
log := cfgLog{
AddTime: false,
Expand Down Expand Up @@ -254,6 +256,26 @@ func defaultConfig() Config {
}
}

func inferEntrypoint() string {
entrypoint := "."
if _, err := os.Stat("./main.go"); err == nil {
return entrypoint
}
filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if name := info.Name(); name == "main.go" {
entrypoint = path
return fs.SkipAll
}
return nil
})

// default to . if no main.go is found anywhere in the tree
return entrypoint
}

func readConfig(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
Expand Down
0