Skip to content

Commit

Permalink
impl: cmd/generate: add inital command interface
Browse files Browse the repository at this point in the history
Add initial command interface for generator.
  • Loading branch information
julieqiu committed Nov 25, 2024
1 parent d390c27 commit 909f79f
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cmd/generate/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Empty file added go.sum
Empty file.
19 changes: 19 additions & 0 deletions internal/generate/errors.go
Original file line number Diff line number Diff line change
@@ -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")
87 changes: 87 additions & 0 deletions internal/generate/flags.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
28 changes: 28 additions & 0 deletions internal/generate/run.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 909f79f

Please sign in to comment.