diff --git a/cmd/generate/main.go b/cmd/generate/main.go new file mode 100644 index 0000000..1104c8e --- /dev/null +++ b/cmd/generate/main.go @@ -0,0 +1,30 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "context" + "log" + "os" + + "github.com/googleapis/generator/internal/generate" +) + +func main() { + ctx := context.Background() + if err := generate.Run(ctx, os.Args[1:]...); err != nil { + log.Fatal(err) + } +} diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/internal/generate/errors.go b/internal/generate/errors.go new file mode 100644 index 0000000..2bd81f6 --- /dev/null +++ b/internal/generate/errors.go @@ -0,0 +1,19 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package generate + +import "errors" + +var errNotImplemented = errors.New("generator: functionality not implemented for language") diff --git a/internal/generate/flags.go b/internal/generate/flags.go new file mode 100644 index 0000000..143b503 --- /dev/null +++ b/internal/generate/flags.go @@ -0,0 +1,87 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package generate + +import ( + "flag" + "fmt" +) + +type config struct { + googleapis string + language string + output string +} + +func parseFlags(cfg *config, args []string) (*config, error) { + flags := flag.NewFlagSet("", flag.ContinueOnError) + flags.StringVar(&cfg.language, "language", "", "specify from cpp, csharp, go, java, node, php, python, ruby, rust") + flags.StringVar(&cfg.googleapis, "googleapis", "", "location of googleapis `dir`") + flags.StringVar(&cfg.output, "output", "", "location of generated client library output") + + // We don't want to print the whole usage message on each flags + // error, so we set to a no-op and do the printing ourselves. + flags.Usage = func() {} + usage := func() { + fmt.Fprint(flags.Output(), `Generator generates client libraries for Google APIs. + +Usage: + + generator [command] [flags] + +Flags: + +`) + flags.PrintDefaults() + fmt.Fprintf(flags.Output(), "\n\n") + } + + if err := flags.Parse(args); err != nil { + if err == flag.ErrHelp { + usage() // print usage only on help + } + return nil, err + } + if err := validateConfig(cfg); err != nil { + usage() // print usage only on help + return nil, err + } + return cfg, nil +} + +func validateConfig(cfg *config) error { + switch cfg.language { + case "cpp": + return errNotImplemented + case "csharp": + return errNotImplemented + case "go": + return errNotImplemented + case "java": + return errNotImplemented + case "node": + return errNotImplemented + case "php": + return errNotImplemented + case "python": + return errNotImplemented + case "ruby": + return errNotImplemented + case "rust": + return errNotImplemented + default: + return fmt.Errorf("invalid -language flag specified: %q", cfg.language) + } +} diff --git a/internal/generate/run.go b/internal/generate/run.go new file mode 100644 index 0000000..6dcdf08 --- /dev/null +++ b/internal/generate/run.go @@ -0,0 +1,28 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package generate + +import ( + "context" +) + +func Run(ctx context.Context, arg ...string) error { + cfg := &config{} + _, err := parseFlags(cfg, arg) + if err != nil { + return err + } + return nil +}