36 lines
1006 B
Go
36 lines
1006 B
Go
package apigen
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// ProjectEntry discovers the API entry recorded by generation, never guessing
|
|
// which of several .api files is the service entry.
|
|
func ProjectEntry(directory string) (string, error) {
|
|
root, err := filepath.Abs(directory)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if err = safePath(root, manifestPath); err != nil {
|
|
return "", err
|
|
}
|
|
data, err := os.ReadFile(filepath.Join(root, manifestPath))
|
|
if err != nil {
|
|
return "", fmt.Errorf("--api is required outside a generated project (no readable %s)", manifestPath)
|
|
}
|
|
var m manifest
|
|
if err = json.Unmarshal(data, &m); err != nil {
|
|
return "", fmt.Errorf("invalid API project manifest: %w", err)
|
|
}
|
|
if m.Version != 2 || filepath.Ext(m.Entry) != ".api" {
|
|
return "", fmt.Errorf("invalid API entry in %s; pass --api explicitly", manifestPath)
|
|
}
|
|
if err = safePath(root, m.Entry); err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(root, filepath.FromSlash(m.Entry)), nil
|
|
}
|