Skip to content
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
10 changes: 6 additions & 4 deletions internal/sidekick/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ type Config struct {
// GeneralConfig contains configuration parameters that affect Parsers and Codecs, including the
// selection of parser and codec.
type GeneralConfig struct {
Language string `toml:"language,omitempty"`
SpecificationFormat string `toml:"specification-format,omitempty"`
SpecificationSource string `toml:"specification-source,omitempty"`
ServiceConfig string `toml:"service-config,omitempty"`
Language string `toml:"language,omitempty"`
SpecificationFormat string `toml:"specification-format,omitempty"`
SpecificationSource string `toml:"specification-source,omitempty"`
ServiceConfig string `toml:"service-config,omitempty"`
IgnoredDirectories []string `toml:"ignored-directories,omitempty"`
}

// LoadConfig loads the top-level configuration file and validates its contents.
Expand Down Expand Up @@ -130,6 +131,7 @@ func mergeConfigs(rootConfig, local *Config) (*Config, error) {
General: GeneralConfig{
Language: rootConfig.General.Language,
SpecificationFormat: rootConfig.General.SpecificationFormat,
IgnoredDirectories: rootConfig.General.IgnoredDirectories,
},
Source: map[string]string{},
Codec: map[string]string{},
Expand Down
3 changes: 3 additions & 0 deletions internal/sidekick/internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func TestLoadRootConfig(t *testing.T) {
General: GeneralConfig{
Language: "root-language",
SpecificationFormat: "root-specification-format",
IgnoredDirectories: []string{"a", "b"},
},
Source: map[string]string{
"s1": "v1",
Expand Down Expand Up @@ -101,6 +102,7 @@ func TestMergeLocalForGeneral(t *testing.T) {
General: GeneralConfig{
Language: "root-language",
SpecificationFormat: "root-specification-format",
IgnoredDirectories: []string{"a", "b"},
},
}

Expand All @@ -123,6 +125,7 @@ func TestMergeLocalForGeneral(t *testing.T) {
SpecificationFormat: "local-specification-format",
SpecificationSource: "local-specification-source",
ServiceConfig: "local-service-config",
IgnoredDirectories: []string{"a", "b"},
},
Codec: map[string]string{},
Source: map[string]string{},
Expand Down
26 changes: 11 additions & 15 deletions internal/sidekick/refreshall.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"path"
"path/filepath"
"slices"
"strings"
"sync"

Expand Down Expand Up @@ -68,7 +69,7 @@ func refreshAll(rootConfig *config.Config, cmdLine *CommandLine) error {
if err != nil {
return err
}
directories, err := findAllDirectories()
directories, err := findAllDirectories(override)
if err != nil {
return err
}
Expand Down Expand Up @@ -104,28 +105,18 @@ func refreshAll(rootConfig *config.Config, cmdLine *CommandLine) error {
return errors.Join(failures...)
}

func findAllDirectories() ([]string, error) {
func findAllDirectories(config *config.Config) ([]string, error) {
var result []string
err := fs.WalkDir(os.DirFS("."), ".", func(path string, d fs.DirEntry, _ error) error {
if d.IsDir() {
return nil
}
dir := filepath.Dir(path)

// TODO(https://github.com/googleapis/librarian/issues/1563): do not
// harcode
if dir == "dart" {
return nil
}
ignored := []string{
"dart/", // Testing
"target/package/", // The output from `cargo package`
}
for _, candidate := range ignored {
if strings.Contains(dir, candidate) {
for _, ignore := range config.General.IgnoredDirectories {
if isInPath(ignore, path) {
return nil
}
}
dir := filepath.Dir(path)
if d.Name() == ".sidekick.toml" && dir != "." {
result = append(result, dir)
}
Expand All @@ -136,3 +127,8 @@ func findAllDirectories() ([]string, error) {
}
return result, nil
}

func isInPath(dir, path string) bool {
components := strings.Split(path, string(filepath.Separator))
return slices.Contains(components, dir)
}
23 changes: 23 additions & 0 deletions internal/sidekick/refreshall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,26 @@ func TestRefreshAll(t *testing.T) {
t.Fatal(err)
}
}

func TestIsInPath(t *testing.T) {
type TestCase struct {
Dir string
Path string
Want bool
}
testCases := []TestCase{
{"dart", "dart", true},
{"dart", "dartboard", false},
{"dart", "dart/v2", true},
{"dart", "dart/v2/d2/d4", true},
{"dart", "a/b/c/d/dart/v2/d2/d4", true},
{"generator", "dart/v2", false},
{"generator", "generator/", true},
}
for _, test := range testCases {
got := isInPath(test.Dir, test.Path)
if got != test.Want {
t.Errorf("got (%v) != want (%v) for %q in %q", got, test.Want, test.Dir, test.Path)
}
}
}
Loading